I am using STM32F407VGT6
with CubeMX
. So I was beginning with general-purpose timers and I am stuck with prescale and period values.
Basically I want to generate a timer interrupt every n
(where n=1,2,3..) ms and perform some tasks.
There is lot of variations in the formula to calculate the value of period and prescale.
Some versions of formula are:
TIMupdateFreq(HZ) = Clock/((PSC-1)*(Period-1))
Update Event = TIM clk/((PSC+1)*(ARR+1)*(RCR+1))
Prescaler = ((((ClockSpeed) / ((period) / (1 / frequency))) + 0.5) - 1)
So coming to the question, my core clock is running at 168 MHz but I can see the timers are connected to the APB1 bus which is running at 84 MHz.
I have tried code that generates a 1 ms delay (according to the author) and after using that values for prescale and periodm I generated code which also generates a 1 ms delay (by intuition - no scope).
The code uses prescale value of 41999 and a period of 1999.
So,
PSC - 41999
ARR - 1999
Applying this to the second formula
Update Event = TIM clk/((PSC+1)*(ARR+1)*(RCR+1))
Update Event = 84000000/(42000*2000) = 1
(Is this 1 ms delay??)
OK so now I am trying to understand how this PSC = 41999
and Period = 1999
are selected?? Is it purely based on assumption as in whatever formula I use I have to assume one variable?? How to calculate prescale and period if I want say 1.5 or 2.3 or 4.9 something like that precise timing??
Moreover, when I used PSC=41999
and Period =999
, the update event value is 2:
Update Event = 84000000/(42000*1000) = 2
But my delay is twice in every sec. i.e 500 ms.
And when I use PSC=39999
and Period=4199
, the update event value is 0.5:
Update Event = 84000000/(40000*4200) = 0.5
and my delay 2 ms.