Go Down

### Topic: PWM Flavor (Read 2848 times)previous topic - next topic

##### Nov 14, 2010, 09:05 am

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

#### brs0906

#1
##### Nov 14, 2010, 10:43 pm
Can you explain the difference between the two? What is the benefit of using phase correct PWM? Thanks

#2
##### Nov 15, 2010, 03:50 amLast Edit: Nov 15, 2010, 04:10 am by bcook Reason: 1

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.

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

#### frank26080115

#3
##### Nov 15, 2010, 07:16 am
I use whatever suits my application, however I do find that phase-correct looks nicer on the logic analyzer I use to debug

#### westfw

#4
##### Nov 15, 2010, 08:02 am
I have never understood how the difference would be relevant, especially at the relatively low speed used for PWM outputs on Arduino...

#5
##### Nov 15, 2010, 08:18 amLast Edit: Nov 15, 2010, 08:19 am by bcook Reason: 1

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.

#### westfw

#6
##### Nov 16, 2010, 05:45 pm
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...

#### RobotPower

#7
##### Nov 16, 2010, 07:34 pmLast Edit: Nov 16, 2010, 07:38 pm by RobotPower Reason: 1
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

#8
##### Nov 16, 2010, 09:13 pm

@RobotPower: Thank you for the post.

Quote
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?

#### westfw

#9
##### Nov 18, 2010, 12:02 am
Quote
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...

Go Up