PIC16F877A-HITECH C- PWM

Photo of author

By Jackson Taylor

//External clock frequency, FOSC = 16MHz
//clock frequency for TIMER2 =  FOSC/4 = 4MHz
// (in datasheet clock frequency  of timer will be quarter of  clock frequency )
//Here in the program pre-scale & post scale value = 1 (set in T2CON register)
//then time for single excution = 1/4MHz = .25micro seconds
//Output frequency= 0.25micro seconds * PR2
#include<pic.h>
void main()
{
TOUTPS3=0;//Timer2 Output Postscale Select bits
TOUTPS2=0;
TOUTPS1=0;
TOUTPS0=0;
T2CKPS1=0;//Timer2 Clock Prescale Select bits
T2CKPS0=0;
TMR2ON=1; //enables timer 2
CCP1M3=1;//SETS CCP1 MODULE IN PWM MODE
CCP1M2=1;//SETS CCP1 MODULE IN PWM MODE
TRISC2=0; //setting RC2 in output mode mode
PR2=245; //FREQUENCY BITS
CCPR1L=0X7F;//8 msb of pwm duty ratio (Here 50% duty ratio)
CCP1X=1;//2 lsb of pwm duty ratio
CCP1Y=1;
while(1);                                                          
}