0
\$\begingroup\$

I want to choose a PWM frequency using the formula from this article: How to use fast PWM (Pulse Width Modulation) Mode of AVR microcontroller Timer.

The formula is:

\$f_{\small PWM} = {\Large{f_{crystal}} \over {\large prescaler \times 510}}\$

Where does the number 510 come from?

\$\endgroup\$
2
  • \$\begingroup\$ Most likely that is the maximum value of the PWM framing counter and thus sets the overall period of the output. But questions on stack exchange sites are required to stand on their own; they must not depend on external links for critical detail. \$\endgroup\$ Nov 12, 2020 at 19:14
  • \$\begingroup\$ At present, you don't have much of an answerable question. To get a useful answer and allow the question to survive, you'll need to edit your question to make it independent, to describe your goal (what the PWM signal is for, what frequency you require, and why) and what flexibility constraints you have: can you chose an arbitrary crystal frequency within the range supported by the MCU? Today that would be unusual. \$\endgroup\$ Nov 12, 2020 at 19:27

1 Answer 1

4
\$\begingroup\$

The article is about a timer with an 8-bit counter; if you set the counter to count to its maximum of 255, the PWM frequency you get is:

\$f_{\small PWM} = {\Large{f_{clock}}\over{\large prescaler \times 255}}\$

This is because the counter has to count to 255 for every period of the PWM, so the PWM frequency is \$1 \over 255\$ of the clock frequency (assuming the prescaler is set to 1).

If you want phase-correct aka centre-aligned PWM, the counter will count up to 255 and then down again to 0 for each PWM period, so it will take 2 × 255 = 510 clock ticks for each PWM period, which explains the 510 in the formula you quoted.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ thank you for the answer \$\endgroup\$ Nov 12, 2020 at 23:58
  • \$\begingroup\$ @Nur-Aqmarina: You're welcome. Please accept the answer (if you accept the answer), so the question doesn't remain open. \$\endgroup\$
    – ocrdu
    Nov 13, 2020 at 1:02

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.