stm32滴答时钟SYStick的优先级设定方法详解

STV3.50固件为对象,:
1.     滴答寄存器的定义和地址分配在内核 .H文件中如下
typedef struct
{
  __IO uint32_t CTRL;                         /*!< Offset: 0x00  SysTick Control and Status Register */
  __IO uint32_t LOAD;                         /*!< Offset: 0x04  SysTick Reload Value Register     */
  __IO uint32_t VAL;                          /*!< Offset: 0x08  SysTick Current Value Register     */
  __I  uint32_t CALIB;                        /*!< Offset: 0x0C  SysTick Calibration Register      */
} SysTick_Type;
#define SysTick             ((SysTick_Type *)       SysTick_BASE)
定义结构指针 SYSTICK,并赋值为物理存储器首地址 0xE000E010,由于结构是顺序的所以可以用结构指针来访问寄存器。
因为滴答事件是内核的异常所以还要牵扯到另一个寄存器 SCB寄存器(系统控制块)
typedef struct
{
__I  uint32_t CPUID;        /*!< Offset: 0x00  CPU ID Base Register                          */
  __IO uint32_t ICSR;       /*!< Offset: 0x04  Interrupt Control State Register                      */
  __IO uint32_t VTOR;     /*!< Offset: 0x08  Vector Table Offset Register                          */
  __IO uint32_t AIRCR;    /*!< Offset: 0x0C  Application Interrupt / Reset Control Register        */
  __IO uint32_t SCR;    /*!< Offset: 0x10  System Control Register                               */
  __IO uint32_t CCR;     /*!< Offset: 0x14  Configuration Control Register                        */
  __IO uint8_t  SHP[12];    /*!< Offset: 0x18  System Handlers Priority Registers (4-7, 8-11, 12-15) */
  __IO uint32_t SHCSR;  /*!< Offset: 0x24  System Handler Control and State Register             */
  __IO uint32_t CFSR;  /*!< Offset: 0x28  Configurable Fault Status Register                    */
  __IO uint32_t HFSR;   /*!< Offset: 0x2C  Hard Fault Status Register                            */
  __IO uint32_t DFSR;   /*!< Offset: 0x30  Debug Fault Status Register                           */
  __IO uint32_t MMFAR; /*!< Offset: 0x34  Mem Manage Address Register                           */
  __IO uint32_t BFAR; /*!< Offset: 0x38  Bus Fault Address Register                            */
  __IO uint32_t AFSR; /*!< Offset: 0x3C  Auxiliary Fault Status Register                        */
  __I  uint32_t PFR[2]; /*!< Offset: 0x40  Processor Feature Register                           */
  __I  uint32_t DFR;  /*!< Offset: 0x48  Debug Feature Register                              */
  __I  uint32_t ADR; /*!< Offset: 0x4C  Auxiliary Feature Register                            */
  __I  uint32_t MMFR[4]; /*!< Offset: 0x50  Memory Model Feature Register                    */
  __I  uint32_t ISAR[5]; /*!< Offset: 0x60  ISA Feature Register                                  */
} SCB_Type;
这里面寄存器很多,在这里大部分我们不用去管,有一个数组 SHP[12],一定要看清他是8位数组啊!这个数组是很重要的,他是用来设置内核异常的优先级别,并不是想的在 NVIC里设置,那个是大于 15号中断的优先组别,换句话就是外部中断什么看门狗,定时器,串口啥的,外设的中断优先级设置在 NVICIP数组中,而小于这个的都是内部异常,他不归 NVIC管制,他受谁管呢?就是这个 SHP[12],滴答属于内核的异常所以他要用 SHP[12]来设置,和内核手册中讲到的那三个 (SHRP1-SHRP3)32位寄存器一一对应,算下来正好有 12个字节,最后一个字节就是我要的 SYStick的优先级设置,他只用了他的高四位,而第四位保留,所以他的范围是 0-15之间的任意数!!
2.系统设置函数:
NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1);
这就是固件库里对 SYSTICK的优先级设置,,后面参数,这里的 __NVIC_PRIO_BITS=4,变形 =1《《 4-1=0xf;第一个参数是表明这是对滴答进行设置, SysTick_IRQn=-1,看实体:
static __INLINE void NVIC_SetPriority(IRQn_Type IRQn, uint32_t priority)
if(IRQn < 0) {
    SCB->SHP[((uint32_t)(IRQn) & 0xF)-4] = ((priority << (8 - __NVIC_PRIO_BITS)) & 0xff); } /* set Priority for Cortex-M3 System Interrupts */
  else {
NVIC->IP[(uint32_t)(IRQn)] = ((priority << (8 - __NVIC_PRIO_BITS)) & 0xff);    }
这里有个 IF,就是这个 IF来判别是内核异常还是外部异常的优先级,如果是内核异常那么由 SCB解决,如果不是那么有 NVIC解决。显然这个是由 SCB解决的。
SHP[((uint32_t)(IRQn) & 0xF)-4]变换 =SHP[f-4]=SHP[11],那么这个 SHP[11]就是设置滴答的优先级!他的优先级是多少?
((priority << (8 - __NVIC_PRIO_BITS)) & 0xff);=priority<<4=(f<<4)=0xf0;
显然这就是他的优先级是 15!!!!!!!!!!!!!!!
有了优先级就了解滴答了,即使没有固件库函数依然可以设置看看自己设置的函数:
void mysystickint()
{
SysTick->LOAD=71999;//加载值 =1ms
SCB->SHP[11]=15;//设置 SYSTICK的优先级为 15,注意 SYSTICK属于系统异常,所以他的优先级在 SCB里设置。
SysTick->CTRL=7;//开启中断,开启定时器,时钟设置为 HCLK=72mhz
}这是中断模式,另外也可以选择查询模式,只是占 CPU时间,查询模式下根本不用设置什么优先级!他只要开开滴答,查询标志位即可!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值