SysTick系统滴答定时器:
core-cm4.h定义了ARM芯片通用的systick配置(使用默认时钟HCLK):
__STATIC_INLINE uint32_t SysTick_Config(uint32_t ticks);
The function initializes the System Timer and its interrupt, and starts the System Tick Timer.
Counter is in free running mode to generate periodic interrupts.
param [in] ticks Number of ticks between two interrupts.
eturn 0 Function succeeded.
eturn 1 Function failed.
clk.c定义了systick配置:
void CLK_SetSysTickClockSrc(uint32_t u32ClkSrc); //作用???
* @param[in] u32ClkSrc is module clock source. Including:
* -
ef CLK_CLKSEL0_STCLKSEL_HXT
* -
ef CLK_CLKSEL0_STCLKSEL_LXT
* -
ef CLK_CLKSEL0_STCLKSEL_HXT_DIV2
* -
ef CLK_CLKSEL0_STCLKSEL_HCLK_DIV2
* -
ef CLK_CLKSEL0_STCLKSEL_HIRC_DIV2
void CLK_EnableSysTick(uint32_t u32ClkSrc, uint32_t u32Count); //使能、并开中断
* @param[in] u32ClkSrc is System Tick clock source. Including:
* -
ef CLK_CLKSEL0_STCLKSEL_HXT
* -
ef CLK_CLKSEL0_STCLKSEL_LXT
* -
ef CLK_CLKSEL0_STCLKSEL_HXT_DIV2
* -
ef CLK_CLKSEL0_STCLKSEL_HCLK_DIV2
* -
ef CLK_CLKSEL0_STCLKSEL_HIRC_DIV2
* -
ef CLK_CLKSEL0_STCLKSEL_HCLK
* @param[in] u32Count is System Tick reload value. It could be 0~0xFFFFFF.
void CLK_DisableSysTick(void) ;
clk.h定义了systick延时(不开中断模式实现):
//时钟源使用HCLK,不开中断,输入的值为内部转换的us值,该函数不用初始化直接用。
__STATIC_INLINE void CLK_SysTickDelay(uint32_t us);
* @param us Delay time. The Max value is 2^24 / CPU Clock(MHz). Ex:
* 72MHz => 233016us, 50MHz => 335544us,
48MHz => 349525us, 28MHz => 699050us ...
1 //初始化滴答定时器 SystemCoreClock = 1s 2 SysTick_Config(SystemCoreClock); 3 4 uint32_t systick_delay; 5 //SysTick_Config()函数在core_cn4.h已定义,可直接使用。 6 //CLK_EnableSysTick()函数在clk.c文件已定义,也可直接使用。 7 //区别在与SysTick_Config()函数使用默认时钟源HCLK,CLK_EnableSysTick()函数可配置。 8 void SysTick_Handler(void) 9 { 10 systick_delay++; 11 } 12 13 systick_delay = 0; 14 while(systick_delay <= 5);
其实上面程序有个硬伤:SysTick_Config(SystemCoreClock)函数不能接收大于0xffffff的值,因为计数器是24位的,所以初始化该函数会失败,导致后续延时就会卡死!!