Professional Documents
Culture Documents
5.2 PWM 7 Mac 2017
5.2 PWM 7 Mac 2017
GENERAL OBJECTIVES
Understand The Internal Features Of The PIC
Microcontroller
SPECIFIC OBJECTIVES
Explain The Pulse Width Modulation (PWM) Feature In
PIC
Give Examples Of C Program To Create PWM Output
Circuit used to demonstrate how to generate PWM using microcontroller
What is
PWM ?
PWM
PWM - Pulse Width Modulation and can be a way of simulating analog voltage
outputs.
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.
PWM Description
Pulse Width Modulation can be constructed with just two words, – duty cycle
and frequency.
The fraction of time period for which the signal is ON to the total time period is
termed as Duty Cycle. so duty cycle can vary inside the range 0 – 100.
Number of periods inside the one second represents the Frequency.
PWM Description
When the duty cycle is 50% ( ratio 0.5 – pulse width taking half of
period time ) the generated digital signal can be observed like as
analog signal 50% of the digital voltage.
For example – if our digital signal voltage (Vhigh) is 5 volts and we
are generating signal with a 50% duty cycle – this can be observed
like a 2.5 volt analog output, or even more simplified – output
voltage is equal to digital signal voltage multiplied by duty cycle
ratio.
Example of usage could be if we want to create a smooth blinking
LED – all we have to do is constant periodic changing of the duty
cycle from 0 to 100 and back.
PWM in Microcontroller
PWM waves can be easily 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.
For the time being we only focus on PWM only
PIC 18F4520 Pin Diagram
Explanation of CCP1 and
CCP2
In 40 pin devices, CCP1 is implemented as an Enhanced CCP module with
standard Capture and Compare modes and Enhanced PWM modes.
Each Capture/Compare/PWM module is associated with a control register
(generically, CCPxCON) and a data register (CCPRx).
The data register, in turn, is comprised of two 8-bit registers: CCPRxL (low byte)
and CCPRxH (high byte). All registers are both readable and writable.
CCPxCON – CCPx Control
Register
Data Register – CCPR1L and CCPR1H
The period of PWM
The CCP module uses Timer 2 and its associated register, PR2 for the PWM
time-base, which means that the frequency of the PWM is a fraction of the
Fosc, the crystal frequency. It uses the PR2 register to set the PWM period as
follows
Tpwm=[(PR2) + 1] 4xNxTosc
Solution:
To set the duty cycle, the CCP module uses the 10-bit register of DC1B9:DC1B0.
The 10-bit register of DC1B9:DC1B0 is formed from 8-bits of CCPR1L and 2 bits from
the CCP1CON register, where CCPR1L is the upper 8 bits and DC1B1:DC1B0 of the
CCP1CON are the lower 2 bits of the 10-bits register
In reality, CCPR1L is the main register for the duty cycle and the lower 2 bits of
DC1B1:DC1B0 are for the decimal point portion of the duty cycle and are set as
follows:
The duty cycle of PWM
DC1B1 DC1B0 Decimal Points
0 0 0
0 1 0.25
1 0 0.5
1 1 0.75
Example :
PR2 Duty Cycle PR2xDuty CCPR1L DC1B2:DC1B1
Cycle
50 20% 20%x50=10 10 00 (0)
70 65 70x65%=45.5 45 10 (0.5)
70 50% 70x50%=35 35 00
Steps in programming PWM in
Microcontroller
1. Set the PWM period by writing to the PR2 register.
2. Set the PWM duty cycle by writing to the CCPR1L register for the higher 8
bits.
3. Set the CCP pin as an output by clearing the appropriate TRIS bit.
4. Using the T2CON register, set the prescale value.
5. Clear the TMR2 register.
6. Configure the CCP1CON register for PWM and set DC1B2:DC1B1 bits for
the decimal portion of the duty cycle.
7. TMR2 ON
8. keep monitoring the value for PR2 and TMR2 when the value is equal it will
raise the TMR2IF flag and reset TMR2 to 0.
Example :
Given XTAL=10 MHz and the generate frequencies for PWM is 2.5
kHz with 75% duty cycle. Find the value of registers
a) PR2
b) CCPR1L
c) DC1B2:DC1B1
Write a program in C language using the above data to show 2.5
kHz PWM frequency with a 75% duty cycle on the CCP1 pin.
Demonstrate the output result using Proteus.
Solution :
Assume N=1, Assume N=4,
PR2=[Fosc / (Fpwmx4xN)] -1 PR2=[Fosc / (Fpwmx4xN)] -1
PR2 = [Fosc / (2.5 kHzx4x1)] -1 PR2 = [Fosc / (2.5 kHzx4x4)] -1
= 10 M - 1 = 10 M - 1
10000 40000
= 999 (Cannot be accepted = 249 (Can be accepted
because larger than 255) because smaller than 255)
Assume N=16,
PR2=[Fosc / (Fpwmx4xN)] -1
PR2 = [Fosc / (2.5 kHzx4x16)] -1
= 10 M - 1
160000
= 61.5 (Not suitable value because contains decimal
point)
PR2 x Duty Cycle = 249x755 = 186.75
CCPR1L=186
DC1B1:DC1B0 = 11
Program from MPLAB
Result from Proteus