PWM Flavor

Arduinos have two kinds of pulse width modulation: fast and phase-correct. Which is important to you?

Can you explain the difference between the two? What is the benefit of using phase correct PWM? Thanks

That's a very good question. Unfortunately, I'm not going to be much help answering it. My only experience with PWM is dimming LEDs.

What I've read and a little bit I know...

  • Fast is good for power regulation. I assume this would be for something like a "switching power supply". A quote from the '85 datasheet: "This high frequency makes the fast PWM mode well suited for power regulation, rectification, and DAC applications. High frequency allows physically small sized external components (coils, capacitors), and therefore reduces total system cost."

  • Phase-correct works well for motor control. A quote from the '85 datasheet: "However, due to the symmetric feature of the dual-slope PWM modes, these modes are preferred for motor control applications."

  • The maximum phase-correct frequency is half the maximum fast frequency.

  • I believe the frequency for fast PWM on a 16 MHz Arduino is 16000000 / (64 * 256) = ~976 Hz. The frequency is half that for 8 MHz Arduinos.

  • I believe the frequency for phase-correct PWM on a 16 MHz Arduino is 16000000 / (64 * 510) = ~490 Hz. The frequnecy is half that for 8 MHz Arduinos.

Hopefully, someone else can provide something more informative.

This helped me understand the difference...
http://www.societyofrobots.com/member_tutorials/node/230

I use whatever suits my application, however I do find that phase-correct looks nicer on the logic analyzer I use to debug

I have never understood how the difference would be relevant, especially at the relatively low speed used for PWM outputs on Arduino...

My motivation for asking is the ATtiny85 processor. It has two timers (0 and 1) each with two PWM channels (A and B). One channel from each timer is connected to a single pin (PB1 is connected to Timer 0 Channel B and Timer 1 Channel A). I'm in the process of filling-out the '85 core files and I need to decide if this pin should be fast or phase-correct.

I believe that on a standard arduino, four of the PWM pins run phase-correct, and two run "fast" (because the timer is shared with the 1ms interrupt.) Most of the time, no one notices...

The ATMega328 datasheet will explain the differences in these two flavors of PWM quite well.

But basically fast PWM uses the counter in a "count up and reset" mode while phase correct mode uses the counter in a "count up and then back down mode".

The result is that in fast mode the PWM duty cycle changes the mid-point of the signal based on its width. Thus the phase of the signal varies.

In phase correct mode the center of the on-time of the PWM signal is always centered around the "top" point of the count because the counter counts up and then back down to zero. Thus the on time is always centered around the max count i.e. the phase doesn't change with duty cycle.

There are two other results. One is that the fast mode is twice the frequency of the phase correct mode. The other result is that the fast mode can never turn fully off. The count compare always matches for at least one count when the duty cycle is commanded to zero. This makes it problematic for motor control.

From what I've seen the counters use phase-correct mode for the AnalogWrite() command in the 328-based and I assume 1280/2560-based Arduinos. I haven't measured them all but the basic frequency is 490Hz for Timer1 and Timer2 at least.

/me

@RobotPower: Thank you for the post.

From what I've seen the counters use phase-correct mode for the AnalogWrite() command in the 328-based and I assume 1280/2560-based Arduinos.

The two pins connected to Timer 0 (the millis timer) are fast PWM. All the others are phase-correct.

Did you vote? Which flavor is more important to you?

The result is that in fast mode the PWM duty cycle changes the mid-point of the signal based on its width. Thus the phase of the signal varies.

Excellent explanation! I had read (well, "looked at") the datasheet and understood the count up vs up&down thing, but I never understood why it was relevant, or how the latter was better for motors...