Code: Select all
// Tosc = 1/8 Mhz = 0.000000125
// PWM Period = 4 x 0.000000125 x 101 x 4 = 0.000202
// PWM Frequency = 1/PWM Period = 1/0.000202 = 4.950 kHz
TRISCbits.RC2 = 1; //set TRIS bit
CCPTMRS0 = 0x00; //select timer resources, Timer2 for CCP1 and all others, page 208
CCPTMRS1 = 0x00;
PR2 = 0x65; // load with PWM period value (fixed at 4.90 kHz)
CCP1CON = 0b00111100; // setup for PWM mode 5:4 are PWM Duty Cycle LSB
CCPR1L = 0b00001111; // eight bits of duty cycle, only lower 6 used (8 bit resolution)
PIR1bits.TMR2IF = 0; // clear interrupt flag
T2CON = 0b00000101; // Timer2 On, 1:1 Post, 4x prescale
while (!PIR1bits.TMR2IF); // wait until interrupt set
TRISCbits.RC2 = 0; // clear TRIS bit