2
\$\begingroup\$

I have a PWM pin that is configured to be at 40Khz but I am seeing it bounce around in my oscilloscope. It would read 40.045KHz and bounce ~ +/- 4Hz or so.

I have another module that is using Atmel MCU that is consistently(No bouncing/jittering) outputting PWM at 39.999Khz so I feel like the oscilloscope i'm using is not the problem.

I am Using internal clock(HSI) at 16MHz
Prescaler = 0
ARR = 399
TIM16->CC1 = 200 // ~50% duty cycle

Is there a way to make the PWM output frequency to be consistent?

Any help/feedback is appreciated.

enter image description here

\$\endgroup\$
2
  • 1
    \$\begingroup\$ The internal RC oscillator (HSI) may not be that stable or accurate. Use HSE, the external crystal oscillator instead \$\endgroup\$
    – RussellH
    Feb 16, 2023 at 3:48
  • \$\begingroup\$ Thank you so much @RussellH. The external crystal oscillator works way better. \$\endgroup\$
    – C_Saetern
    Feb 16, 2023 at 17:17

1 Answer 1

2
\$\begingroup\$

The datasheet defines HSI frequency in range 15.88-16.08MHz, or 0.5-0.75%. On top of that - temperature drift can be up to 2%. Observed error is about 0.1%, which is quite good. Keep in mind that under a different conditions the error might get much worse.

To minimize frequency error, you can use an external crystal oscillator, as RussellH proposed.

If the design can tolerate frequency errors about 0.3% and there is some external reference frequency, you can try to utilize HSI frequency trimming function. But single step here is about 0.3%, thus HSI trimming can help to keep frequency error within this step, but not better.

enter image description here

\$\endgroup\$
1
  • \$\begingroup\$ Thank you so much. That actually makes so much sense. I totally overlooked my clock configuration. \$\endgroup\$
    – C_Saetern
    Feb 16, 2023 at 17:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.