STM32使用systick實(shí)現(xiàn)精確延時(shí)
SYSTICK寄存器初始化
void SysTick_Configuration(void)
{
if (SysTick_Config(SystemCoreClock / 100))
{
while (1);
}
NVIC_SetPriority(SysTick_IRQn, 0x0);
}
SysTick_Config默認(rèn)時(shí)鐘為SysTick_CLKSource_HCLK,所以在這之前使用SysTick_CLKSourceConfig()選擇系統(tǒng)時(shí)鐘不會(huì)改變systick的時(shí)鐘
static __INLINE uint32_t SysTick_Config(uint32_t ticks)
{
if (ticks > SysTick_LOAD_RELOAD_Msk)return (1);
SysTick->LOAD= (ticks & SysTick_LOAD_RELOAD_Msk) - 1;
NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1);
SysTick->VAL= 0;
SysTick->CTRL=SysTick_CTRL_CLKSOURCE_Msk |
SysTick_CTRL_TICKINT_Msk|
SysTick_CTRL_ENABLE_Msk;
return (0);
}
systick中斷服務(wù)程序
void SysTick_Handler(void)
{
systime ++;
}
延時(shí)程序
void delay_ms(unsigned int ms)
{
unsigned int temp;
temp = systime;
while((systime - temp) < ms);
}
void SysTick_CLKSourceConfig(uint32_t SysTick_CLKSource)在misc.c文件中定義
static __INLINE uint32_t SysTick_Config(uint32_t ticks)在core_cm3.h中定義