# 关于STM32的HAL库中超时函数中的tick溢出后产生的问题的思考及求证

__IO uint32_t uwTick;
__weak uint32_t HAL_GetTick(void)
{
return uwTick;
}

源代码：

__weak void HAL_Delay(__IO uint32_t Delay)
{
uint32_t tickstart = 0;
tickstart = HAL_GetTick();
while((HAL_GetTick() - tickstart) < Delay)
{
}
}

(uint32_t)uwTick - (uint32_t）（tickstart）=  0x00 -  (0xFFFF FFFF - 0x0A)  = 0x00 +  (uint32_t) 0xA - 0xFFFF FFFF = (uint32_t) 0x ‭FFFFFFFF00000001‬ = 0x01;

(uint32_t)uwTick - (uint32_t）（tickstart）=  0x01 -  (0xFFFF FFFF - 0x0A)  = 0x01 -  (uint32_t) 0xA - 0xFFFF FFFF = (uint32_t) 0x ‭FFFFFFFF00000002‬ = 0x02;

(uint32_t)uwTick - (uint32_t）（tickstart）=  0x01 -  (0xFFFF FFFF - 0x0A)  = 0x02 -  (uint32_t) 0xA - 0xFFFF FFFF = (uint32_t) 0x ‭FFFFFFFF00000003‬ = 0x03;

…… 继续累加到0xD

(uint32_t)uwTick - (uint32_t）（tickstart）=  0x0D-  (0xFFFF FFFF - 0x0A)  = 0x0D -  (uint32_t) 0xA - 0xFFFF FFFF = (uint32_t) 0x ‭FFFFFFFF00000003‬ = 0x0D;

(HAL_GetTick() - tickstart) = uwTick - tickstart = 0x0D ==》等于要求的延时 Delay = 0x0D ，定时到退出while循环；



wiwa 2016.12.13