In this tutorial, we will learn How to generate PWM signals using PIC PIC16F877A. Our PIC MCU has a special module called Compare Capture module (CCP) which can be used to generate PWM signals. Here, we will generate a PWM of 5 kHz with a variable duty cycle from 0% to 100%. To vary the duty cycle we are using a potentiometer, hence it is recommended to learn ADC tutorial before starting with PWM. PWM module also uses timers to set its frequency hence learn how to use timers beforehand here. Further, in this tutorial we will use a RC circuit and a LED to convert the PWM values to Analog voltage and use it for dimming the LED light.

What is a PWM Signal?

Pulse Width Modulation (PWM) is a digital signal which is most commonly used in control circuitry. This signal is set high (5v) and low (0v) in a predefined time and speed. The time during which the signal stays high is called the “on time” and the time during which the signal stays low is called the “off time”. There are two important parameters for a PWM as discussed below:

Duty cycle of the PWM:

The percentage of time in which the PWM signal remains HIGH (on time) is called as duty cycle. If the signal is always ON it is in 100% duty cycle and if it is always off it is 0% duty cycle.

Duty Cycle =Turn ON time/ (Turn ON time + Turn OFF time)

Frequency of a PWM:

The frequency of a PWM signal determines how fast a PWM completes one period. One Period is complete ON and OFF of a PWM signal as shown in the above figure. In our tutorial we will set a frequency of 5KHz.

PWM using PIC16F877A:

PWM signals can be generated in our PIC Microcontroller by using the CCP(Compare Capture PWM) module. The resolution of our PWM signal is 10-bit, that is for a value of 0 there will be a duty cycle of 0% and for a value of 1024 (2^10) there be a duty cycle of 100%. There are two CCP modules in our PIC MCU (CCP1 And CCP2), this means we can generate two PWM signals on two different pins (pin 17 and 16) simultaneously, in our tutorial we are using CCP1 to generate PWM signals on pin 17.

The following registers are used to generate PWM signals using our PIC MCU:

CCP1CON (CCP1 control Register)

T2CON (Timer 2 Control Register)

PR2 (Timer 2 modules Period Register)

CCPR1L (CCP Register 1 Low)

Programming PIC to generate PWM signals:

In our program we will read an Analog voltage of 0-5v from a potentiometer and map it to 0-1024 using our ADC module. Then we generate a PWM signal with frequency 5000Hz and vary its duty cycle based on the input Analog voltage. That is 0-1024 will be converted to 0%-100% Duty cycle. This tutorial assumes that you have already learnt to use ADC in PIC if not, read it from here, because we will skip details about it in this tutorial.

The following steps should be taken when configuring the CCP module for PWM operation:

Set the PWM period by writing to the PR2 register.

Set the PWM duty cycle by writing to the CCPR1L register and CCP1CON<5:4> bits.

Make the CCP1 pin an output by clearing the TRISC<2> bit.

Set the TMR2 prescale value and enable Timer2 by writing to T2CON.

Configure the CCP1 module for PWM operation.

There are two important functions in this program to generate PWM signals. One is the PWM_Initialize() function which will initialize the registers required to set up PWM module and then set the frequency at which the PWM should operate, the other function is the PWM_Duty() function which will set the duty cycle of the PWM signal in the required registers.

The above function is the PWM initialize function, in this function The CCP1 module is set to use PWM by making the bit CCP1M3 and CCP1M2 as high.

The timer module’s prescaler is set by making the bit T2CKPS0 as high and T2CKPS1 as low the bit TMR2ON is set to start the timer.

Now, we have to set the Frequency of the PWM signal. The value of the frequency has to be written to the PR2 register. The desired frequency can be set by using the below formulae

PWM Period = [(PR2) + 1] * 4 * TOSC * (TMR2 Prescale Value)

Rearranging these formulae to get PR2 will give

PR2 = (Period / (4 * Tosc * TMR2 Prescale )) - 1

We know that Period = (1/PWM_freq) and Tosc = (1/_XTAL_FREQ). Therefore.....

PR2 = (_XTAL_FREQ/ (PWM_freq*4*TMR2PRESCALE)) – 1;

Once the frequency is set this function need not be called again unless and until we need to change the frequency again. In our tutorial I have assigned PWM_freq = 5000; so that we can get a 5 KHz operating frequency for our PWM signal.

Our PWM signal has 10-bit resolution hence this value cannot be stored in a single register since our PIC has only 8-bit data lines. So we have use to other two bits of CCP1CON<5:4> (CCP1X and CCP1Y) to store the last two LSB and then store the remaining 8 bits in the CCPR1L Register.

The PWM duty cycle time can be calculated by using the below formulae:

PWM Duty Cycle = (CCPRIL:CCP1CON<5:4>) * Tosc * (TMR2 Prescale Value)

Rearranging these formulae to get value of CCPR1L and CCP1CON will give:

CCPRIL:CCP1Con<5:4> = PWM Duty Cycle / (Tosc * TMR2 Prescale Value)

The value of our ADC will be 0-1024 we need that to be in 0%-100% hence, PWM Duty Cycle = duty/1023. Further to convert this duty cycle into a period of time we have to multiply it with the period (1/ PWM_freq)

You can check the complete program in the Code section below along with the detailed Video.

Schematics and Testing:

As usual let us verify the output using Proteus simulation. The Circuit Diagram is shown below.

Connect a potentiometer to 7th pin to feed in a voltage of 0-5. CCP1 module is with pin 17 (RC2), here the PWM will be generated which can be verified using the Digital oscilloscope. Further to convert this into a variable voltage we have used a RC-filter and an LED to verify the output without a scope.

What is a RC-Filter?

An RC filter or a Low pass filter is a simple circuit with two passive elements namely the resistor and the capacitor. These two components are used to filter the frequency of our PWM signal and make it a variable DC voltage.

If we examine the circuit, when a variable voltage is applied to the input of R, the capacitor C will begin to charge. Now based on the value of the capacitor, the capacitor will take some time to get fully charged, once charged it will block the DC current (Remember capacitors block DC but allows AC) hence the input DC voltage will appear across the output. The high frequency PWM (AC signal) will be grounded through the capacitor. Thus a pure DC is obtained across the capacitor. A value of 1000Ohm and 1uf was found to be appropriate for this project. Calculating the values of R and C involves circuit analysis using transfer function, which is out of scope of this tutorial.

The output of the program can be verified using the Digital Oscilloscope as shown below, vary the Potentiometer and the Duty cycle of the PWM should change. We can also notice the output voltage of the RC circuit using the Voltmeter. If everything is working as expected we can proceed with our hardware. Further check the Video at the end for full process.

Working on Hardware:

The hardware setup of the project is very simple, we are just going to reuse our PIC Perf board shown below.

We will also need a potentiometer to feed in the analog voltage, I have attached some female end wires to my pot (shown below) so that we can directly connect them to the PIC Perf board.

Finally to verify the output we need a RC circuit and a LED to see how the PWM signal works, I have simply used a small perf board and soldered the RC circuit and the LED (to control brightness) on to it as shown below

We can use simple female to female connecting wires and connect them according to the schematics shown above. Once the connection is done, upload the program to the PIC using our pickit3 and you should be able to get a variable voltage based on the input of your potentiometer. The variable output is used to control the brightness of the LED here.

I used my multimeter to measure the variable outputs, we can also notice the brightness of the LED getting changed for different voltage levels.

That’s it we have programmed to read the Analog voltage from the POT and convert into PWM signals which in turn have been converted into Variable voltage using RC filter and the result is verified using our hardware. If you have some doubt or get stuck somewhere kindly use the comment section below, we will be happy to help you out. The complete working is working in the video.

Comments

Hi sir,
I am electrical engineering and I need more simple practical circuits about (delay time cct) in my work (electronic change over equipment) so as to make more safity in the work
Asimple practical cct please
And I would like to thank you very much about all these informatios
thank you very much
Arkan M R .

Hi. I am writing a PWM code for PIC18f4550 micrcocontroller. I am using MPLAB X IDE with XC8. I am referring to your code to write a simple PWM code to test it on oscilloscope and later on build on it to do more things. I don't understand what are:
CCP1X = duty & 1; //Store the 1st bit
CCP1Y = duty & 2; //Store the 0th bit

As you can see the variable duty contains the value of the PWM duty cycle. This va;ue has to be used to set the CCP1X and CCP1Y bit and also the CCPR1L register.

The following lines are from the datasheet

The PWM duty cycle is specified by writing to the
CCPR1L register and to the CCP1CON<5:4> bits. Up
to 10-bit resolution is available. The CCPR1L contains
the eight MSbs and the CCP1CON<5:4> contains the
two LSbs.

CCP1CON<5:4> is nothing but the CCP1X and CCP1Y bits respectively. The value of duty after this formulae(shown below) can be upto 10-bit resolution

duty = ((float)duty/1023)*(_XTAL_FREQ/(PWM_freq*TMR2PRESCALE));

Since the Registers in our PIC MCU can hold only upto 8-bits(PIC16F877A is a 8-bit MCU) the additional two bits (CCP1X and CCP1Y) are used to store the least two significant bits and the remaining 8 bits are stored into the CCPR1L register.

I was just wondering about the order of bit masking for the 0th and the 1st bit of the duty cycle to store in the CCP1X and CCP1Y locations. So actually, CCP1X should be storing the 1st bit of duty cycle and the CCP1Y should be containing the 0th bit right and it is commented like that also. I am pasting the code below for reference

I am not sure how you have interfaced the fan or bulb with the PIC MCU. Let me know what your driver circuit is. If you have used a relay it will not work. Hence make sure you use a power electronic device like MOSFET and you switch it properly using the PWM signals.

Here the data type of duty is integer. So when this integer value is devided by 1023 it will also be an integer. But we need it in terms of decimal for better accuracy. So we prefix (float) to ask the compilernot to truncate the obtained result to interger but keep it in float as such.

please give me an example of this... am a bit confuse.. from PWM_Duty(adc_value); i understand that, voltage from 0-5v will also be converted from 0-1023
take for instance adc_value=1020 which in our duty formulae has been divided by 1023.duty=((float)duty/1023) saying (1020/1023)=0.9971 which will result everything to be zero.. so please explain to me how (float) works in that code with an example of duty result between 0-5v.. Thank you!(i want to move to the next tutorial after getting full understanding of this)

Yes, it is very much possible. Here in this code above. The variable adc_value controls the duty cycle of the output PWM signal. You simply have to connect two push buttons and use some If statements to check if they are pressed. If pressed simply increment/decrement the value of adc_value. note the range of this variable should only be from 0-1024

Hi,
I am using a PIC16F873A with a variation of your code that uses a for() loop to increase and decrease the duty cycle. I am using a LED component that effectively has RGB LED's, connected to PORTC 1, 2, and 3. Is there any way of changing which pin the pwm signal outputs on? have tried altering the TRISC2=0 to TRIS1=0 etc to no avail. is it possible to output the signal on multiple pins at the same time?

Hello!
Never have I used PWM signals before so it's kinda tricky to me. I am using PIC18F8722 and am supposed to build a function with the following structure : void config_PWM(int en, int ch, int freq, int period, int c_duty) . //en=enable the module, ch=PWM output channel, freq=osc. freq., period=PWM period(0-127), c_duty=cycle duty(0-1024) .
All the information here has been really helpful, I am just having difficulties in putting all this stuff together within one function. If you could please land me a hand I would be very grateful, since I won't have access to the microcontroller til next week. Thank you!