Skip navigation.
Home
A Touch Of Cloth

HOWTO Get a PIC18F2550 running at 48MHz from a 4MHz Resonator

"To get there, I wouldn't start from here". Let's do something easy first. Run the PIC from its internal clock at 8Mhz. Let's change an LED on RB0 every 0.1sec.

To get the options, pull up a command window and with the C compiler in your path, enter C:\> mcc18 --help-config -p18f2550

That'll tell you all the allowed variables and values for #pragma config. Useful! FOSC is our main one here. #include <delays.h> // Use the internal oscilator and allow RA6 to be used for input. #pragma config FOSC = INTOSCIO_EC // Turn off watch dog, brown out, low voltage programming and enable master clear #pragma config WDT = OFF #pragma config BOR = OFF #pragma config LVP = OFF #pragma config MCLRE = ON // Port B default to digital, saving code #pragma config PBADEN = OFF void main ( void ); void init ( void ); void main ( void ) { init(); PORTB = 0; while ( 1 ) { PORTB++; // 0.1sec @ 8Mhz = 800,000 cycles // 4 cycles per instruction = 200,000 instructions Delay10KTCYx ( 20 ); } } void init ( void ) { // OSCCON<6:4> (internal oscillator frequency) = 111 = 8MHz // OSCCON<1:0>(system clock select) = 1x = internal clock OSCCON = 0xff; // PortB all outputs TRISB = 0; }

Right, now let's attach a 4MHz resonator like below and change the code so it uses the XT on OSC1 and OSC2 and feeds it into the PLL to give 48MHz. Check it by modifying the delay to give the same LED toggle time.

#include <delays.h> // For 4Mhz external clock #pragma config FOSC = XTPLL_XT #pragma config PLLDIV = 1 #pragma config CPUDIV = OSC1_PLL2 // Turn off watch dog, brown out, low voltage programming and enable master clear #pragma config WDT = OFF #pragma config BOR = OFF #pragma config LVP = OFF #pragma config MCLRE = ON void main ( void ); void init ( void ); void main ( void ) { init(); PORTB = 0; while ( 1 ) { PORTB++; // 0.1sec @ 48Mhz = 4,800,000 cycles // 4 cycles per instruction = 1,200,000 instructions Delay10KTCYx ( 120 ); } } void init ( void ) { // OSCCON<1:0> (system clock select) 00 = primary oscillator OSCCON = 0xfC; PORTB = 1; TRISB = 0; }