PIC18 PWM Setup Problems
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

PIC18 PWM Setup Problems

by stumpylog on Mon Feb 11, 2013 3:34 am

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 | TOGGLE FULL SIZE
    // 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

Posts: 1
Joined: Mon Feb 11, 2013 2:48 am

Re: PIC18 PWM Setup Problems

by zener on Tue Feb 12, 2013 4:41 pm

You need to post the smallest code that demonstrates the problem. I don't think those last 4 lines have anything to do with the PWM. It would be helpful (to me) if you seperate your timer setup section from your PWM setup section, and make your comments very explicit.

Also, show all of your other configuration stuff like watchdog settings etc.

Posts: 3720
Joined: Sat Feb 21, 2009 2:38 am

Please be positive and constructive with your questions and comments.