Home / Tutorials / PIC Tutorial / PIC16 PWM Tutorial
PIC16F877A

PIC16 PWM Tutorial

Pulse width modulation may have been invented to encode messages into pulsing signal but it was originally conceived as a technique for controlling power supplied to motors. In this tutorial, we will look at how PIC16 PWM works.

The CCP Module

PWM with PIC16F877A is achieved using the Capture/Compare/PWM (CCP) module. This microcontroller has two CCP pins: CCP1 at #17 (RC2) and CCP2 at #16 (RC1). Each pin has a corresponding CCP register, CCP1CON and CCP2CON:

Capture and compare will be covered in another tutorial. You’ll see that we only have a few bits for concern: CCPxX:CCPxY and CCPxM3:CCPxM0.

Duty Cycle and Period

According to the image above, the former is the two least significant bits of the PWM duty cycle. Duty cycle is the high portion of the pulse and is calculated using the formula:

The PWM period is the length of one pulse and is calculated using the formula:

Naturally, the period is longer than the duty cycle as shown in the figure below:

However, you may (accidentally) set the duty cycle greater than the period. If this happens then the CCP pin will not produce a PWM signal.

For example, the following code in XC8 will produce a duty cycle of 400 us with a period of 819 us:

Here’s the calculations using the settings in the code:

C Code for PWM

Now we want to create a code wherein we can just specify the PWM frequency and duty cycle. Here’s what I did: I created a function that will calculate PR2 based on the given frequency:

Where TMR2Pre is equal to the TMR2 Prescale value.

I also added within the same function a routine that will compute the values of CCPRXL:CCPXCON<5:4> based on a given duty cycle.

The input duty cycle must be a percentage from 1 to 100.

Another function was created to turn on PWM:

These functions are saved into a header file named “pwm.h”. Now, because of how the PWM period and duty cycle are computed, there are minimum and maximum frequencies depending on the choice of oscillator and TMR2 prescale. I calculated the values for two oscillator values:

Prescale 16 at 20 MHz: 1.22k to 312.5 kHz
Prescale 4 at 20 MHz: 4.882k to 1.25 MHz
Prescale 1 at 20 MHz: 19.53k to 5 MHz
Prescale 16 at 4 MHz: 243.19 to 62.5 kHz
Prescale 4 at 4 MHz: 4.863k to 1.25 MHz
Prescale 1 at 4 MHz: 1.215k to 312.5 kHz

Here’s a full example code that utilizes "pwm.h" to generate around 50% duty cycle at 500 Hz:

Check Also

L298N Motor Controller Tutorial

How to Use L298N Motor Driver

You can drive a LED on or off using a microcontroller like Arduino or PIC. …