[Simple] Generating Pulse Width Modulation using PIC Microcontroller – Mikro C & Proteus Simulation

How to generate Pulse Width Modulation (PWM) using PIC microcontroller? This is going to be a continuation of our microcontroller online tutorial series. This article covers the detailed explanation and simulation of PWM generation using PIC16F877A microcontroller.What is a Pulse Width Modulator? A PWM has series of pulses with variable pulse width. We have used Mikro C pro for pic microcontroller programming because it has inbuilt library functions to do many operations especially in PWM, this will simplify the user headache.

The simulation is done with Proteus design suite. In my previous article we have already discussed How to Simulate PIC Microcontroller in Proteus?

PWM has wide applications in automation such as DC motor speed control circuit , generating analog signals from digital signals and LED brightness control.

Generating PWM using PIC

Before reading PIC PWM please have a look to the following


Two CCP module is available with a PIC16F877A MCU. It is capable of generating PWM signals.

MikroC PRO for PIC provides library which simplifies PWM operations. The generated PWM output comes via pin No: 16 and 17 of PIC16F877A microcontroller.

As the name indicates, CCP (Capture Compare PWM) has 3 modes of operation.

  1. Capture mode
  2. Compare mode
  3. PWM mode

In PWM mode PIC can generate signals of varying frequency and duty cycle.

Other modes are beyond the scope of this article. We will discuss those modes later.

Now let’s begin PIC PWM generation!

Add PWM Library to Mikro C

Before writing the program we should add PWM library functions to our code. Otherwise you may be getting compilation errors such as ‘Undeclared identifier PWM1_Init in expression’.

So open ‘Library Manager’ tab and put a check mark on ‘PWM’ library. Then proceed to the program.

PWM Library file in Mikro C

PWM Mikro C Program

In this program two input switches are used to control the Duty cycle of PWM signal. S1 for increasing duty cycle and S2 for decreasing the same. Frequency of PWM signal is set to 5KHz[cc lang=”C”]void main()
short duty1 = 16; // initial value for Duty cycle

TRISD = 0xFF; // Set PORTD as input
TRISC = 0x00; // Set PORTC as output

PWM1_Init(5000); // Initialize PWM1 for 5Kh
PWM1_Start(); // start PWM1
PWM1_Set_Duty(duty1); // Set current duty for PWM1

while (1) // endless loop
if (PORTD.F0==0) //Checking the button pressed or not
duty1++; // increment duty cycle
PWM1_Set_Duty(duty1); //Change the duty cycle
if (PORTD.F1==0) // Checking the button pressed or not
duty1–; // decrement duty cycle

Interpretation of important codes

There are two CCP modules inside PIC16F877A. Our program is manipulating the 1st module, thus ‘PWM1′ is used here. ‘_Init’ means initializing the PWM1  for 5KHz.

Start PWM1 signal.

Set current duty cycle for PWM1, using this code we are varying the pulse width.

PWM Simulation in Proteus

Now it’s time to start simulation, the Virtual Lab!

Step 1:
Write the codes in Mikro C and build the .Hex file. If you don’t know how to build hex file please read my article below.

Step 2:
Run Proteus 8 and draw the schematic diagram as shown.

PIC PWM Schematic diagram
(Click on the image to enlarge)

For beginners play the help video tutorial below.

Help Video: Getting Started with Proteus

Step 3:
Load .Hex file for PIC simulation.

PWM Hex file loading

Step 4:
Run the simulation.

Run Proteus PWM simulation

Step 5:
Enable Digital oscilloscope output by going to Debug >> Digital Oscilloscope and make necessary adjustments for clear view.

Digital Oscilloscope in Proteus 8

Step 6:
Now vary the duty cycle by hitting the keys provided S1 and S2. Below image shows the simulated window of CRO.

PWM Signal on CRO

Enjoyed this article? Please like our facebook page.

Next >>  Pulse width modulation DC motor speed control.

Let me know if this article was helpful to you through the comment box below.

7 thoughts on “[Simple] Generating Pulse Width Modulation using PIC Microcontroller – Mikro C & Proteus Simulation

  1. I try this circuit with a 16f76 wich is very similar to the 16f877 and like to know whem youb pres the up or down button to change the duty works fine but as soon you release the button the duty cycle resets as the initial how can you keep the duty cycle at a point you want. eg. if I want 50% duty I start to increase and when reach the % I want it should keep that output.

  2. This is such an excellent tutorial and example. Thank you! I was successful at emulating the circuit and code. I am just beginning to learn micro controllers so forgive me if my question is very basic. How would you go about limiting the increment/decrement functions to stop at their upper/ lower limits ( 0 or 255). Currently once 100% duty is reached the PWM duty cycle jumps to 0% and continues climbing if the increment button remains on. How would you make it stop at 100%?

  3. I have made the hardware of above circuit. And i received a DC signal on DSO (Which is not a pure PWM wave). Also, i am still not able to increase or decrease the duty cycle using correspond push buttons. Kindly Help me !

  4. I am not getting any output on CRO.I am using microC PRO. I have also checked with your hex file provided. Kept processor clock frequency @10MHz of PIC16F877A microcontroller. By default X1 Crystal frequency was @1MHz. Kindly assist me. If possible kindly mail me the proteus design with the micro C code/ hex file to my mail ID debnath.sujoy.2011@gmail.com

Leave a Reply

Your email address will not be published. Required fields are marked *