I'm working with a PIC18F25K22 (
http://www.microchip.com/wwwproducts/Devices.aspx?dDocName=en546240) and trying to get the first pwm module working. I'm following the steps outlined in section 14.3.2 of the datasheet, but I get no output from the module, either when programmed, or when using the simulator of MPLAB X. I've posted my code below, and hope someone can provide me with some pointers as to where I'm not setting things right. I've configured the PIC for 8Mhz oscillation.
- 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