TRISC2 = 0 // make port pin on C as output T2CKPS0 = 1 T2CKPS1 = 0 TMR2ON = 1 //Configure the Timer module PR2 = (_XTAL_FREQ/(PWM_freq*4*TMR2PRESCALE)) - 1 //Setting the PR2 formulae using Datasheet // Makes the PWM work in 5KHZĬCP1M3 = 1 CCP1M2 = 1 //Configure the CCP1 module 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. There are two important functions in this program to generate PWM signals.
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.
#DOES MPLAB IDE V5.10 HAVE 1 WIRE LIBRARY HOW TO#
PWM module also uses timers to set its frequency hence learn how to use timers beforehand here. To vary the duty cycle we are using a potentiometer, hence it is recommended to learn ADC tutorial before starting with PWM. Here, we will generate a PWM of 5 kHz with a variable duty cycle from 0% to 100%. 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. If you are an absolute beginner, then please visit the complete list of PIC tutorials here and start learning. Till now, we have covered many basic tutorials like LED blinking with PIC, Timers in PIC, interfacing LCD, interfacing 7-segment, ADC using PIC etc. This is our 10th tutorial of Learning PIC microcontrollers using MPLAB and XC8.