Professional Documents
Culture Documents
PWM
PWM
Aug 2008
Lecture 7
Pulse Width Modulation
The aims of this lecture are:
• to introduce the concept of Pulse Width Modulation,
• to explore the application of PWM to motor control,
• to explore the PWM source on the 16F873A,
• to explore the H-bridge, as a means of interfacing PWM to an inductive load.
For further
information
and
background,
read
Pages 218-220,
237-252.
The copyright to all diagrams is held by Microchip
Technology, or T. Wilmshurst, unless otherwise stated
The Principles of PWM 1 S
VS
open
switchVG
t t t
VS/R
IL
t t t
a) Time Constant small compared to “on” time
b) Time Constant large compared to “on” time, narrow pulse
c) Time Constant large compared to “on” time, wide pulse
The principles of PWM 2
V
"freewheeling" diode S
L (with resistance R)
decaying current flows
in this direction, when IL
switched off.
A Practical
V Implementation
G
logic gate,
or port bit
output
t
on
Closed In general:
Switch
Open ton VS
T ___ _____
I Iave = x
Iave
T R
The 16F873A
PWM Source 1
Timer 2, free-running,
connected to Fosc/4 via its
prescaler.
This is a comparatively simple SFR. The lower 4 bits set the mode, and the only other two active bits
can be used as the lower 2 bits of the PWM duty cycle.
Example Waveforms for the 16F873A PWM Generator
CCPRIL value
Timer 2 equals Timer 2 and PR2 values changed in program
CCPR1H value, PWM equal, Timer is reset and
output is cleared PWM goes high
CCPRIL value transferred to
CCPRIH as Timer clears
PR2 Value
CCPR1L/H Value
PWM Output
T
ton
The PWM period T is determined by the interaction of the PR2 register and the 8 bits of Timer 2. It may
be calculated as follows:
T = (PR2 + 1) × (Timer 2 input clock period)
= (PR2 + 1) × {Tosc × 4 × (Timer 2 prescale value)}
The PWM pulse width ton is determined by the interaction of the extended CCPR1H register (all 10 bits
of it) and the extended (10-bit) Timer 2. It may be calculated as follows:
where ‘PWM timer input clock period’ is the period of the clock input to the extended Timer 2 and ‘pulse
width register’ is the value in the extended CCPR1H register. Hence,
Chapter 9 of An
Introduction to the Design
of Small-Scale Embedded
Systems, Palgrave, by Tim
Wilmshurst, covers DC and
stepper motors at an
appropriate level of detail
for embedded systems.
On-Off Switching of Inductive Loads V S1
Vi
Vi
0V
I
The Principle of the H-Bridge
VS
A B
"high side"
Input X Input Y
Load
"low side"
Continuously Variable VS
I LOAD
PWM Signal
I
t
The L293D Dual H-Bridge
2 10
IN 1 1 3 IN 3
Enable 1 1 9 Enable 2
Vos V
os
7 15 IN 4
IN 2 2 4
6 14 4,5,12,13
Out 2 Out 4 0V
+9V +5V
Port C bit 1 VLS Vos Out 1
16 8 Vos 3
11
Port C bit 2
En1 VLS
Left 2
1
1/6 'HC14 In1 In3
Motor IN 1
Out1 Out3 9
10
0V 0V
0V 0V 1
Motor
Enable 1 Vos
Out2 Out4 Right 1/6 'HC14
In2 In4 Motor
VOS En2 8 7
2
IN 2
L293D IL
Out 2
1/2 L293D
PWM
Drive
positive
IL 0
negative
Some PWM Motor Drive Subroutines for the 16F873A (Part of Program Example 9.2)
;set up PWM
movlw B'00000100' ;switch on Timer2, no pre or postscale
movwf t2con
movlw B'00001100' ;enable PWM
movwf ccp1con
movwf ccp2con
movlw 0f9 ;249 decimal
movwf pr2
...
(main program omitted – appears as Program Example 8.4)
...
;motor drive subroutines
leftmot_fwd ;sets left motor running forward
bsf porta,mot_en_left Increase this number (max. 255)
movlw D'176'
for motor to run faster forward.
movwf CCPR2L
return
rtmot_fwd bsf porta,mot_en_rt ;sets right motor running forward
movlw D'176'
movwf CCPR1L
return
leftmot_rev bsf porta,mot_en_left ;sets left motor running backwards
movlw D'80' Decrease this number (min. 0) for
movwf CCPR2L
return
motor to run faster backwards.
rtmot_rev bsf porta,mot_en_rt ;sets right motor running backwards
movlw D'80'
movwf CCPR1L
return
PWM used for Digital to Analog Conversion
Place modulation
R G
frequency in pass-band Cut-off frequency
Place PWM
C frequency deep
in stop-band