Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 17

Intermediate Embedded Systems

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

While the microcontroller is a relentlessly


digital device, it needs to be able to vary R
continuously the outputs it controls. One
means of doing this is by PWM. When D
applied to an inductive load, this depends
L
upon the fundamental equation of
inductance:
V = Ldi/dt
I

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

The 16F873A has two identical PWM


sources, which form part of its CCP
(Capture, Compare, PWM) modules. These
modules interact with its Timers, giving the
Timers extended capability. The PWM
outputs are on bits 1 and 2 of Port C.
The 16F873A PWM Source 2

This register acts as buffer.


Programmer writes required
pulse width here.

Receives value from buffer.


Determines PWM pulse
width.

Timer 2, free-running,
connected to Fosc/4 via its
prescaler.

Sets the PWM Period, for


both modules.
The 16F873A CCP1CON, CCP2CON Registers (repeat from earlier slide)

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

This timing diagram illustrates the action of the PWM source,


both in steady state, and as the pulse width is changed.

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

Timer 2 Value CCPR1L

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:

ton = (pulse width register) × (PWM timer input clock period),

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,

ton = (pulse width register) × {Tosc × (Timer 2 prescale value)}


DC and Stepper Motors

The principles of DC and


Stepper motors are
outside the scope of this
course. For a useful
reference see bottom
right. For our purposes,
we simply view their
windings as inductive
loads, and control them
accordingly.

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

Inductors, including motors, relays, path of decaying


solenoids etc, store energy when current current when transistor
is switched off. R
is flowing in them, and this energy must p

be returned to the circuit in a controlled D


manner when the current is switched L
off. Otherwise arcing will occur. For
“freewheeling” diode
DC switching this is normally done
with a “free-wheeling” diode. I

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

Reversible Switching with


Unipolar Supply –
( H-Bridge with PWM)
Level-Shifting Level-Shifting
Circuit Circuit

I LOAD

PWM Signal

PWM Signal PWM Signal

I
t
The L293D Dual H-Bridge

VOS Out 1 Out 3 VLS


V V
8 os 3 11 os 16

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

Operating Conditions - Highlights


600mA Output Current per channel 1.2A Peak Output Current (non repetitive) per channel
Over temperature protection High noise immunity (Logic 0 input voltage to 1.5V)
The L293D Applied in the Derbot Motor Drive Circuit

Port A bit 5 Port A bit 2


CCP2 (Port C, bit 1) Port A, bit 5

+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

Filtering a PWM Stream to Produce an Analog Voltage


A Sin Wave Generated with PWM

a) The Output Sin Wave b) Lower: the PWM Stream


Upper: Detail of Analog Output

End of Lecture Note

You might also like