2
\$\begingroup\$

I'm trying to switch from an external 8MHz clock to the internal 31kHz clock on the PIC18F4550.

this is my code

#include <p18cxxx.h>
#include <delays.h>
#pragma config PLLDIV = 2, CPUDIV = OSC1_PLL2, USBDIV = 2,FOSC = HSPLL_HS, FCMEN =  OFF, 
IESO = ON, PWRT = OFF, BOR = OFF, VREGEN = OFF, WDT = OFF, CCP2MX = ON, PBADEN = OFF, 
LPT1OSC = OFF, MCLRE = ON, STVREN = OFF, LVP = OFF, ICPRT = OFF, XINST = ON, DEBUG = OFF, 
CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF, CPB = OFF, CPD = OFF, WRT0 = OFF, WRT1 = OFF, 
WRT2 = OFF, WRT3 = OFF, WRTC = OFF, WRTB = OFF, WRTD = OFF, EBTR0 = OFF, EBTR1 = OFF, 
EBTR2 = OFF, EBTR3 = OFF, EBTRB = OFF

int main(void) {
    long int i = 0;
    PORTB = 0;
    TRISB = 0;
    PORTB = 0;
    OSCTUNE = 0;//use internal INTRC osciallator for 32kHz clock

while (1){

    while (i++ <100000){
        PORTBbits.RB1 = ~PORTBbits.RB1;//toggle RB1 in external clock mode
    }
    i=0;//reset counter i
    OSCCON = 2;//switch clock to 32khz clock
    PORTBbits.RB5 = 1;//set RB5 high while in 32kHz mode
    while (i++<500){
        PORTBbits.RB1 = ~PORTBbits.RB1;//toggle RB1
    }
    OSCCON = 0;//set clock back to exterl oscillator
    i = 0;//reset counter
    PORTBbits.RB5 = 0;//clear RB5 while in external oscillator mode
}
return 0;

}

i hooked up PORTB to a scope to see if the waveform would change but it didnt, which makes me believe that no clock switching took place. I'm not sure where i'm getting it wrong.

I'm using MPLABX with C18 compiler.

EDITED:

\$\endgroup\$
1
  • \$\begingroup\$ What do you see on PORTC? \$\endgroup\$
    – AngryEE
    Apr 25 '12 at 23:53
2
\$\begingroup\$

Looking at the datasheet (pg32) I'm guessing it's because you're not changing IRFC2:IRFC0 (bits 4-6 in OSCCON). IRFC controls the frequency of the internal oscillator and since you're not changing it the waveform won't change either.

The confusion probably comes from the fact that this PIC has two internal oscillators - high speed (INTOSC) and low speed (INTRC). The PIC can generate the 31KHz clock from either of these oscillators but by default IRFC value (which determines the frequency of the internal oscillator) is 100b which equates to 1MHz and uses the INTOSC oscillator. You need to change that to 000b to configure the frequency to be 31KHz and if you want to ensure that you're using INTRC you also have to set the INTRC bit (OSCTUNE:7) to 1 (datasheet pg. 32 again).

Edit: I now realize that can't completely be the case - if the internal oscillator was set to 1MHz and the external oscillator is indeed 8MHz, then you should see some sort of change on PORTB. Also, you're setting OSCCON to 0x02 which should clear the IRFC bits I mentioned earlier and set the frequency to 31KHz. Very odd. I think your code should work, but it's badly commented and oddly written so it's hard to tell.

Please probe PORTC and tell us what you get. I'm guessing that you expect to see PORTC go high while PORTB's waveform changes frequency, then low again when PORTB goes back to the original waveform. Do you indeed see this? Do you have any graphics of the waveform for us? Also, can you use breakpoints and see if your instructions are ever executed?

\$\endgroup\$
2
  • \$\begingroup\$ Sorry about the "odd" looking code, i've edited it a bit. Yes i get a waveform on PORTB, and a small gitter on PORTC, that didn't seam to make any sense. \$\endgroup\$
    – TiOLUWA
    Apr 26 '12 at 14:46
  • \$\begingroup\$ Thanks for the help, it helped me figure out what was wrong The problem i was having was that the delay between switching was too short for me to see. The loops were counting the same amount of time, but on 8MHz and 31kHz, that it was millions of cycles different, so the output at 8MHz was happening for just a very short period of time, that i couldn't see it on the scope, most of what I was seeing was the output at 31kHz. So i made the loop at 8MHz far greater than that at 31kHz, then i was able to see that the clock was indeed switching. \$\endgroup\$
    – TiOLUWA
    Apr 26 '12 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.