0
votes

I'm trying to make a PWM output with this code, but apparently something is missing. Because when I check the Logic Analyzer in MPLab, nothing happens on CCP2 output. I'm working with the pic18f25k80.

void main() {

    // Set up PWM
    CCP2CON = 0b00001100;       // Enable PWM on CCP2, bits 5:4 are LSB part of duty cycle
    CCPTMRS = 0b00000000;       // Use timer2 for all CCP modules
    CCPR2L = 31;                // MSB part of duty cycle
    TRISC = 0b00000000;         // Set port C as output
    PORTC = 0;                  // Clear port C

    // Set Up timer2
    PR2 = 249;                  // PWM period = (PR+1)*4*Tcy = 1ms
    T2CON = 0b00000100;         // Enable TMR2 with prescaler = 1

    while(1)
    {
    }
}

I expect when

  • TMR2 = PR2, CCP2 output toggles and timer is reset

  • further, TMR2 = CCPR2L (duty cycle), CCP2 output toggles

  • TMR2 keeps counting until step 1 is reached.

    I suppose this is what should happen automatically. I mean I don't have to write the code for that, because that's THE function of the PWM module, right? What am I missing?

1
Yes, that's the idea. I can't spot any obvious issues with your code, though admittedly there are often little gotchas squirreled away in a corner of the datasheet. Have you tried toggling the pin manually to see whether you get an output on the scope, does TMR2 increment when you step the debugger? If that works then my first suspicious would be a bug in the logic analyzer. These simulators are notoriously dodgy it's likely that things work just fine on real hardwaredoynax

1 Answers

0
votes

Additional info:

  • TMR 2 is counting.
  • When I add PORTC = 0xFF; in the while loop and debug the code again. All signals on Port C are set, except RC2 (RC2 = corresponding output of CCP2).
  • When I try the same code for CCP3, all signals on port C are set, except RC2 and RC6 (RC6 = corresponding output of CCP3).
  • When I replace PORTC = 0xFF; with PORTCbits.CCP2 = 1;, only RC1 is set high.

Does this last bullet mean that the CCP2 is muxed with RC1 instead of RC2?