2010-02-05 03:19 AM
Timer question
2011-05-17 06:07 AM
Hi,
Default system clock is HSI/8 (2 MHz), so it'd be:
TIM2_TimeBaseInit( TIM2_PRESCALER_512, 156); (an Interrupt every 40ms)
brazov2
2011-05-17 06:07 AM
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 ?2011-05-17 06:07 AM
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 mozra2011-05-17 06:07 AM
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 !2011-05-17 06:07 AM
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 mozra2011-05-17 06:07 AM
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 ?2011-05-17 06:07 AM
Who knows why only TIMER1 needs to add +1 to the prescaler ?
2011-05-17 06:07 AM
Timer 1 has a linear prescaler (any value from 1 to
65536)
, whilst the other timers have power of 2 prescaler (2^n withn=[0..15] for 16 bit timers)
, thus formulas are different...