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.
-
1\$\begingroup\$ The internal RC oscillator (HSI) may not be that stable or accurate. Use HSE, the external crystal oscillator instead \$\endgroup\$user319836– user3198362023年02月16日 03:48:34 +00:00Commented Feb 16, 2023 at 3:48
-
\$\begingroup\$ Thank you so much @RussellH. The external crystal oscillator works way better. \$\endgroup\$C_Saetern– C_Saetern2023年02月16日 17:17:54 +00:00Commented Feb 16, 2023 at 17:17
1 Answer 1
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.
-
\$\begingroup\$ Thank you so much. That actually makes so much sense. I totally overlooked my clock configuration. \$\endgroup\$C_Saetern– C_Saetern2023年02月16日 17:15:55 +00:00Commented Feb 16, 2023 at 17:15
Explore related questions
See similar questions with these tags.