0% found this document useful (0 votes)
33 views39 pages

CH 8 AVR Peripherals - WaveGenPWM ICP

Digital Systems notes on wave generation and PWM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views39 pages

CH 8 AVR Peripherals - WaveGenPWM ICP

Digital Systems notes on wave generation and PWM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

AVR PERIPHERAL FUNCTIONALITY

TIMER APPLICATIONS
INPUT CAPTURE WAVE GENERATION AND PWM

DR T WALINGO

UNIVERSITY OF KWAZULU-NATAL 1 DIGITAL SYSTEM DESIGN SERIES


INTRODUCTION AND CONTENTS
• Timers have been used in previous lectures to generate delays
and count external events.
• Timers can also be used for the following
– Generating different square waves.
– Capturing events.
– Measuring the frequency and duty circles of waves.
– Pulse width modulation.
Contents
• Wave generation using 8 bit timers.
• Wave generation using 16 bit timers.
• Input capture of events.
• Measuring the frequency and duty circle of waves.
• Pulse width modulation.

UNIVERSITY OF KWAZULU-NATAL 2 DIGITAL SYSTEM DESIGN SERIES


THE WAVEFORM GENERATOR
• Each timer has at least one OCRn
register.
• A comparator continuously compares
TCNT0 with the Output Compare
Register (e.g. OCR0). The Output
Compare Flag (OCF0) is set when a
match occurs.
• If enabled (OCIE0 = 1 and Global
Interrupt Flag in SREG is set) and OCF0
is set, an output compare interrupt is
generated.
• When the TCNTn reaches Top or Bottom
or a compare match occurs, the
waveform generator is informed.
• Then, the waveform generator
generates an output on OCn (OC0) pin
according to operating mode set by the
WGM01:0 bits and Compare Output
Mode (COM01:0) bits of the TCCRn.

UNIVERSITY OF KWAZULU-NATAL 3 DIGITAL SYSTEM DESIGN SERIES


TIMER REGISTERS- TCCR0

Selecting the Timer Action


Actionon
onCTC
compare
mode
Mode match

Clock Select Bits

New modes

UNIVERSITY OF KWAZULU-NATAL 4 DIGITAL SYSTEM DESIGN SERIES


EIGHT BIT WAVEFORM GENERATORS- NORMAL, CTC MODES
• When TMR0 is in CTC mode
(WGM01:0=10) or Normal mode
(WGM01:0=00) and a compare match
occurs, depending on the value of
COM01:0 bits the following happens
on pin OC0:
– Remain unaffected.
– Toggle the OC0 pin.
– Clear the OC0 pin. OC0
– Set the OC0 pin. Dual purpose pin

• Same can be said for the other


timers OCx
Dual purpose pin

UNIVERSITY OF KWAZULU-NATAL 5 DIGITAL SYSTEM DESIGN SERIES


CLASS EXERCISE – NORMAL WAVEFORMS
• For a 8Mhz clock, write a program that generate a 15.625Khz square wave on OC0 (PB.3)
pin with TMR0 in normal mode. Ttimerclock = 1/8 MHz=0.125µS
; CODE FOR A
.org 0x00 ;Reset vector
Twave = 1/15,625 HZ = 64 µS
rjmp Init ; Bypass the interrupt vector table
;******************************************
;CONFIGURATION SECTION No.steps = 64/0.125 = 512
Calculate the
.org 0x100 values

Init: sbi DDRB, 3 ;pins set as output Toggle pin after 512/2 = 256 steps

TCNT0

ldi Temp, 100 OXFF

out OCR0, Temp ;OCR0= Delay reached OCR0

ldi Temp, 0x11


out TCCR0, Temp ;Normal mode, No prescaler, 0
Time
OC0
256
1
Start: rjmp Start

Does the value 0 Time


matter

UNIVERSITY OF KWAZULU-NATAL 6 DIGITAL SYSTEM DESIGN SERIES


CLASS EXERCISE – CTC WAVEFORMS
• For a 8Mhz clock, write a program that generate a 19.9Khz square wave on OC0 (PB.3) pin
with TMR0 in CTC mode TCNT0
OXFF
Which method
; CODE FOR B is better
.org 0x00 ;Reset vector OCR0
rjmp Init ; Bypass the interrupt vector table
;******************************************
0
;CONFIGURATION SECTION Calculate the Time
OC0
.org 0x100 values X
1
Init: sbi DDRB, 3 ;pins set as output
0
Time
ldi Temp, 200
out OCR0, Temp ;OCR0= Delay reached
Ttimerclock = 1/8 MHz=0.125µS
ldi Temp, 0x19
out TCCR0, Temp ;CTC mode, No prescaler
Twave = 1/19,9K HZ = 0.05mS

Start: rjmp Start


Twave = 2(X+1)*0.125µS  X=200
How can you increase
the frequency

UNIVERSITY OF KWAZULU-NATAL 7 DIGITAL SYSTEM DESIGN SERIES


ATTEMPT ON YOUR OWN– GENERATING PULSES USING CTC
• Assuming a XTAL=1 MHz, draw the wave generated by the following program.
; ******************************************************
.org 0x00
rjmp Start
;******************************************************
;CONFIGURATION SECTION
.org 0x100
sbi DDRB,3

Start: ldi Temp, 69


out OCR0, Temp
ldi Temp, 0x19
out TCCR0, Temp
;*******************************************************
L1: in Temp, TIFR
sbrs, Temp, OCF0
rjmp L1
ldi Temp, 1<<OCF0
out TIFR, Temp
ldi Temp, 99
out OCR0, Temp
ldi Temp, 0x29
out TCCR0, Temp
L2: in Temp, TIFR
sbrs, Temp, OCF0
rjmp L2
ldi Temp, 1<<OCF0
out TIFR, Temp Check
Check mode
mode and
and Students to attempt
what
what happens
happens to
to the
the the question in class
rjmp Start pin
pin
;*******************************************************

UNIVERSITY OF KWAZULU-NATAL 8 DIGITAL SYSTEM DESIGN SERIES


GENERATING WAVES USING TIMER1 -REGISTERS

UNIVERSITY OF KWAZULU-NATAL 9 DIGITAL SYSTEM DESIGN SERIES


TIMER1 – MODES OF OPERATION
• Timer 1 has 16 different modes of operation defined by WGM13, WGM12, WGM11
and WGM10. They can be categorized into five groups
– Normal mode.
– CTC mode.
– Fast PWM mode.
– Phase correct PWM and
– Phase and Frequency correct PWM
• TOP is the highest value that the TCNT register reaches while counting. For the 16 bit
timer
– Normal mode – Top = 0xFFFF
– In some modes it is defined by the value of the OCR1x
– In other modes it is fixed.
– The values in different modes can be identified by checking in the data sheet.
• Different flags are set when the timer rolls over depending on the TOP value:
– Fixed TOP valueTOV1 flag is set.
– OCR1A defines the TOP value  OCF1 flag will be set
– ICR1 defines TOP value  ICF1 flag will be set.

UNIVERSITY OF KWAZULU-NATAL 10 DIGITAL SYSTEM DESIGN SERIES


MODES OF OPERATION

• Column 6 – Timer counter mode of operation.


• Column 7 – TOP value.
• Column 8 – Discussed in next chapter.

UNIVERSITY OF KWAZULU-NATAL 11 DIGITAL SYSTEM DESIGN SERIES


WAVE GENERATORS WITH TIMER1
• Timer 1 has two independent waveform
generators: Waveform generator A and
waveform generator B.
• The compare match between OCR1A and
TCNT1 affects Waveform Generator A, and
the wave generated by Waveform Generator
A shows up on OC1A pin.
• The compare match between OCR1B and
TCNT1 affects Waveform Generator B, and
the wave generated by Waveform Generator
B shows up on OC1B pin.
• The COM1A1 and COM1A0 bits have control
over Waveform Generator A; whereas
COM1B1 and COM1B0 control Waveform
Generator B. All of the COM bits are in the
TCCCR1A.
• The operation mode of Timer1 (WGM13,
WGM12, WGM11 and WGM10 bits of
TCCR1A and TCCR1B) affect both generators.

UNIVERSITY OF KWAZULU-NATAL 12 DIGITAL SYSTEM DESIGN SERIES


WAVE GENERATION IN THE CTC MODE
• When Timer 1 is in normal or CTC
(WGM13:0 = 0100 or WGM13:0 =
1100) mode after compare match
occurs, the waveform operators
can perform one of the following
action depending on the values
of COM1A1:0 and COM1B1:0 bits
respectively:
OC1B
– Remain unaffected Dual purpose pin
– Toggle the OC1x pin
– Clear the OC1x pin
OC1A
– Set the OC1x pin Dual purpose pin

UNIVERSITY OF KWAZULU-NATAL 13 DIGITAL SYSTEM DESIGN SERIES


GENERATNG SQUARE WAVES -CTC
• Set the Timer to CTC mode
• Set the COM1x1 and COM1x0 bit of the Waveform Generators to
toggle mode.
• The OC1x pin will be toggled on each compare match and a square
wave will be generated.
TCNT1
OXFFFF

OCR1x

Time
OC1x
X
1

Time

UNIVERSITY OF KWAZULU-NATAL 14 DIGITAL SYSTEM DESIGN SERIES


CLASS EXERCISE – GENERATING SQUARE WAVES IN CTC MODE
• Assuming a XTAK=8 MHz, draw and calculate the frequency of the wave generated by the following code snipet.

; ****************************************
.org 0x00
rjmp Init
;*****************************************
;CONFIGURATION SECTION
.org 0x100
Init:
sbi DDRD, 5

ldi Temp, 0x40


out TCCR1A, Temp
ldi Temp, 0x09
out TCCR1B, Temp
ldi Temp, HIGH (512)
out OCR1AH, Temp
ldi Temp, LOW (512)
out OCR1AL, Temp

;*****************************************
Start : rjmp Start Students to attempt
Remember accessing
;***************************************** the question in class
16 bit registers
7.797KHz

UNIVERSITY OF KWAZULU-NATAL 15 DIGITAL SYSTEM DESIGN SERIES


INPUT CAPTURE PROGRAMMING
• The input capture function is widely used in:
– Recording the arrival time of an event.
– Pulse width measurement.
– Period measurement.
• The AVR has an input pin to trigger the input
capture event.
• A signal change at this pin causes the following:
– The timer value to be read and saved in the
Input Capture Register (ICRx).
– The Input Capture Flag (ICFx) in the TIFR will be
set. This is useful to measure the width of
external pulses.
• The input pin is either the Input Capture Pin (ICP1)
or on the Analog Comparator pins
• Analog Comparator compares the input values on
the positive pin AIN0 and negative pin AIN1. When
the voltage on the positive pin AIN0 is higher than
the voltage on the negative pin AIN1, the Analog
Comparator Output, ACO, is set.

UNIVERSITY OF KWAZULU-NATAL 16 DIGITAL SYSTEM DESIGN SERIES


INPUT CAPTURE
• There are two event sources selected by ACIC flag of the ACSR.
– The ICP1 pin, PD.6
– The output of an analogue comparator (AIN0 & AIN1).

ICP1

UNIVERSITY OF KWAZULU-NATAL 17 DIGITAL SYSTEM DESIGN SERIES


IINPUT CAPTURE REGISTERS

UNIVERSITY OF KWAZULU-NATAL 18 DIGITAL SYSTEM DESIGN SERIES


STEPS TO PROGRAM INPUT CAPTURE FUNCTION
1. Initialize the TCCR1A and TCCR1B for a proper timer mode, enable or disable the noise
canceller, and select the edge (positive or negative) we want to measure the arrival time
for.
– Note: The input capture unit does not operate in modes where the ICR1 defines the TOP
value.
– Any modes except modes 8, 10, 12 and 14.
2. Initialize the ACSR to the selected desired event source.
3. Monitor the ICF1 flag in TIFR to see if the edge has arrived. Upon the arrival of the edge,
the TCNT1 value is loaded into the ICR1 register automatically by the AVR.
4. How do we measure period.
5. If measuring period:
a) Save the ICR1
b) Monitor the ICF1 to see if the second edge has arrived. Upon arrival TCNT1 value is
loaded into the ICR1 register automatically by the AVR.
c) Save the ICR1 for the second time
d) Subtract the first edge value from the second edge value and get the pulses.

First edge Second edge

UNIVERSITY OF KWAZULU-NATAL 19 DIGITAL SYSTEM DESIGN SERIES


CLASS EXERCISE – MEASURING PERIOD
• Assume that the clock pulses are fed into PORTD.6, write a program to measure the
period of the pulses in clock pulses. Place the binary results on PORTA and PORTB.
; ************************************************ L1: in Temp1, TIFR
.org 0x00 sbrs, Temp1, ICF1 ; skip if ICF1 is set
rjmp L1
rjmp Start in Temp2, ICR1L ;Temp2 =ICR1L
;************************************************ in Temp3, ICR1H ;Temp3 =ICR1L
;CONFIGURATION SECTION
out TIFR, Temp1 ;ICRF1=1 , you use 1 to clear a
.org 0x100 ;flag in AVR
Start: ldi Temp, 0xFF L2: in Temp1, TIFR
sbrs, Temp1, ICF1 ; skip if ICF1 is set
out DDRA, Temp ;PORTA as output
rjmp L2
ldi Temp, 0xFF
out DDRB, Temp ;PORTB as output out TIFR, Temp1 ;clear ICF1 Flag

in Temp4, ICR1L ;Temp2 =ICR1L


ldi Temp, 0x00
out TCCR1A, Temp ;Timer in normal mode sub Temp4, Temp2 ;get the period
ldi Temp, 0x41 ; rising edge, out PORTA, Temp4 ;PORTA= Temp 4
out TCCR1B, Temp ; no prescaler, no noise in Temp5, ICR1H
;cancellor sbc Temp5, Temp3
;*********************************************** out PORTB, Temp5 ;PORTB= Temp 5
Start:
loop1: rjmp loop1
Why not bothered with
UNIVERSITY OF KWAZULU-NATAL ACSR (ACIC) 20 DIGITAL SYSTEM DESIGN SERIES
MULTI-BYTE SUBTRACTIONSUBTRACTION IN ASSEMBLER – ON YOUR OWN
• Write a program to subtract two 16-bit numbers: 2762H-1296H.

; *************************************************************************************************

ldi Temp1, High(0x2762) ; Temp1 = 27H


ldi Temp2, Low(0x2762) ;Temp2 = 62H
ldi Temp3, High(0x1296) ;Temp3 = 12H
ldi Temp4, Low(0x1296) ;Temp4 = 96H

sub Temp2, Temp4 ; 62-96 =CCH ; Flags C = borrow = 1, N = 1

subc Temp1, Temp3 ; 27 – 12 – 1 = 14H

;Temp1 has 14H while Temp2 has CCH


;2762H-1296H=14CCH

; *************************************************************************************************

UNIVERSITY OF KWAZULU-NATAL 21 DIGITAL SYSTEM DESIGN SERIES


STEPS FOR MEASURING PULSE WIDTH
1. Initialize the TCCR1A and TCCR1B, and select capturing on rising edge.
2. Initialize the ACSR to the selected desired event source.
3. Monitor the ICF1 flag in TIFR to see if the edge has arrived. Upon the arrival of the
edge, the TCNT1 value is loaded into the ICR1 register automatically by the AVR.
4. Save the ICR1 and change the capturing to the falling edge.
5. Monitor the ICF1 to see if the second edge has arrived. Upon arrival TCNT1 value is
loaded into the ICR1 register automatically by the AVR.
6. Save the ICR1 for the second edge.
7. Subtract the first edge value from the second edge value and get the time/pulses.
Change capturing

First edge Second edge

UNIVERSITY OF KWAZULU-NATAL 22 DIGITAL SYSTEM DESIGN SERIES


CLASS EXERCISE – MEASURING PERIOD
• Assume that a 60 HZ frequency pulse is
connected to ICP1 (pin PORTD.6). Write a
program to measure its pulse width. Use the
prescaler value that gives the results in a single
byte. Display the result on PORTB. Assume XTAL
is 8MHz.

UNIVERSITY OF KWAZULU-NATAL 23 DIGITAL SYSTEM DESIGN SERIES


CLASS EXERCISE – MEASURING PERIOD
• Assume that a 60 HZ frequency pulse is connected to ICP1 (pin PORTD.6). Write a program to measure its
pulse width. Use the prescaler value that gives the results in a single byte. Display the result on PORTB.
Assume XTAL is 8MHz.
; ************************************************ L1: in Temp1, TIFR
.org 0x00 sbrs, Temp1, ICF1 ; skip if ICF1 is set
rjmp L1
rjmp Start in Temp2, ICR1L ;Temp2 =ICR1L
;************************************************ out TIFR, Temp1 ;ICRF1=0
;CONFIGURATION SECTION
ldi Temp, 0x05 ; falling edge,
.org 0x100 out TCCR1B, Temp ; prescaler=1024, no noise
Start: ldi Temp, 0xFF
out DDRB, Temp ;PORTB as output
L2: in Temp1, TIFR
ldi Temp, 0x00 sbrs, Temp1, ICF1 ; skip if ICF1 is set
rjmp L2
out TCCR1A, Temp ;Timer in normal mode
ldi Temp, 0x45 ; rising edge, in Temp3, ICR1L ;Temp3 =ICR1L
out TCCR1B, Temp ; prescaler=1024, no noise
sub Temp3, Temp2 ;get the period
;***********************************************
Start: out PORTB, Temp4 ;PORTA= Temp 4
out TIFR, Temp1 ;PORTB= Temp 4
Students to do the
calculation loop1: rjmp loop1 ;wait

UNIVERSITY OF KWAZULU-NATAL 24 DIGITAL SYSTEM DESIGN SERIES


PULSE WIDTH MODULATION IN 8 BIT TIMERS
• PWM involves changing (modulating) the
width of the pulse on a pin of the MCU.
• A technique used to generate analogue Ton Toff
signals from digital devices like the MCU T

• It generates voltages between two d = (Ton/T) *100 %


extreme values (5 and 0) by varying the
duty cycle between 0% and 100%.
• The duty cycle gives an equivalent
analogue voltage output. OC0
• The CPU can generate the pulses,
however, the AVR built in PWM feature
gives the programmer the option of
changing the period and duty circle. It
relieves the CPU to do other things.
• The waves are generated on the OCXn
pin. OC1B

OC1A

UNIVERSITY OF KWAZULU-NATAL 25 DIGITAL SYSTEM DESIGN SERIES


TIMER COUNTER UNIT

UNIVERSITY OF KWAZULU-NATAL 26 DIGITAL SYSTEM DESIGN SERIES


TIMER REGISTERS- FAST PWM - TCCR0

Selecting the Timer Action at overflow


Mode

Clock Select Bits

Force Output Compare

UNIVERSITY OF KWAZULU-NATAL 27 DIGITAL SYSTEM DESIGN SERIES


FAST PWM MODES
• The counter counts from BOTTOM (0) to TOP
(255) and restarts from the bottom.
• On a compare match between TCNT0 and OCR0
– Non inverted PWM mode – The OC0 is
cleared and set at the bottom.
– Inverted PWM mode- the OCO is set and
cleared at the bottom.

• Frequency of
Determine thegenerated
frequencywave is 1/256
of the the
generated
frequency
waveofinthe timerto
relation clock.
timer clock
Non inverted
• Non inverted mode duty cycle Inverted PWM PWM
DDetermine
= ((OCRO+1)/256) x 100
the duty cycle TCNT0
OXFF

• Inverted mode duty cycle OCR0

D = Determine
((255-OCRO)/256) x 100
the duty cycle
0
Time
OC0
OCR0+1 OCR0+1 OCR0+1 OCR0+1
• Note: In non PWM modes, a desired value 1
is loaded to the OCR0 instantly. In PWM
the value is loaded when the timer 0 Time
reaches the TOP or BOTTOM

UNIVERSITY OF KWAZULU-NATAL 28 DIGITAL SYSTEM DESIGN SERIES


CLASS EXERCISE – FAST PWM
• Assuming a XTAL of 8MHz, using non-inverted mode, write a program that generates a
wave with frequency of 31,250Hz and duty cycle of 75%
Calculating Frequency ;***********************************************
Determine
Fgeneratedwave the prescaler
= (Foscillator ) / (256xN) value CONFIGURATION SECTION
where N is the prescaler setting Init: SBI DDRB,3
Write the program
N = 8M/(31,250 x 256) = 1
LDI TEMP, 191
Calculating Duty Cycle OUT OCR0 TEMP ;OCR0=191
Determine the
D = ((OCRO+1)/256) OCR0 value
x 100
OCR0= ((75 x 256)/100)-1 LDI TEMP, 0X69 ; FAST PWM, NO
OCR0 = 191 OUT TCCR0 TEMP ; PRESCALER, NON INVERT

Configuration RJMP START


;**********************************************
Determine the TCCR0 value
START: RJMP START

0 1 1 0 1 0 0 1

UNIVERSITY OF KWAZULU-NATAL 29 DIGITAL SYSTEM DESIGN SERIES


TIMER REGISTERS- PHASE CORRECT PWM

Selecting the Timer Action at overflow


Mode

Clock Select Bits

Force Output compare

UNIVERSITY OF KWAZULU-NATAL 30 DIGITAL SYSTEM DESIGN SERIES


PHASE CORRECT PWM MODES
• The counter counts from BOTTOM (0) to MAX (255)
and the from MAX to BOTTOM.
• On a compare match between TCNT0 and OCR0
– Non inverted PWM mode – The OC0 is cleared
while counting up and set while counting down..
– Inverted PWM mode – The OC0 is set while
counting up and cleared while counting down.

• Frequency
Determineofthe
generated
frequencywave is 1/510
of the the
generated
frequency
waveof in
the timer clock.
relation to timer clock
Non inverted
255 255 PWM
TCNT0
• Non inverted mode duty cycle. OXFF

D =Determine
((2x OCR0)/510) x 100
the duty cycle OCR0

• Inverted mode duty cycle. 0


Time
OC0 OCR0 OCR0
D =Determine
((510 –(2 xthe
OCRO))/510)
duty cyclex 100 1

0 510 clocks
510 clocks Time

UNIVERSITY OF KWAZULU-NATAL 31 DIGITAL SYSTEM DESIGN SERIES


CLASS EXERCISE – PHASE CORRECT PWM
• Assuming a XTAL of 8MHz, using non-inverted mode, write a program that generates a
wave with frequency of 15,686Hz and duty cycle of 75%
Calculating Frequency ;***********************************************
Determine
Fgeneratedwave the prescaler
= (Foscillator ) / (510 x N)value CONFIGURATION SECTION
where N is the prescaler setting Init: SBI DDRB,3
Write the program
N = 8M/(15686 x 510) = 1
LDI TEMP, 191
Calculating Duty Cycle OUT OCR0 TEMP ;OCR0=191
D = ((2 X OCRO)/510)
Determinex 100
the TCCR0 value
OCR0= ((75 x 255)/100) LDI TEMP, 0X61 ; FAST PWM, NO
OCR0 = 191 OUT TCCR0 TEMP ; PRESCALER, NON INVERT

Configuration RJMP START


;**********************************************
Determine the OCR0 value
START: RJMP START

0 1 1 0 0 0 0 1

UNIVERSITY OF KWAZULU-NATAL 32 DIGITAL SYSTEM DESIGN SERIES


FAST PWM VS PHASE CORRECT PWM
Fast PWM Phase correct PWM
Phase of the wave different for different duty Phase of the wave same for different duty
cycle cycles

Duty cycle cannot be 0-non inverted mode or Can be from 0 to 100% for all the modes
100% in inverted mode

Frequency twice that of phase correct Frequency half of fast PWM

UNIVERSITY OF KWAZULU-NATAL 33 DIGITAL SYSTEM DESIGN SERIES


EXERCISE
• The student should extend the knowledge learnt
to Timer1 and cover the following aspects:
– Fast PWM.
– Phase correct PWM.
– Phase and frequency correct PWM.

UNIVERSITY OF KWAZULU-NATAL 34 DIGITAL SYSTEM DESIGN SERIES


DC MOTOR CONTROL WITH PWM
• DC motors are distinguished by their ability to operate from direct current. It’s a
device that translates electrical pulses into mechanical movement.
• The Parts
– Rotor – The rotating center portion. Term may also refer to the winding that is
on the rotor.
– Stator – The static (stationary) windings around the rotor. In many small
motors, the stator can be replaced with permanent magnets (not as efficient).
– Commutator – The brush connection to the winding on the rotor.
• In the DC motor we have only + and – leads. Connecting them to a DC voltage
source moves the motor in one direction. By reversing the polarity, the DC motor
will move in the opposite direction.
• The DC motor has two rpms: no-load and loaded. The rpm is reduced when moving
a load and it decreases as the load is increased
• A DC motor has the ability to turn through 360 degrees, continuously in one
direction when power is applied.
• A constant applied voltage leads to a constant speed. Reduction in voltage reduces
speed.
• With PWM, the average magnitude of the applied voltage is controlled and so is
the motor speed. PWM is the process of using the width of a pulse to convey
information in digital form.

UNIVERSITY OF KWAZULU-NATAL 35 DIGITAL SYSTEM DESIGN SERIES


PULSE WIDTH MODULATION
• The speed of the motor depends on three factors: (a) load, (b) voltage, and (c) current.
• By changing (modulating) the width of the pulse applied to the DC motor we can
increase or decrease the amount of power provided to the motor, thereby increasing or
decreasing the motor speed. Wider the pulse, the higher the speed.
• PWM is so widely used in DC motor control that some microcontrollers come with the
PWM circuitry embedded in the chip. In such microcontrollers all we have to do is load
the proper registers with the values of the high and low portions of the desired pulse,
and the rest is taken care by the microcontroller. This allows the microcontroller to do
other things.
• For microcontrollers without PWM circuitry, we must create the various duty cycle
pulses using software, which prevents the microcontroller from doing other things.

UNIVERSITY OF KWAZULU-NATAL 36 DIGITAL SYSTEM DESIGN SERIES


H-BRIDGE MOTOR DRIVER CONFIGURATION

UNIVERSITY OF KWAZULU-NATAL 37 DIGITAL SYSTEM DESIGN SERIES


DC MOTOR CONTROL WITH OPTOISOLATOR –DARLINGTON AND MOSFET

Zener diode  reduce gate voltage below max


Capacitor  reduce the EMI

Class Exercise: Do it to completion


Write a program that monitors the status of the switch and moves the motor with a
pulse of frequency 245Hz at duty cycle of 25% if switch open and 50% if switch is closed.
Use PWM and non PWM modes

UNIVERSITY OF KWAZULU-NATAL 38 DIGITAL SYSTEM DESIGN SERIES


CLASS EXERCISE – PHASE CORRECT PWM
• Assuming a XTAL of 8MHz, using non-inverted mode write a program that monitors the status of the
switch and moves the motor with a pulse of frequency 245Hz at duty cycle of 25% if switch open and
50% if switch is closed. Use PWM and non PWM modes
Calculating Frequency ;***********************************************
Determine
Fgeneratedwave the prescaler
= (Foscillator ) / (510 x N)value CONFIGURATION SECTION
where N is the prescaler setting Init: Write the program
SBI DDRB,3
N = 8M/(245 x 510) = 64 CBI DDRA,7
SBI PORTA,7
Calculating Duty Cycle LDI TEMP, 0X73 ;
Determine the OCR0 value
D = (OCRO/255) x 100 OUT TCCR0 TEMP
25%  OCR0= ((25 x 255)/100) = 64 L1: SBIC PINA,7
50%  OCR0= ((50 x 255)/100) = 127 LDI TEMP, 64
SBIS PINA,7
Configuration LDI TEMP, 127
OUT OCR0 TEMP
Determine the TCCR0 value
RJMP START
;**********************************************
0 1 1 1 0 0 1 1
START: RJMP START

UNIVERSITY OF KWAZULU-NATAL 39 DIGITAL SYSTEM DESIGN SERIES

You might also like