cancel
Showing results for 
Search instead for 
Did you mean: 

Keeping 2 Timer Interrupts with different Frequencies in Sync

LMorr.3
Senior II

Hello,

I have two 32bit timers running on an STM32F373, TIM2 and TIM5.  

I have successfully chosen TIM2's prescale and ARR values to generate the 'update' interrupt as I require by using TIM2's prescale set to 0 and modifying the ARR to set  a target frequency.  TIM2 is working as I need.

I now need to have TIM5 run its interrupt the same way, but 24 times faster than TIM2.  If I change TIM2's ARR value, I plan on setting TIM5's ARR to TIM2->ARR/24.  

My question is; Over time, I see TIM5 will become slightly out of synch with TIM2 due to rounding.  Is it ok to adjust TIM5->CNT each time TIM2's interrupt occurs to ensure they are in sync?  Is there a better way to do this?  I was looking at cascading timers but maybe that is too complex compared to just setting TIM5->CNt on each TIM2 interrupt.

UPDATE:  I think I have this figured out.  I'll re-configure my setup a bit to make use of only 1 TIM running at 24x with a cnt variable to trigger a function instead of a second timer's interrupt. 

Thank you!!

4 REPLIES 4
Sarra.S
ST Employee

Hello @LMorr.3 

Any updates on this topic? 

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Hello,

I had added an 'update' note to my original post, so I'll all good now, thank you!!

KnarfB
Principal III

Anyway. Note, that to divide a timer clock by x, you have to set ARR to x-1, not x. This might have caused the error you have observed.

hth

KnarfB

MasterT
Lead

I've solved similar task before, instead of divide by /24 you need multiply x24.  First set ARR value of the tim-5 -> fast, than ARR2 =  (ARR5 +1) x 24 - 1.