AnsweredAssumed Answered

How to generate a delay within a Timer Callback function?

Question asked by Marco S on Jun 15, 2017
Latest reply on Jun 15, 2017 by Zhitai Liu

Hello, I'm trying to read the tension in a port with the ADC every 2 seconds. In order to achieve this, I call the ADC reading function within a HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) function and I have set the timer to accurately count 2000 ms. Nevertheless, as there is a buffer with an RC filter right before the input, I have to wait a time 10 times tau (where tau = RC) to capture the value. So, before doing the lecture I introduce a delay function with a for loop and a nop assembly instruction. This is not accurate, so I have tried to use the following function implemented with the timer:

 

void delay(uint32_t t){
    uint32_t t1 = __HAL_TIM_GET_COUNTER(&htim3);
    while((__HAL_TIM_GET_COUNTER(&htim3)-t1)<t);
}

 

But calling this function within the Timer Callback function blocks the MCU. The same happens if I try to use the HAL_Delay function. How can I implement an accurate delay within the Timer Callback? Or is it just that it's not possible as it is a bad practice?

Outcomes