cancel
Showing results for 
Search instead for 
Did you mean: 

How to calculate the prescaler of TIM?

Carter Lee
Associate III
Posted on September 28, 2017 at 06:50

HI.

Now I'm trying to implement TIM into the STM32F429i-DISCO kit.

But I need to help how to calculate the prescaler and period value for user?

As I know currently I used 180Mhz as system clock. and I want to set TIM work every 1 's single second.

What am I suppose to do? the below snippet code I found from google.

But I can't understand what does 6000000 mean?

/* Compute the prescaler value */

PrescalerValue = (uint16_t) ((SystemCoreClock / 2) / 6000000) - 1;

/* Time base configuration */

TIM_TimeBaseStructure.TIM_Period = (6000-1); //Autoreload value (ARR) -> 1ms

TIM_TimeBaseStructure.TIM_Prescaler = (6000-1);

TIM_TimeBaseStructure.TIM_ClockDivision = 0;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

I found some explain from google as the below.

TIM_Prescaler = N - 1; Divides the Bus/TIM clock down by N

TIM_Period = N - 1; Divide that clock down by N, ie the *period* is N ticks.

So assume your TIMCLK is ticking at 72 MHz, the *external* clock

If TIM_Prescaler = 71; the timers *internal* clock

will be 1 MHz

If TIM_Period = 999; the 1MHz is divided by a 1000, so

it becomes 1KHz

Your update interrupt with fire every 1ms

Now if you want to play with the channels, say in a PWM mode you can play with the output pins.

If TIM_Pulse = 500; the channel will out a frequency of 1 KHz, with a 50/50 duty cycle. ie TIM_Pulse = N / 2

How to calculate and know ?

If TIM_Prescaler = 71; the timers *internal* clock

 

will be 1 MHz

If TIM_Period = 999; the 1MHz is divided by a 1000, so

 

it becomes 1KHz

How to calculate and know ? 

What if 

If TIM_Prescaler = 63; the timers *internal* clock

 

will be ?? MHz

If TIM_Period = 123; the

it becomes ?? KHz

Would you please help me for understanding  how to calculate ?

When I set with the following setting, I've got the below waveform.

TIM_TimeBaseStructure.TIM_Period = 71;

TIM_TimeBaseStructure.TIM_Prescaler = 999;

TIM_TimeBaseStructure.TIM_ClockDivision = 0;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

/* TIM Interrupts enable */

TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);

/* TIM2 enable counter */

TIM_Cmd(TIM2, ENABLE);0690X00000608QBQAY.png
9 REPLIES 9
Posted on September 28, 2017 at 18:45

Hello!

How to calculate the prescaler of TIM?

In terms of frequency:

HandleTimer.Init.Prescaler = Timer'sClockFrequency/DesiredFrequency -1;

and in terms of period

HandleTimer.Init.Prescaler = Timer'sClockFrequency*DesiredPeriod -1;

Frequency is in Hz  and Period in Seconds.

Example to get a counter period 1 sec (1HZ)

HandleTimer.Init.Prescaler = Timer'sClockFrequency -1;

Example to get a counter frequency 100 Hz (10 msec period)

HandleTimer.Init.Prescaler = Timer'sClockFrequency/100 -1;

Regards

vf

Posted on September 28, 2017 at 19:06

This isn't very complicated.

Update Frequency = (TIM_CLOCK / (Prescaler + 1)) / (Period +1)

The dividers use an N-1 setting, ie N counts from 0 to N-1

The circuitry does a simple comparison with the current value vs N-1, to determine if the NEXT state should be zero.

For an F429 at 180 MHz, TIM on APB2 clock at 180 MHz, on APB1 at 90 MHz

Update Frequency = TIM_CLOCK / (P * Q);

Where Prescaler = Q - 1; and Period = P - 1;

On APB2, TIM_CLOCK = 180 MHz

A prescaler value of 180-1 will clock the main portion of the TIM at 1 MHz

The Prescaler is always 16-bit so has a valid range of 0-65535, giving division from 1 to 65536

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on September 28, 2017 at 22:16

Microchip has something very nice regarding the visual code generator, you can specify the period in milliseconds or microseconds and it shows all the changes, results and generate the right code for you. This should be another improvement/proposal for CubeMX.

0690X00000608NYQAY.png
Posted on September 29, 2017 at 01:06

Is TIM_CLOCK specified in the datasheet, or is something we can pick from the Configuration Clock diagram in CubeMX (the diagram is for an L152RE)?

0690X00000608QpQAI.png

Won't be better to have some combobox controls in the ''Parameter Configuration'' window of the Timer, related to the ''things'' already set in the Configuration Clock diagram and related to some named variables in LL/HAL drivers? I'm thinking in the relation with the other comment I have here. And we must see the resulted tick period displayed, to see if we managed to do it right.

Posted on September 29, 2017 at 01:10

It would be under the Clock Tree Diagram, the source for the specific TIM would depend on the APB the TIM is attached, nominally 2x APB

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Sid
Associate II
Posted on May 02, 2018 at 15:33

Yeah its complicated but i use another tool for setting precise values for the timer, this tool is a free to use Timer calculator for Microchip PIC, ST STM32 and TI Stellaris series uC from Mikro Elektronika. Its called timer calculator and the link is 

https://libstock.mikroe.com/projects/view/398/timer-calculator

 

U can just choose your series of microcontroller, Crystal frequency, choose timer type, enter the time lapse you want for the timer , choose time unit like ns,us,ms or s and click calculate. 

The calculator will give u values for PSC or Prescaler and ARR or AutoReload Register. Just put these values in CubeMx or directly in your code and have fun... focus on the actual programming task and not on these trivial issues.! 

Cheers!

henry.dick
Senior II
Posted on May 04, 2018 at 16:23

You have to figure out first the largest numbers your Prescaler and counter can hold. For really long delays and really high system clocks, you may need a counter in the timer isr to help extend the counting.

Lets say that your clock is 200Mhz, and your Prescaler is 16 bit and your counter is 16bit as well. They hold a maximum of 64k x 64k ticks, far exceed 200M. So you don't need an isr counter here. 

Just pick two numbers whose multiple gives you 200M. 40k x 5k would work for example.

Posted on May 04, 2018 at 16:13

Yeah, it becomes trivial when you have such a tool. But there is one big problem... is windows only and there are many developers on Mac and Linux. It should have been written in Lazarus + FreePascal to get a functional graphical interface in Linux - it is instead, developed in Delphi.

JHelm
Associate

There are many combinations for the parameters you've chosen:

    PSC  ARR     F     ERROR EXACT

0   2879 62499 1.000000 0.0000000000  YES

0   2999 59999 1.000000 0.0000000000  YES

0   3124 57599 1.000000 0.0000000000  YES

0   3199 56249 1.000000 0.0000000000  YES

0   3599 49999 1.000000 0.0000000000  YES

0   3749 47999 1.000000 0.0000000000  YES

0   3839 46874 1.000000 0.0000000000  YES

0   3999 44999 1.000000 0.0000000000  YES

0   4499 39999 1.000000 0.0000000000  YES

0   4799 37499 1.000000 0.0000000000  YES

0   4999 35999 1.000000 0.0000000000  YES

0   5624 31999 1.000000 0.0000000000  YES

0   5759 31249 1.000000 0.0000000000  YES

0   5999 29999 1.000000 0.0000000000  YES

0   6249 28799 1.000000 0.0000000000  YES

0   6399 28124 1.000000 0.0000000000  YES

0   7199 24999 1.000000 0.0000000000  YES

0   7499 23999 1.000000 0.0000000000  YES

0   7999 22499 1.000000 0.0000000000  YES

0   8999 19999 1.000000 0.0000000000  YES

0   9374 19199 1.000000 0.0000000000  YES

0   9599 18749 1.000000 0.0000000000  YES

0   9999 17999 1.000000 0.0000000000  YES

How did I come up with these many working combinations? I wrote a simple python program to compute exact (and even inexact, within a tolerance you specify) combinations. See my answer on StackOverflow:

https://stackoverflow.com/questions/51906691/stm32f4-timers-calculation-of-period-and-prescale-and-generating-1-ms-delay/53548161#53548161