Problem making an accurate ms delay, either HAL_Delay or my own delay function
Hi,
I am facing an odd situation when I use HAL_Delay. In most of the functions I have it works well, but in some functions the program stays in this loop forever:
while((HAL_GetTick() - tickstart) < wait) {}
When I debug, the difference between two values are more than the wait value, but the while statement is always TRUE and stays in the loop !!!
A similar problem exists when I make my own delay using a timer like TIM21. It rises an interrupt and I decrease a counter in the handler as below:
void delay_m(uint16_t ms)
{
HAL_StatusTypeDef state;
if (HAL_TIM_Base_Start_IT(&htim21) != HAL_OK)
{
Error_Handler();
}
myDelayCount = ms;
while(myDelayCount)
LDR_OFF();
HAL_TIM_Base_Stop_IT(&htim21);
return;
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM21)
{
myDelayCount --;
}
}
Again: it works well sometimes, but after a while or at some conditions, this operation fails. The myDelayCount would reach 0 or whatever condition I set, but the while statement is not true at all and stays in the while(myDelayCount) for ever.
Could someone help me about this issue?
Thanks
