Tutorial 14: Pulse Width Modulation

Two pulse width modulated signals are generated on C1,2 representing a value of an 8 bit number seen on port D. 

Description

Pulse Width Modulation is a technique for representing a ## information

To configure the CCP modules for PWM operation the following steps are taken

1. Set the PWM period by writing the PR2 register

The PWM period is the time in which the PWM signal will repeat itself. Timer2 is used by the PWM to compute the period. The period is set as the time it takes for Timer2 to overflow. Timer2 receives its input directly from the internal clock so increments with period  Tosc/4. Timer2 also has a prescalar which can be set to 1:2, 1:4 or 1:16.  Unlike Timer0 or Timer1 which overflow when incrementing past their maximum value, Timer2 has a variable overflow value which is stored in the Period Register PR2. When Timer2 register TMR2 equals PR2 the next increment of TRM2 will cause an overflow and mark the end of the period.

The PWM period then is computed  as

PWM period = [(PR2) + 1] • 4 • TOSC •(TMR2 prescale value)

In this example, a 1Khz PWM signal is

Download FEATURE877_Tutorial14.zip and unzip into folder <your path>\Tutorial 14

Filename Version Description
tut14.pjt 1.0 MPLAB Project
tut14.asm 1.0 PIC16F877 Assembler code
tut14.hex 1.0 Precompiled HEX
tut14.lst 1.0 Precompiled LST
tut14.vbb 1.0 Virtual Breadboard simulation

Demonstrates

Timer2 PWM mode