Skip to main content
Visitor II
June 12, 2023
Question

Why does Timer1 seem to have a slower clock?

  • June 12, 2023
  • 1 reply
  • 946 views

I have a common function that I use to set up Timer 5 (I'm using it for PWM output, if it matters):

timerCounts = CLK_GetClockFreq() / pow(2, TIM5_PRESALER_xx) / pwmFrequency;

TIM5_TimeBaseInit(TIM5_PRESCALER_xx, timerCounts);

And that works really well. I'm getting around +/- 0.4% error on that clock frequency. But if I try a similar approach in Timer 1, the end frequency is around 12-14% too low. If I command 1000 Hz, I get somewhere around 880Hz instead. Right now, I've got a fudge-factor of 50/57 which makes it accurate enough for a couple orders of magnitude, but I'm really curious why I need it, and if I can use information available to accurately calculate that fudge-factor so the code is more readable. For reference, here's what I'm doing for Timer 1:

timerCounts = ((CLK_GetClockFreq() * 50 / 57) / timerPrescaler) / pwmFrequency;

TIM1_TimeBaseInit(timerPrescaler, TIM1_COUNTERMODE_UP, timerCounts, 0);

    This topic has been closed for replies.

    1 reply

    Visitor II
    June 12, 2023

    I guess you know that the pre-scaler value is one less than the division, i.e. if you set the pre-scale to 15, the incoming clock will be divided by 16