cancel
Showing results for 
Search instead for 
Did you mean: 

TIM2 exact timebase settings

FF1
Associate III

Dear all,

I'm using the STEVAL-PCC009V2 based on a STM32F103RBT6 microcontroller, set to work with an internal clock frequency equal to 72 MHz so CK_INT = 72 MHz (refer to figure 100 of RM0008 Rev. 19, pag. 367/1132).

I need to obtain a 10 ms time base, so I'm using the TIM2 as a simple counter with overflow.

Here below my Timer code:

void TIM4_TimeBase_Encoder_Init(void)
{
  TIM_TimeBaseInitTypeDef       TIM_TimeBaseInitStructure;
  
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4 , ENABLE);
 
  // TIM4 configuration 
  TIM_TimeBaseInitStructure.TIM_Period = (100-1);
  TIM_TimeBaseInitStructure.TIM_Prescaler = (7200-1);
  TIM_TimeBaseInitStructure.TIM_ClockDivision = 0x0;    
  TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;  
  TIM_TimeBaseInit(TIM4, &TIM_TimeBaseInitStructure);
 
  // Clear TIM4 update pending flags 
  TIM_ClearFlag(TIM4, TIM_FLAG_Update);
 
  //TIM enable counter 
  TIM_Cmd(TIM4, ENABLE);	
 
  //Enable TIM4 Update interrupts 
  TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
 
  // Enable the TIM4 global Interrupt 
  NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}

To have a better estimation of the period I've to subctract 1 to the PERIOD and the PSC value.

What I need to understand is what should be the exact relationship to use in order to give the theoretical exact estimation of the final period of the timer, basically the time should be equal to (from direct inspection of figure 100 inside the RM0008):

Tcounter = (PRESCALER * PERIOD)/Fclock_psc

in my case I've Fclock_psc=72 MHz then to achieve a 10 ms interrupt I've to use:

10 ms = (PRESCALER * PERIOD)/72MHz -> PRESCALER * PERIOD = 720000

then theoretically setting PRESCALER = 7200 and PERIOD = 100 will give the expected time base but the best result is by subtracting 1 to each one.

May be the reason is because 0 is a special value so into the real computation the value are increased by one unit so in order to achieve the correct result I've to use the theoretical value minus 1 (using theoretical values practically give me a measurement of 20.1 ms instead of 20 ms by using the reduced ones by 1 unit)?

Thank to help me in better understanding!

Best regards

Fabio

PS Into the attached image I'm toggling one output pin every TIM2 interrupt so the real timebase is the half of the measured period.

4 REPLIES 4

The comparators on the TIM deal with N cycles (0 .. N-1) by checking the last value in the sequence being N-1, the next cycle goes to ZERO and is referred to as the UPDATE event. So DIV100 is written as 99

Generally I would recommend having the PRESCALER being the smallest value, it can be ZERO as this is the DIV1 case. The PERIOD can't be ZERO, and anything below 3 is probably undesirable.

The TIM clock depends on the APB clocks, if you're getting low values check the settings for the AHB and APB clocks. Have the code print out what it sees.

Toggling a GPIO will appear to half frequency.

*Editor not allowing UP/DOWN cursor, first Answer to post, not a re-edit.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Thenk you @Community member​ ,

yes the frequency is half, so the real timebase is 10 ms as requested.

Thank for the explanation, make sense, also, through the RM0008 reading, page 419/1132, concerning the Prescaler Value I can see a formula:

f_clock_cnt = f_clock_psc / (PRESCALER + 1)

so due to the N cycles for the PERIOD as pointed by you the formula give:

T_counter = T_clock_psc*(PERIOD+1)*(PRESCALER+1)

So for 10 ms timebase and f_clock_psc=72 MHz we have:

10ms = (1/72MHz)*(PERIOD+1)*(PRESCALER+1)

720000 = (PERIOD+1)*(PRESCALER+1)

hence we can write:

7200 = PRESCALER+1 -> PRESCALER = 7200 -1

100 = PERIOD+1 -> PERIOD = 100-1

Could you please also let me know a little more about your tip:

"Generally I would recommend having the PRESCALER being the smallest value"

Thank!

Best regards

Fabio

>>Could you please also let me know a little more about your tip

You're factoring a pair of numbers, if the prescaler is small it provides you with finer control/granularity to control the period, or more usually the pulse parameter when PWM or duty is important.

With a period of 100 (99) you can't do 12.3% or 12.34% duty

Now while your end system might not be able to resolve that, my general rule is to throw out precision as the last thing I do, rather than the first. Errors compound like interest on a debt..

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
FF1
Associate III

Thank you @Community member​ ,

I've got the point!

Best regards

Fabio