Aquaticus

Projects for geeks

PWM Modes

11 Aug 2009

AVR timers offers more than one operation mode for PWM. Number of modes depend on timer. Timer1 is the most function rich timer, it offers 13 PWM modes, timer0 and timer1 offer only 3 modes.

General modes:

Phase correct PWM

Use it to control motor speed controllers and servos. If you do not know which mode to use, choose this one. As name says its phase is always correct (see drawings to guess what phase correct means).

Fast PWM

You can guess it is fast. To be more precise its frequency is twice that of the Phase Correct mode but it is not phase correct. Used for digital to analog converters and where phase does not matter but higher frequency is needed.

Phase and Frequency Correct

Unlike the above modes, frequency of the signal can be easily changed. In the fast and phase correct PWM modes frequency is determined by the prescaler value which limits number of possible frequencies to 5. This mode is used to generate sound or to interface any device that is controlled by the variable frequency. Best suited to generate sound. Use when the phase of the signals should not change.

CTC

Similar to Phase and Frequency Correct mode, frequency can be smoothly controlled. The only way to generate variable frequency PWM for timer0 and timer2.

Phase correctness

When duty cycle is changed, phase of the signal may be changed. There are some applications when phase of the signal should not change even when duty cycle is changed (e.g. servo control). Fortunately AVR offers phase correct modes. These modes are:

  • Phase and Frequency Correct
  • Phase Correct

Because of the way phase correct modes generate PWM wave, maximum frequency of the signal is lower (2 times) then in phase incorrect mode.

Here are the two animations showing the difference between phase correct mode and the other mode as duty cycle changes. In the example Fast PWM mode is shown but it is the same for any other phase incorrect mode.

Phase correct mode. Duty cycle does not affect phase.

Phase incorrect PWM wave. Phase is not correct while duty cycle is changed.

Summary

Simplifying things we can say that in Phase Correct and Fast PWM modes ability to change duty cycle is useful. In Phase and Frequency Correct and CTC modes the possibility to change the frequency is the most useful thing.

In addition 16 bit timer1 allows to select 8, 9 or 10 bits accuracy. 8 bits timers (timer0 and timer2) provide always 8 bit PWM.

For full list of modes see Waveform Generation Mode Bit Description table in Atmel documentation (e.g. for ATmega32A timer1 it’s on page 114).

In Atmel documentation one of the timer modes is called Normal. It has nothing to do with PWM (it is not normal PWM mode). In this mode timer operates as an ordinary timer/counter without generating PWM signal. Although normal mode of timer can be used to generate software PWM.