#include #pragma DATA _CONFIG1H, _OSC_HS_1H //10 MHz #pragma DATA _CONFIG2H, _WDT_OFF_2H #pragma DATA _CONFIG4L, _LVP_OFF_4L #pragma DATA _CONFIG3H, _MCLRE_ON_3H #pragma CLOCK_FREQ 10000000 void main (void) { //1. set the pwm period by writing to the PR2 register = 0x9C (hex) pr2 = 156; //2. set the pwm duty cycle by writing to the CCPRxL register and CCPxCON<5:4> bits //in this case CCPR1L & CCP1CON (with 40% duty cycle); PWM Duty Cycle = 4.0192e-4 s ccpr1l = 125 >> 2; ccpr2l = 376 >> 2; //ccpr1l = 00011111b; //for 20% duty cycle //ccpr2l = 01011110b; //for 60% duty cycle //writing CCP1CON ccp1con.5 = 0; ccp1con.4 = 1; //5. configure the CCPx module for PWM operation (for PWM mode: 11xx) ccp1con.3 = 1; ccp1con.2 = 1; ccp1con.1 = 0; ccp1con.0 = 0; //writing CCP2CON ccp2con.5 = 0; ccp2con.4 = 0; //5. configure the CCPx module for PWM operation (for PWM mode: 11xx) ccp2con.3 = 1; ccp2con.2 = 1; ccp2con.1 = 0; ccp2con.0 = 0; //3. make the CCPx pin an output by clearing the appropriate tris bit trisc.2 = 0; //the output of CCP1 is c2, not c1 trisc.1 = 0; //the output of CCP2 is c1 //4. set the TMR2 prescale value, then enable Timer2 by writing to T2CON //TMR2 = 16 (00 = 1; 01 = 4; 1x = 16) t2con.1 = 1; //prescale value t2con.0 = 0; //prescale value t2con.2 = 1; //turning on timer2 while (1) {} }