// dwTime <= 1000ns
void Delay_us(vu32 dwTime)
{
vu32 dwCurCounter = 0; //当前时间计数值
vu32 dwPreTickVal = SysTick->VAL; //上一次SYSTICK计数值
vu32 dwCurTickVal; //上一次SYSTICK计数值
// dwTime = dwTime * (rcc_clocks.HCLK_Frequency/1000000); //需延时时间,共多少时间节拍
dwTime = dwTime * 9; //SysTick_SetReload(1000*9); //RCC_PLLMul_9
for(;;)
{
dwCurTickVal = SysTick->VAL;
if(dwCurTickVal < dwPreTickVal)
{
dwCurCounter += dwPreTickVal - dwCurTickVal;
}else{
dwCurCounter += dwPreTickVal + SysTick->LOAD - dwCurTickVal;
}
dwPreTickVal = dwCurTickVal;
if(dwCurCounter >= dwTime) return;
}
}
void Delay_ms(u16 wTime)
{
u16 i;
for(i = 0; i < wTime; i ++) Delay_us(1000);
}
我得弄成这样才能用,变量声明成volatile 很重要! 不然延时50MS 实际打印出来是35~40ms随机波动
现在这样写打印常来很标准了
相关配置:
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
SysTick_SetReload(1000*9);
SysTick_CounterCmd(SysTick_Counter_Enable);
SysTick->VAL = 1000*9;
SysTick_ITConfig(ENABLE);
|