You are currently viewing Generating PWM with PIC16F877A PIC Advanced Development Board

Generating PWM with PIC16F877A PIC Advanced Development Board

Spread the love

PIC16F/18F Advanced Development Board is specifically designed to help students to master the required skills in the area of embedded systems. The kit is designed in such way that all the possible features of the microcontroller will be easily used by the students. The kit supports in system programming (ISP) which is done through USB port.

Microchip’s PIC(PIC16F877A)PIC16F/18F Advanced Development Kit is proposed to smooth the progress of developing and debugging of various designs encompassing of High speed 8-bit Microcontrollers.

PWM (Pulse Width Modulation)

Pulse width modulation (PWM) is a powerful technique for controlling analog circuits with a processor’s digital outputs. PWM is employed in a wide variety of applications, ranging from measurement and communications to power control and conversion.

Interfacing PWM

Figure 1 shows four different PWM signals. One is PWM output at a 25% duty cycle. That is, the signal is on for 25% of the period and off the other 75%. Next shows PWM output at 50%, 75% and 100% duty cycles, respectively. These three PWM outputs encode three different analog signal values, at 10%, 50%, and 90% of the full strength.

Generating PWM with PIC16F877A PIC Advanced Development Board

Fig. 1 PWM Outputs

Interfacing PWM with PIC16F877A

We now want to generate a PWM in PIC16F/18F Advanced Development Board at a particular frequency. Pulse Width Modulation is a technique for getting analog results with digital means.

Digital control is used to create a square wave, a signal switched between on and off. This on-off pattern can simulate voltages in between full on (5 Volts) and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off. The duration of “on time” is called the pulse width. To get varying analog values, you change, or modulate, that pulse width.

Pin Assignment with PIC16F877A

Circuit Diagram to Interface PWM with PIC16F877A

Source Code

The Interfacing PWM with PIC16F877A program is very simple and straight forward, which generates a pulse pattern in a particular frequency. An ADC signal is used to varying the duty cycle of PWM signal. The C program is written in Mplab software & it executed with Hi-Tech C compiler.

C Program to generate PWM in PIC16F877A

//Define PIC registers __CONFIG(0x3f72);
//Select HS oscillator, Enable (PWRTE,BOREN),
//Disable (CPD,CP,WDTEN,In-circuit Debugger)
#define XTAL 10000 //10Mhz=10000Khz
#define PWM_Freq 1 //1Khz PWM frequency
#define TMR2_PRE 16 //Timer2 Prescale
#define PR2_Val ((char)((XTAL/(4*TMR2_PRE*PWM_Freq))-1))
//Calculation for Period register PR2 (1Khz)
#define Duty_Cyc PR2_Val*2 unsigned int i;
void PWM_init(void);
void PWM_change(unsigned int);
void DelayMs(unsigned int);
void main(void)
void PWM_init(void)
//PWM channel 1 and 2 configured as output TRISC1=0;
PORTC = 0x00; CCP1CON=0x0c;
//CCP1 and CCP2 are configured for PWM CCP2CON=0x0c;
//Move the PR2 value T2CON=0x03;
//Timer2 Prescale is 16 TMR2=0x00;
//Turn ON timer2
void PWM_change(unsigned int DTY)
//Duty cycle change routine
//Value is between 0 to 255 CCPR2L=DTY;
void DelayMs(unsigned int Ms)
//Delay Routine
int delay_cnst; while(Ms>0)
for(delay_cnst = 0;delay_cnst <220;delay_cnst++);
//delay constant for 1Ms @10Mhz

To compile the above C code you need the Mplab software & Hi-Tech C Compiler. They must be properly set up and a project with correct settings must be created in order to compile the code. To compile the above code, the C file must be added to the project.

In Mplab, you want to develop or debug the project without any hardware setup. You must compile the code for generating HEX file. In debugging Mode, you want to check the port output without PIC16F/18F Advanced Development Board.

The PICKIT2 software is used to download the hex file into your microcontroller IC PIC16F877A through USB port.

Testing the PWM with PIC16F877A

Give +12V power supply to PIC16F/18F Advanced Development Board. The PWM port line is connected in PIC16F/18F Advanced Development Board. When the program is downloading into PIC16F877A in Advanced Development Board, the PWM output is generating at a particular frequency.

If you are not reading any PWM output, then you just check the jumper connections. Otherwise you just check it with debugging mode in Mplab. If you want to see more details about debugging just see the videos in below link.

General Information

  • For proper working use the components of exact values as shown in Circuit file. Wherever possible use new components.
  • Solder everything in a clean way. A major problem arises due to improper soldering, solder jumps and loose joints.
  • Use the exact value crystal shown in schematic.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.