cancel
Showing results for 
Search instead for 
Did you mean: 

How to generate a delay within a Timer Callback function?

Marco S
Associate II
Posted on June 15, 2017 at 10:54

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?

#timer #delay #hal #stm32f091
1 REPLY 1
Zt Liu
Senior III
Posted on June 15, 2017 at 16:19

What about use timerA(according to your update period) to trigger timerB(according to your RC time),

then your adc is trigger by timerB.