Pulse Width Modulation (PWM)
ECET 209 – Lecture 19
Introduction to Microcontrollers
What is Pulse Width Modulation?
Pulse Width Modulation
D
F
ECET 209 Purdue University 2
Where is PWM Used?
• Motor Controllers
– Speed
• Light Dimmers
– Brightness
• Switching Power Supplies
– Output Voltage
• Audio Amplifiers
ECET 209 Purdue University 3
Methods to Produce PWM
• The Microcontroller is capable of producing
PWM is several ways
– Sequential Flow
– Timer Interrupts
– Auto PWM Generation
ECET 209 Purdue University 4
Sequential Flow
#include <delay.h>
void main (void)
{
DDRA = 0xFF;
while(1)
{
PORTA.0 = 1;
delay_ms(5);
PORTA.0 = 1;
delay_ms(5);
}
}
ECET 209 Purdue University 5
Automatic PWM
• The Microcontroller has special hardware
that will automatically create a PWM output
(no software required)
ECET 209 Purdue University 6
Output Compare Register
Pulse Width Modulation with Timer0
255
OCR0
ECET 209 Purdue University 7
Output Compare Register
Pulse Width Modulation with Timer0
255
OCR0
0
D
F
ECET 209 Purdue University 8
Automatic PWM
ECET 209 Purdue University 9
Output Compare Register
TCNT0 OCR0
Waveform Generator
ECET 209 Purdue University 10
Automatic PWM
• Several Different Modes of PWM
– Clear Timer on Compare Match
– Toggle on Compare Match
– Fast PWM
– Phase Correct PWM
ECET 209 Purdue University 11
Clear Timer on Compare Match
Clear Timer on Compare Match
255
OCR0
ECET 209 Purdue University 12
Clear Timer and Toggle on
Compare Match
Clear Timer on Compare Match with Toggle
255
OCR0
OC0
ECET 209 Purdue University 13
Fast PWM
Fast PWM
255
OCR0
OC0
ECET 209 Purdue University 14
Phase Correct PWM
Pulse Width Modulation with Timer0
255
OCR0
OC0
ECET 209 Purdue University 15
Fast PWM
Fast PWM
255
OCR0
OC0
ECET 209 Purdue University 16
Timer Counter Control Register
ECET 209 Purdue University 17
Timer Counter Control Register
ECET 209 Purdue University 18
Timer Counter Control Register
ECET 209 Purdue University 19