Skip to main content
HKaze.2
Visitor II
February 17, 2023
Question

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

  • February 17, 2023
  • 2 replies
  • 2019 views

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?

This topic has been closed for replies.

2 replies

KnarfB
Super User
February 17, 2023

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

waclawek.jan
Super User
February 17, 2023

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