cancel
Showing results for 
Search instead for 
Did you mean: 

Timer question

oscar
Associate
Posted on February 05, 2010 at 12:19

Timer question

8 REPLIES 8
brazov22
Associate II
Posted on May 17, 2011 at 15:07

Hi,

Default system clock is HSI/8 (2 MHz), so it'd be:

TIM2_TimeBaseInit( TIM2_PRESCALER_512, 156); (an Interrupt every 40ms)

brazov2

jeffrey23
Associate II
Posted on May 17, 2011 at 15:07

I tried to calculate it as below:

   

// fCK_CNT = fCK_PSC / (2 ^ PSC[2:0]), where fCK_PSC = fMASTER

// TIMER4 Period = fMASTER / TIM4_PRESCALER / TIM4_PERIOD

//               = 2 MHz / 512 / 156 = 25.04 Hz

    // Set the Prescaler bValue

    TIM4->PSCR = TIM4_PRESCALER_512;

    // Set the Autoreload bValue

    TIM4->ARR = 155;            // 155 or 156 ?

What's correct ARR value ? 155 or 156 ?

mozra272
Associate II
Posted on May 17, 2011 at 15:07

Hi,

You need an Interrupt every 40ms

2 MHz / 512 / 157 = 24.88 Hz ==>40,19ms ==> error 0,479%

2 MHz / 512 / 156 = 25.04 Hz ==>39,93ms ==> error 0,175%

2 MHz / 512 / 155 = 25.20 Hz ==>39,68ms ==> error 0,8%

the correct autoreload value is 156

Regards

mozra

jeffrey23
Associate II
Posted on May 17, 2011 at 15:07

Hi,

TIMER4 Period = fMASTER / TIM4_PRESCALER / TIM4_PERIOD

= fMASTER / TIM4_PRESCALER / (ARR+1)

Is the formula correct ?

Is TIM4_PERIOD equal to (ARR+1) ?

// Set the Autoreload bValue

    TIM4->ARR = 155;            // 155 !

mozra272
Associate II
Posted on May 17, 2011 at 15:07

Hi,

your formul is incorrect and the correct one:

TIMER4 Period = fMASTER / TIM4_PRESCALER / TIM4_PERIOD

= fMASTER / TIM4_PRESCALER / TIM4->ARR

only when using TIMER1 you need to add +1 to the prescaler register to calculate the counter frequency (fCK_CNT is equal to fCK_PSC / (PSCR[15:0]+1).

// Set the Autoreload bValue

    TIM4->ARR = 156;            // 156 !

Regards

mozra

jeffrey23
Associate II
Posted on May 17, 2011 at 15:07

Hi Mozra27,

How about the TIMER2 ? Is it similar to TIMER4 ?

TIMER2 Period = fMASTER / TIM2_PRESCALER / TIM2_PERIOD

                          = fMASTER / TIM2_PRESCALER / TIM2->ARR

Is the formula correct ?

jeffrey23
Associate II
Posted on May 17, 2011 at 15:07

Who knows why only TIMER1 needs to add +1 to the prescaler ?

Olga DIAMANTE
Associate II
Posted on May 17, 2011 at 15:07

Timer 1 has a linear prescaler (any value from 1 to 

65536)

, whilst the other timers have power of 2 prescaler (2^n with

n=[0..15] for 16 bit timers)

, thus formulas are different...