AnsweredAssumed Answered

How to calculate the prescaler of TIM?

Question asked by Carter Lee on Sep 28, 2017
Latest reply on May 4, 2018 by dhenry


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 */