一楼的写法的确是有BUG的,并非是溢出问题,假设延时 30ms 0x1E:
void HAL_Delay(__IO uint32_t Delay)
{
__IO uint32_t timingdelay;
timingdelay = uwTick + Delay; //假设此时uwTick=0xFFFFFFF0 Delay=0x1E 则 timingdelay=0x1 0000000E,溢出后保留32位=0x0000000E。
//while(HAL_GetTick() < timingdelay)
while(uwTick < timingdelay) //此时的uwTick仍是0xFFFFFFF0或任何小于0xFFFFFFFF的值,则 if(0xFFFFFFF0<0x0000000E) 为假,立即退出,并没有延时30ms。
{;
}
}
正确的写法应该是这样,仍然假设延时 30ms 0x1E:
__weak void HAL_Delay(__IO uint32_t Delay)
{
uint32_t tickstart = 0;
tickstart = HAL_GetTick(); //假设此时uwTick=0xFFFFFFF0 Delay=0x1E 则 tickstart=0xFFFFFFF0。
while((HAL_GetTick() - tickstart) < Delay) //此时的uwTick仍是0xFFFFFFF0或任何小于0xFFFFFFFF的值时,假如是0xFFFFFFF5-0xFFFFFFF0=0x05,if(0x05<0x1E)为真,继续等待。
//如果溢出后 uwTick=0x0000000D,则0x0000000D-0xFFFFFFF0=0x1D,if(0x1D<0x1E)为真,继续等待。
//继续 uwTick=0x0000000E,则0x0000000E-0xFFFFFFF0=0x1E,if(0x1E<0x1E)为假,延时完成,退出。
{
}
}
|