火龙果MM32F3273G8P开发板MindSDK开发教程4 - 滴嗒定时器Systick的配置
1、Systick寄存器
Systick是ARM内核的一个外设,所以在不同芯片的代码上移植比较方便,他总共有4个寄存器,
从Systick定义中可以看到:
typedef struct
{__IOM uint32_t CTRL; /*!< Offset: 0x000 (R/W) SysTick Control and Status Register */__IOM uint32_t LOAD; /*!< Offset: 0x004 (R/W) SysTick Reload Value Register */__IOM uint32_t VAL; /*!< Offset: 0x008 (R/W) SysTick Current Value Register */__IM uint32_t CALIB; /*!< Offset: 0x00C (R/ ) SysTick Calibration Register */
} SysTick_Type;
2、轮询方式延时
一般,systick ms级别的延时,采用中断方式,load值为sysclk/1000,这样每ms产生中断,可以用来ms级别的延时,或者为freeRtos提供节拍。
同样,采用轮询方式延时,也可以实现ms级的延时,并且可以实现us级的延时。
systick的时钟源分两种,ctrl寄存器第二位为0时采用sysclk/8的时钟频率,第二位为1时采用系统时钟。
轮询方式延时采用sysclk/8分频方式,而中断方式直接采用系统时钟。
假如系统时钟为120M,8分频后为15M,即1/15000000计一次数,1us需要15次。所以代码中倍频因子为了代码的可移植性,使其count_1us = sysclk/8000000 = 15。
相关代码:
volatile static float count_1us = 0;
volatile static float count_1ms = 0;/*** 初始化滴答定时器函数* 轮询方式延时
*/void BOARD_Systick_Init()
{// Systick CTRL 寄存器 第二位为0时,Systick时钟==Sysclk/8 ,// 第二位为1时,Systick = SysclkSysTick->CTRL &= ~(1<<2);// 将systick使用内核时钟,根据时钟树,即120/8=15M;count_1us = (float)(CLOCK_GetBootHSEValue()/8000000);count_1ms = (float)1000*count_1us;
}void BOARD_Delay1Us(uint32_t count)
{uint32_t ctl;/* reload the count value */SysTick->LOAD = (uint32_t)(count * count_1us);/* clear the current count value */SysTick->VAL = 0x0000U;/* enable the systick timer */SysTick->CTRL = SysTick_CTRL_ENABLE_Msk;/* wait for the COUNTFLAG flag set */do{ctl = SysTick->CTRL;}while((ctl&SysTick_CTRL_ENABLE_Msk)&&!(ctl & SysTick_CTRL_COUNTFLAG_Msk));/* disable the systick timer */SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;/* clear the current count value */SysTick->VAL = 0x0000U;}void BOARD_Delay1Ms(uint32_t count)
{uint32_t ctl;/* reload the count value */SysTick->LOAD = (uint32_t)(count * count_1ms);/* clear the current count value */SysTick->VAL = 0x0000U;/* enable the systick timer */SysTick->CTRL = SysTick_CTRL_ENABLE_Msk;/* wait for the COUNTFLAG flag set */do{ctl = SysTick->CTRL;}while((ctl&SysTick_CTRL_ENABLE_Msk)&&!(ctl & SysTick_CTRL_COUNTFLAG_Msk));/* disable the systick timer */SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;/* clear the current count value */SysTick->VAL = 0x0000U;
}
3、实验现象
BOARD_Delay1Ms(1000)
BOARD_Delay1Us(1000)
产生的对应波形为
4、代码
https://gitee.com/xiaoguo-tec_0/mm32-f3273.git