cancel
Showing results for 
Search instead for 
Did you mean: 

What is the best way to execute an event every constant time interval?

Anh Tran
Associate II

Hi,

My board is STM32F411RE Nucleo.

Currently, I'm using a timer (timer 2, channel 3 to be exact) in output compare (no output), using TIM_OCMODE_TIMING mode, interrupt enable.

My understanding is the timer will tick until it is overflowed (in this case, the overflow value is 2^32-1, as TIM2 is 32 bit) , and an event will trigger when the timer tick pass the period value. And since this period is a set value, the event will only trigger once.

So if my understanding is correct, in order to achieve what I want, I should also increment the Period to a new (that is multiple of the original value) every time an event is triggered.

Is this logic correct, and is there a better way to do this?

3 REPLIES 3
turboscrew
Senior III

The timer ticks from zero to TIMx_CNT and starts over - or from TIMx_CNT to zero if downcounting.

The ticking speed can be adjusted with TIMx_PSC.

The timer can be set to interrupt when the counting is done and reload takes place (UG).

You don't need channels for that.

You CAN set the timer to one shot mode too, Then after UG, counting stops.

Sorry I used the HAL generated by CubeMX so I didn't actually set any register (maybe I did but through CubeMX's marco). But I'm guessing that those are the values that can be config through Prescaler and Period.

If that's the case then I wouldn't want to change those value since the said timer is also used for other timing related stuffs (measuring the frequency of some signals).

S.Ma
Principal

It's easier to answer with better answer if we know what you want to actually do in the end, and what latency/precision you want.

If you just want minimum delay time (like 50+ msec, which is slow respective to core frequency), then just hack 1msec Systick (which is a timer overflow interrupt event), and use a RAM 32 bit counter. In the interrupt, when this SW counter reaches multiple of 50, set a RAM flag to tell the main loop 50msec elapsed and it's time to scan buttons, etc...