100% found this document useful (1 vote)
697 views30 pages

TOPIC 5 - Part 2 PWM

The document discusses pulse width modulation (PWM) and its implementation using a PIC microcontroller. It explains that PWM varies the pulse width to produce analog voltages from digital signals. The CCP module in PIC microcontrollers can generate PWM waves by configuring its registers like PR2, CCPR1L, and CCP1CON. PR2 determines the period and CCPR1L sets the duty cycle. Programming examples are provided to set up PWM of different frequencies and duty cycles on the CCP1 pin for applications such as LED brightness control.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
697 views30 pages

TOPIC 5 - Part 2 PWM

The document discusses pulse width modulation (PWM) and its implementation using a PIC microcontroller. It explains that PWM varies the pulse width to produce analog voltages from digital signals. The CCP module in PIC microcontrollers can generate PWM waves by configuring its registers like PR2, CCPR1L, and CCP1CON. PR2 determines the period and CCPR1L sets the duty cycle. Programming examples are provided to set up PWM of different frequencies and duty cycles on the CCP1 pin for applications such as LED brightness control.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

DEC5052 - EMBEDDED SYSTEM

APPLICATIONS

TOPIC 5:
HARDWARE INTERFACING
(Part 2 – PWM)
2

LEARNING OUTCOME

At the end of this chapter, student should be able


to:
Explain the Pulse Width Modulation
(PWM) feature in the PIC.

Give examples of C program to create


PWM output.

Produce the PWM to control the


current and voltage flow into the DC
load.
3

INTRODUCTION
 Pulse Width Modulation (PWM) is the one of the simple and most
commonly used technique to produce analog voltages from digital signals. It
has a wide variety of applications such as Digital to Analog Converter (DAC),
DC Motor Speed Control, Sine Wave Inverters, Brightness control etc.
 PWM signals are ON – OFF signals (HIGH or LOW) (hence the name Pulse)
whose HIGH or ON duration is changed (hence Width Modulation) in
accordance with our requirements.
 Pulse Width Modulation (PWM) is a technique by which the width of a pulse
is varied while keeping the frequency of the wave constant.
4
CCP – Capture/Compare/ PWM Module

 PWM waves can be generated using


CCP modules available with most of
the PIC Microcontrollers.
 CCP stands for
Capture/Compare/PWM, which means
that it can be used for Capture or
Compare or PWM operations.
 Microchip’s PIC 18F4550
microcontroller has two CCP modules:
CCP1 and CCP2 which are multiplexed
with RC1 & RC2 of PORTC.
5

PWM Signal
 A PWM output has a time base and a time
that the output stays high. One cycle has a
fixed time period called ‘Period’
 A period of a pulse consists of an ON
cycle and an OFF cycle. The fraction for
which the signal is ON over a period is
known as a duty cycle.

𝑇 𝑜𝑛
𝐷𝑢𝑡𝑦 𝐶𝑦𝑐𝑙𝑒= x 100 %
𝑇𝑜𝑡𝑎𝑙 𝑃𝑒𝑟𝑖𝑜𝑑
6

PWM REGISTER

• Pulse-Width Modulated (PWM) signal generated on the


CCP1 and CCP2 pins on PIC18f.
• Duty cycle, period and resolution determined by the
following registers
7

TIMER2 Control Register


CCPxCON REGISTER
8
9

CCPR1H & CCPR1L REGISTER


 In CCP module, there is a 16-bit register which is split as two 8-bit registers
- CCPR1H and CCPR1L.

 Only CCPR1L is used to decide the duty cycle of the PWM. CCPR1H is
not user accessible for the PWM mode.
10

PR2 REGISTER
 Time period of the generated PWM waves are determined by the value
of PR2 Register.
 PR2 is an 8-bit register which is used to load a count for a period of the
pulse (TPWM).
 The value for the PR2 :

Where:
Fosc= 1/ Tosc
Fpwm= 1/ Tpwm
Tpwm – desired PWM period
N – prescaler of 1, 4, or 16 set by T2CON
11

EXAMPLE 1

Given Fosc=20MHz. Determine the value of PR2 and


Prescale, if we want FPWM­ = 3.094KHz.
Solution:

PR2= [ Fosc / (Fpwm x 4 x N) ] - 1 where N is Prescale (1:1 or 1:4 or 1:16)

PR2= ((20MHz/(3.094 KHz x 4 x 1))-1=1615 (invalid – more than 255)


PR2= ((20MHz/(3.094 KHz x 4 x 4))-1=403 (invalid – more than 255)
PR2= ((20MHz/(3.094 KHz x 4 x 16))-1=100 (valid)

 PR2= 100, Prescale = 16

11
12

EXAMPLE 2
Find the minimum and maximum Fpwm frequency allowed
for XTL =10MHz. State the PR2 and prescaler values for
the minimum and maximum Fpwm.

Solution:

Minimum Fpwm  PR2=255 and prascaler=16,


Fpwm = 10MHz/(4 x 16 x 256) = 610 Hz.

Maximum Fpwm  PR2=1 and prescaler=1,


Fpwm =10 MHz/(4 x 1 x 1) = 2.5MHz.
13

ACTIVITY 1

Find the PR2 value and the prescaler needed to get the
following PWM frequencies:

1. 1.22 kHz
2. 4.88 kHz
3. 78.125 kHz

Assume XTAL = 20 MHz.


14

DUTY CYCLE REGISTER


 As the PIC18F4550 generates a 10-bit PWM pulse, to set the duty cycle it
uses 10-bit register.
 The higher 8 bits (MSBs) of this register are in CCPR1L register (8-bit) and
lower 2 bits(LSBs), which are used for a decimal portion in duty cycle, are
in CCP1CON register at bit 5 and 4 respectively.
 So the 10-bit value for duty cycle is represented by
CCPR1L : CCP1CON
<bit5, bit4>
15

EXAMPLE 3
1. If PR2=50, and we need a 20% duty cycle,
then CCPR1L = 20% x 50 =10.0 and
DC2:DC1= 00

2. Now assume that we want a 25% duty cycle for the same PR2:
50 x 25%=12.5
then CCPR1L=12 and
DC2:DC1=10 to take care of the 0.5 part.
*It must be noted that the value for the duty cycle register of the CCPR1L is
always some percentage of the PR2 register.
EXAMPLE 4 16

Find the values of registers PR2, CCPR1L and DC1B2:DC1B1 for the 2.5 kHz
PWM frequency if we want a 75% duty cycle. Assume XTAL=10MHz.

Solution:
• PR2 = ((10MHz/(2.5 kHz x 4 x 1))-1 = 999 (invalid- more than 255)
• PR2 = ((10MHz/(2.5 kHz x 4 x 4))-1 = 249 (valid)

75% duty cycle = 75% x PR2 = 75% x 249 = 186.75


we have CCPR1L= 186 and
DC1B2:DCB1 = 11 for the 0.75 portion
17

ACTIVITY 2

If PR2=200, determine the value of CCPR1L in order to produce the


following duty cycle of PWM:

1. 20 % of duty cycle
2. 40% of duty cycle
18

Steps for Programming

1. Load the PR2 value which will decide the period of the pulse.

2. Set the duty cycle by loading value in the CCPR1L: CCP1CON<5:


4>

3. Configure the CCP1CON register for setting a PWM mode.

4. Initialize the pin CCP1 as an output pin which will give PWM
output.

5. Configure the T2CON register and enable TMR2 using T2CON


19

Programming PWM

PR2 = _____ ; //Set the PWM period


CCPR1L = ____; //Set the PWM duty cycle
CCP1CON = 0B_____ ; //Configure the CCPxCON register for PWM
TRISC2= 0; //Set the CCPx pin as an output.
T2CON = 0B_______ ; //set the prescale value
TMR2ON = 1 ; //Start Timer2
EXAMPLE 5 20

Write a program to output a 2.5 kHz PWM with a 75% dutycycle on


the CCP1(RC2) pin by using 4 prescale and Fosc = 10 MHz.

PR2 = [Fosc/(Fpwm x 4 x N)] – 1 #include <xc/h>

= [10 MHz/ (2.5kHz x 4 x4)] -1 void main (void)


{
= 249 //configure PWM
TRISC2=0; //make CCP1 pin an output
PR2=249;
CCPR1L = 75% x 249 = 186.75 CCPR1L=186 //75% duty cycle
T2CON = 0x01; //Timer2, 4 prescale, no postscaler
CCP1CON=0B00111100; //PWM mode, 11 for DC1B1:B0
TMR2=0; //clear Timer2
TMR2ON=1; //turn on Timer2

while(1)
{
CCPR1L=186 //output 75% duty cycle
}
}
EXAMPLE 6 21

Write a program to produce PWM pulse on CCP1 pin using 2.48 kHz
PWM with prescaler N=1 and Fosc = 10MHz . The pulse is used to
control the brightness of LED.
#include <xc/h>

EXAMPLE 6 void delay_ms(unsigned int x);


22
signed int DutyCycle ; //value for CCPR1L

void main (void)


PR2 = [Fosc/(Fpwm x 4 x N)] – 1 {
//configure PWM
= [10 MHz/ (2.48 kHz x 4 x 1)] -1 TRISC2=0; // RC2/CCP1 as output pin
PR2=100;
= 100 CCPR1L= 0; // 0% duty cycle
T2CON = 0B00000000; // prescale(N=1), no postscaler
CCP1CON = 0B00001100; // PWM mode
TMR2ON=1; // turn on Timer2
DutyCycle = 0;

while(1)
{
DutyCycle = DutyCycle + 20; //increase DutyCycle

if (DutyCycle >= PR2)


DutyCycle=95;
CCPR1L=DutyCycle;
delay_ms(10);
DutyCycle = DutyCycle - 20; //decrease DutyCycle
if(DutyCycle <=5 )
DutyCycle=5;
CCPR1L=DutyCycle;
delay_ms(10);

}
DC MOTOR & SPEED CONTROL 23

USING PWM

 The speed of the motor depends on three factors:


1. Load
2. Voltage
3. Current

 For a given fixed load we can maintain a steady speed by


using a method called Pulse Width Modulation (PWM).
 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.
24

DC MOTOR– Unidirectional Control

 DC motor has (+) and (–) leads.

 By connecting the leads to a DC voltage source, the


motor will move in one direction.
 By reversing the polarity, the DC motor will move in the
opposite direction.
25

MOTOR DRIVER – L293D


 L293D and L293 are two such
ICs.
 These are dual H-bridge motor
drivers, ie by using one IC we can
control two DC Motors in both
clock wise and counter clockwise
directions. 
 The L293D can
provide bidirectional drive
currents of up to 600-mA at
voltages from 4.5 V to 36 V while
L293 can provide up to 1A at
same voltages.

Input Logic table of L293D


26

MOTOR DRIVER – L293D

1. Activate DC motor to move clockwise


EN1=1; //enable L293D
IN1=0;
IN2=1;
2. Activate DC motor to move counter clockwise
EN1=1; //enable L293D
IN1=1;
IN2=0;
3. Stop DC motor
EN1=1; //enable L293D
IN1=0;
IN2=0;
27
CONTROL HIGH VOLTAGE MOTOR USING
LOW VOLTAGE SIGNAL
28

DC MOTOR SPEED CONTROL

https://
i0.wp.com/deepbluembedded.com/wp-content/uploads/2018/09/DC-Motor-Sp
eed-Control-Simulation.gif?resize=1024%2C596&ssl=1
EXAMPLE 6 #include<xc.h>
29

void main (void)


Given prescaler, N=16 and Fpwm=3.09KHz. By {
refering to figure below, write a C program to run TRISD7=0;
the motor clockwise with 80% of full speed. TRISD6=0;

//configure to PWM mode


PR2=100;
TRISC2=0;
T2CON=0B00000110;
CCP1CON=0B00001100;

// clockwise direction
RD6=1;
RD7=0;

//PWM duty cycle = 80%


CCPR1L=80;
while(1);
}
30

ACTIVITY 3
Refer to the following figure, write a C program to monitor the status of the
switch and perform the following:

a) If Switch=1, the DC motor moves FORWARD with 25% duty cycle pulse
b) If Switch=0, The DC motor moves REVERSE with 50% duty cycle pulse.

30

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy