今天尝试用sysTick来构造10ms的延时函数Delay(),但是似乎严重不准确,我Delay(500),应该得到5s的延时,但我掐着表,2秒钟就从串口发出数据了。奇怪。高手多多指教~~
systick的设置 void SysTick_Config(void) { /* Configure HCLK clock as SysTick clock source */ SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);//HLCK is 72MHz,so the source of sysTick is 72MHz.
SysTick_SetReload(720000); // 每tick一次,过去 1/72M 秒; 达到720,000次,则过去0.01s(10ms). /* Enable the SysTick Interrupt */ SysTick_ITConfig(ENABLE);
}
中断处理函数 void SysTickHandler(void) { if(TimingDelay != 0) TimingDelay--; }
delay函数 void Delay(u32 param) { /* Enable the SysTick Counter */ SysTick_CounterCmd(SysTick_Counter_Enable); TimingDelay = param; while(TimingDelay != 0) { } SysTick_CounterCmd(SysTick_Counter_Disable); SysTick_CounterCmd(SysTick_Counter_Clear); }
|