Page 1 of 2

PIC18F45K22 PWM

PostPosted: Mon Jun 30, 2014 2:45 pm
by SLTom992
The manual is really pretty sketchy on this product. It should give you step by step instructions. In some places it does and in others such as a PWM it seems to cover it rather sparsely.

What I'm looking for is a simple free running PWM that I can change the frequency of as quickly as possible.

Code: Select all
   
    // Initialize PWM output
    TMR4 = 0x6D;                    // Set for 1600 Hz
    TRISC |= 0x02;                  // Disable CCP output
    CCPTMRS0bits.C1TSEL = 1;     // Use timer 4
    PR4 = 0x6D;                     // Period Register
    CCP4CON = 0x0C;
    CCPR4L = 0x36;                  // Compare register
    T4CON = 0x24;                   // /1 prescaler /5 postscaler TMR on
    TRISC &= ~0x02;                 // Enable CCP output


Can anyone tell me what I'm doing wrong?

The output at pin 35 is low and never moves.

Re: PIC18F45K22 PWM

PostPosted: Mon Jun 30, 2014 3:02 pm
by vloki
One of my demos for our students ...
Code: Select all
void Sound_PWM(void){   //PWM   ###TODO -> Lautstaerke !
    unsigned short frequency = 400;

//--------------------------------------------------------------------- __init()
    LCD_Init();
    LCD_ConstTextOut(0,0,"uC-Quick");
    LCD_ConstTextOut(1,0," Sound ");

    ENC_INT_TRI = INPUT_PIN;    // encoder setup
    ENC_DIR_TRI = INPUT_PIN;

    mENC_IR_CLR();
    mENC_IR_EN();

    CCP1CONbits.CCP1M = 0b1100;                             // PWM mode
    CCP1CONbits.P1M = 0;                                    // single
    CCPTMRS0bits.C1TSEL = 0;                                // CCP1-TMR2
    OpenTimer2(TIMER_INT_OFF & T2_PS_1_16 & T2_POST_1_1);
//    OpenTimer2(TIMER_INT_OFF & T2_PS_1_1 & T2_POST_1_1);
    PR2 = 62500 / frequency;                                // 1MHz:16
    CCPR1L = PR2 >> 1;
    SPEAKER_TRI = OUTPUT_PIN;

    flags.all = 0;

    INTCONbits.GIE = 1;
//----------------------------------------------------------------------- main()
    while(1){
        if(flags.encUp){
            if(frequency < 800) {frequency = frequency + 10;}
              else {frequency = frequency + 100;}
            if(frequency > 15600){frequency = 15600;}
            PR2 = (62500 / frequency) -1;             // 1MHz : 16
            CCPR1L = PR2 >> 1;
            LCD_ConstTextOut(1,0,"      ");      // delete old value
            LCD_ValueOut(1,1,frequency);
            flags.encUp = 0;
        }
        if(flags.encDown){
            if(frequency < 800) {frequency = frequency - 10;}
              else {frequency = frequency - 100;}
            if(frequency < 250) {frequency = 250;}
            PR2 = (62500 / frequency)-1;              // 1MHz : 16
            CCPR1L = PR2 >> 1;
            LCD_ConstTextOut(1,0,"      ");      // delete old value
            LCD_ValueOut(1,1,frequency);
            flags.encDown = 0;
        }
    }
}

Re: PIC18F45K22 PWM

PostPosted: Mon Jun 30, 2014 4:21 pm
by SLTom992
It appears to me that we're doing the same thing except you're using interrupts and I'm free running.

Re: PIC18F45K22 PWM

PostPosted: Mon Jun 30, 2014 6:15 pm
by vloki
SLTom992 wrote:It appears to me that we're doing the same thing except you're using interrupts and I'm free running.

Yes I'm using interrupt for detecting rotary encoder ...

Re: PIC18F45K22 PWM

PostPosted: Mon Jun 30, 2014 7:37 pm
by Tom Maier
If you are using mplabx, you might want to try "Configurator" plugin and see what it comes up with for configuration.

I haven't tried it yet, but it sure sounds good.

You might have to upgrade to the latest mplabx.

Re: PIC18F45K22 PWM

PostPosted: Wed Jul 02, 2014 4:47 pm
by SLTom992
Tom - I'm using the latest MPLAB X and the configuration bits are good. Though that should have occurred to me to check.

Re: PIC18F45K22 PWM

PostPosted: Wed Jul 02, 2014 5:09 pm
by Tom Maier
Check your chip rev level. I think it is displayed while programming the chip.

------------------------------------------------

ERRATA

7. Module: CCP3, CCP4 and CCP5

PWM mode does not work independently of
CCP2. Clock selection is
cross-wired with that of
CCP2.

Work around
Use CCP1 and/or CCP2 for PWM applications.
Reserve CCP3, CCP4 and CCP5 for capture and
compare applications.

Affected Silicon Revisions
A2 A3 A4 A5
X X

------------------------------------------------------------

Re: PIC18F45K22 PWM

PostPosted: Wed Jul 02, 2014 7:35 pm
by SLTom992
You are correct that I got CCP2 and TMR4 confused. I made the changes and still nothing:

Code: Select all
    // Initialize PWM output
    TMR4 = 0x7D ;                   // Set for 1600 Hz
    T4CON = 0x24;                   // /1 prescaler /5 postscaler TMR on
    TRISC |= 0x02;                  // Disable CCP output
    PSTR2CONbits.STR2A = FALSE;     // Port steering clear default
    PSTR2CONbits.STR2B = TRUE;      // Port steering to RB1
    PSTR2CONbits.STRSYNC2 = FALSE;  // Steer cintunuously
    CCPTMRS0bits.C1TSEL = 0x01;     // Use timer 4
    CCP2CONbits.CCP2M = 0b1100;     // PWM Mode
    PR4 = 0x7D;                     // Period Register
    CCPR2L = PR4 >> 1;              // Compare register
    TRISC &= ~0x02;                 // Enable PWM output

Re: PIC18F45K22 PWM

PostPosted: Thu Jul 03, 2014 12:25 am
by Tom Maier
Have you tried the "Configurator" plugin?

Re: PIC18F45K22 PWM

PostPosted: Thu Jul 03, 2014 2:12 am
by SLTom992
What is that?

If you mean the windows/PIC Memory Views/Configuration Bits - yes I have and there doesn't appear to be anything related to this problem in there.The one configuration bit that might be related is the one that switches CCP2 from RC1 to RB3 and it is set correctly.