Pulse-width modulation (PWM) is a modulation process or technique used in most communication systems. The PWM signal we can use to control the speed of DC motor or to control the intensity. It is also used to control the Analog Devices. And mainly the power which is delivered to the Analog device is controlled using this technique. Now, because of its high-efficiency low power loss and its ability to precisely control the power this technique is used in many applications. In this article, we will discuss PWM in PIC16F877A.

PWM - PIC16F877A

Pulse Width Modulation (PWM) is a method of changing the duration of a pulse with respect to the analog input. The duty cycle of a square wave is modulated to encode a specific analog signal level. The PWM signal is digital because, at any given instant of time, the full DC supply is either ON or OFF completely. PWM method is commonly used for speed controlling of fans, motors, lights in varies intensities, pulse width modulation controller, etc. These signals may also be used for approximate time-varying of analog signals. PWM is employed in a wide range of applications, ranging from measurement and communications to power control and conversion.


Pulse width modulation is basically a square wave with a varying high and low time. A basic PWM signal is shown in the figure below.

pulse width modulation duty cycle

There are various terms associated with PWM:

  1. On-Time(Ton): Duration of time signal is high
  2. Off-Time(Toff): Duration of time signal is low
  3. Period: It is represented as the sum of on-time and off-time of PWM signal
  4. Duty cycle: It is represented as a percentage of time signal remains on during the period of the PWM signal

The first step to create a PWM signal is to define the period of a single PWM cycle. The next step is to define the period of time within the PWM cycle. Let us see how to implement this in the PIC16F877A peripheral. In PIC16F877A, only Timer 2 can be used for PWM generation. TMR2 is a 16-bit Timer 2 register which is used to hold the count.

PWM Working - PIC16F877A

PWM in PIC16F877A –  Operation Steps

  • Set the PWM period by writing the PR2 register.
  • Set the PWM duty cycle. The PWM duty cycle is specified by writing to the CCPRxL register and to the CCPxCON<5:4> bits. Up to 10-bit resolution is available.
  • RC2 pin is used for output configuration.
  • The Prescaler value set in the TMR2 register.After setting up enable the timer2 module by writing to T2CON.
  • We are configuring the CCP module for PWM operation. We can do this by CCP1CON mode selection bits (bits 3-0)

Register configuration for PWM in pic16f877a

PIC16F877A microcontroller has two independent CCP(Capture/Compare/PWM) modules, named as CCP1 and CCP2. Each CCP module has two 8-bit resistors(CCPxH, CCPxL) that can be used as:

  • 16-bit Capture Register
  • 16-bit Compare Register
  • 10-bit PWM Register

PIC16F877A has two PWM module with a resolution of 10-bits. The 8-MSB bits are stored in CCPRxL and the remaining 2-bits in the CCPxCON register. The below tables shows the PWM module of PIC.
The below table shows the registers associated with PIC16F877A PWM.

PIC16F877A PWM registers

Structure Of CCPxCON Register

pwm register CCPxCON

Unimplemented(bit 7-6): Read as ‘0’

CCPxX:CCPxY(bit 5-4): PWM Least Significant bits,
Capture Mode
Compare Mode
PWM Mode
These bits are the two LSbs of the PWM duty cycle. The eight MSBs are found in CCPRxL.

CCPxM3:CCPxM0(bit 3-0): CCPx Mode Select bits
0000 – Capture/Compare/PWM disabled (resets CCPx module)
0100 – Capture mode, every falling edge
0101 – Capture mode, every rising edge
0110 – Capture mode, every 4th rising edge
0111 – Capture mode, every 16th rising edge
1000 – Compare mode, set output on match (CCPxIF bit is set)
1001 – Compare mode, clear output on match (CCPxIF bit is set)
1010 – Compare mode, generate software interrupt on match (CCPxIF bit is set, CCPx pin is unaffected)
1011 – Compare mode, trigger special event (CCPxIF bit is set, CCPx pin is unaffected);
11xx – PWM mode

PWM Period calculation

PWM period = [(PR2) + 1] x 4 x Tosc x (TMR2 prescale value)

Example: We use 20 MHz clock and the O/P frequency required is 5KHz
Here PWM period = 1/ Frequency (that will be 1/5000 = 0.0002)
0 .0002 = (PR2 + 1) x (1 / 20000000) x 16
PR2 + 1 = (0.0002 x 20000000) / 16 =250
PR2 = 249
PR2 = 0xF9

These are the necessary steps required to generate a PWM signal in a single channel. You might want to update the cycle period and vary the duty cycle. For this, create a function where you can enter your required PWM Pulse Period.

PWM IN PIC16F877A – LED fading Example 

Program to demonstrates the LED fading using PWM signals.

Advantages of PWM

  • PWM gives output in terms of the digital signal which reduces noise.
  • Less heat dissipated versus using resistors for intermediate voltage values.
  • Average value proportional to duty cycle, so variation in pulse width leads to change in output which can be easily controlled.

Disadvantages of PWM

  • Complexity of circuit
  • Radio Frequency Interference
  • Voltage spikes
  • Electromagnetic noise





Spread the love, share this