cancel
Showing results for 
Search instead for 
Did you mean: 

How to restart an STM32 timer halfway with a new value?

HKaze.2
Associate

I have a timer which at each interrupt first stops itself, and then set its own next delay:

HAL_TIM_Base_Stop_IT(&htim);
// do stuff
__HAL_TIM_SET_PRESCALER(&htim, new_p);
__HAL_TIM_SET_AUTORELOAD(&htim, new_a);
__HAL_TIM_CLEAR_FLAG(&htim, TIM_FLAG_UPDATE);
HAL_TIM_Base_Start_IT(&htim);

everything works well. But if I stop the timer before it overflows and interrupts, and then fill it with new values, it keeps finishing its last delay. For example, I stop it a second into a 15-second delay and fill it with values for 1 second delay, after I start it again, it takes ~13 seconds to interrupt instead of 1 second. How can I restart a timer?

2 REPLIES 2
KnarfB
Principal III

set the counter to 0 too. Depending on your settings on your chip (which one?) you might also need to generate an update event to make the settings effective. If that doesn't help, check the reference manual.

hth

KnarfB

Agree fully with KnarfB, just a remark: if you generate Update event by setting TIMx_EGR.UG, it simultaneously sets the counter to 0 in hardware, so setting setting TIMx_EGR.UG is the only thing you need to do.

Don't forget, that it of course also triggers the Update interrupt, if you have it enabled.

JW