PIC18F45K22 PWM

(instructions, reset, WDT, specifications...) PIC17Cxx, PIC18Fxxx

PIC18F45K22 PWM

Postby SLTom992 » Mon Jun 30, 2014 2:45 pm

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.
SLTom992
 
Posts: 58
Joined: Tue Jun 10, 2014 8:59 pm
PIC experience: Professional 1+ years with MCHP products

Re: PIC18F45K22 PWM

Postby vloki » Mon Jun 30, 2014 3:02 pm

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;
        }
    }
}
vloki
Verified identity
 
Posts: 186
Joined: Wed May 28, 2014 8:42 am
Location: Germany
PIC experience: Professional 5+ years with MCHP products

Re: PIC18F45K22 PWM

Postby SLTom992 » Mon Jun 30, 2014 4:21 pm

It appears to me that we're doing the same thing except you're using interrupts and I'm free running.
SLTom992
 
Posts: 58
Joined: Tue Jun 10, 2014 8:59 pm
PIC experience: Professional 1+ years with MCHP products

Re: PIC18F45K22 PWM

Postby vloki » Mon Jun 30, 2014 6:15 pm

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 ...
vloki
Verified identity
 
Posts: 186
Joined: Wed May 28, 2014 8:42 am
Location: Germany
PIC experience: Professional 5+ years with MCHP products

Re: PIC18F45K22 PWM

Postby Tom Maier » Mon Jun 30, 2014 7:37 pm

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.
User avatar
Tom Maier
Verified identity
 
Posts: 179
Joined: Mon May 26, 2014 2:37 pm
PIC experience: Professional 5+ years with MCHP products

Re: PIC18F45K22 PWM

Postby SLTom992 » Wed Jul 02, 2014 4:47 pm

Tom - I'm using the latest MPLAB X and the configuration bits are good. Though that should have occurred to me to check.
SLTom992
 
Posts: 58
Joined: Tue Jun 10, 2014 8:59 pm
PIC experience: Professional 1+ years with MCHP products

Re: PIC18F45K22 PWM

Postby Tom Maier » Wed Jul 02, 2014 5:09 pm

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

------------------------------------------------------------
User avatar
Tom Maier
Verified identity
 
Posts: 179
Joined: Mon May 26, 2014 2:37 pm
PIC experience: Professional 5+ years with MCHP products

Re: PIC18F45K22 PWM

Postby SLTom992 » Wed Jul 02, 2014 7:35 pm

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
SLTom992
 
Posts: 58
Joined: Tue Jun 10, 2014 8:59 pm
PIC experience: Professional 1+ years with MCHP products

Re: PIC18F45K22 PWM

Postby Tom Maier » Thu Jul 03, 2014 12:25 am

Have you tried the "Configurator" plugin?
User avatar
Tom Maier
Verified identity
 
Posts: 179
Joined: Mon May 26, 2014 2:37 pm
PIC experience: Professional 5+ years with MCHP products

Re: PIC18F45K22 PWM

Postby SLTom992 » Thu Jul 03, 2014 2:12 am

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.
SLTom992
 
Posts: 58
Joined: Tue Jun 10, 2014 8:59 pm
PIC experience: Professional 1+ years with MCHP products

Next

Return to 16-Bit Core

Who is online

Users browsing this forum: No registered users and 23 guests

cron