2. PWM
• PWM is the abbreviation of Pulse Width
Modulation.
• It is used to generate a pulse train with varying
duty cycle.
• In the AVR, the timer/counter are used to
generate PWM signals.
• To change the PWM base frequency, the timer
clock frequency and top counter value is
changed. Faster clock and lower top value will
increase the PWM base frequency or timer
overflow frequency.
3.
4. PWM MODE in AVR.
FAST PWM MODE.
• The fast pulse width
modulation or fast PWM
mode (WGM01:0=3)
provides a high frequency.
• PWM waveform generation
option.
• The fast PWM differs from
the other PWM option by
its signal-slop operation.
• The counter counts from
BOTTOM to MAX then
restarts from BOOTM.
PHASE CORRECT PWM MODE.
• The phase correct PWM
mode (WGM01:0=1) provides
a high resolution phase
correct PWM.
• Waveform generation option.
• The phase correct PWM
mode is based on a dual
slope operation.
• The counter counts
repeatedly from BOTTOM to
MAX and from MAX to
BOTTOM.
5. FAST PWM MODE.
• In the Fast PWM, the counter counts like it
does in the Normal mode. After the timer is
started, it starts to count up. It counts up until
it reaches its limit of 0xFF. When it rolls over
from 0xFF to 00, it sets HIGH the TOV0 flag.
6.
7.
8. Frequency of the generated wave in Fast PWM mode.
• In Fast PWM mode, the timer counts from 0 to top and then rolls
over. So frequency of generated wave is 1/256 of the frequency of
timer clock.
• N = determined by the prescaler.
9. Duty cycle of the generated wave in Fast PWM mode.
Non-Inverted: Inverted:
10. Example.
• Assuming XTAL=8MHz , using non-inverted mode, write a
program that generates a wave with frequency of 31,250 Hz
and duty cycle of 75%.
• Solution:
11. PHASE CORRECT PWM MODE.
• In Phase correct PWM, the TCNT0 goes up-
down. First it counts up until it reaches the
top value. Then it counts down until it reaches
zero. The TOV0 flag is set whenever it reaches
zero.
12.
13.
14. Frequency of the generated wave in
Phase Correct PWM mode.
• For Phase Correct PWM mode the frequency of generated wave
is 1/510 of the frequency of timer clock.
• The frequency generated in phase correct pwm mode is half of
the frequency generated in fast pwm mode.
15. Duty cycle of the generated wave in
Phase Correct PWM mode.
Non-Inverted mode: Inverted mode:
16. Example:
• Assuming XTAL=8MHz , using non-inverted mode, write a
program that generates a wave with frequency of 15,686 Hz
and duty cycle of 75%.
• Solution: