TOPIC 5 - Part 2 PWM
TOPIC 5 - Part 2 PWM
APPLICATIONS
TOPIC 5:
HARDWARE INTERFACING
(Part 2 – PWM)
2
LEARNING OUTCOME
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 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
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
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:
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
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)
ACTIVITY 2
1. 20 % of duty cycle
2. 40% of duty cycle
18
1. Load the PR2 value which will decide the period of the pulse.
4. Initialize the pin CCP1 as an output pin which will give PWM
output.
Programming PWM
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>
while(1)
{
DutyCycle = DutyCycle + 20; //increase DutyCycle
}
DC MOTOR & SPEED CONTROL 23
USING PWM
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
// clockwise direction
RD6=1;
RD7=0;
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