Skip to main content
Visitor II
February 5, 2010
Question

Timer question

  • February 5, 2010
  • 8 replies
  • 1448 views
Posted on February 05, 2010 at 12:19

Timer question

    This topic has been closed for replies.

    8 replies

    Visitor II
    May 17, 2011
    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

    Visitor II
    May 17, 2011
    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 ?

    Visitor II
    May 17, 2011
    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

    Visitor II
    May 17, 2011
    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 !

    Visitor II
    May 17, 2011
    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

    Visitor II
    May 17, 2011
    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 ?

    Visitor II
    May 17, 2011
    Posted on May 17, 2011 at 15:07

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

    Visitor II
    May 17, 2011
    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...