Swordfish Code Snippet - Internal Oscillator and PLL


Using the internal oscillator that is available on many PIC's decreases the external component count by 3 (removing the crystal and two capacitors). Most 18F PIC's that end with 0 have an internal oscillator, such as the 18F4520, or the 18F2520.

Most 18F PIC's with internal oscillators can run at 8Mhz, and with PLL enabled, that means its the equivalent of a 32Mhz crystal attached. PLL allows the PIC to execute a new command every oscillation of the crystal, instead of once every forth, effectively quadrupling the speed of the PIC. Now the speed of the PIC is the actual oscillator speed, in this case, 8 MIPS (Million Instructions Per Second).

Ok, with that out of the way, this is how to enable the internal oscillator on most 18F PIC's. (The

Config OSC setting can vary from PIC to PIC)

Device = 18F4520
Clock = 32
Config OSC = INTIO67
OSCCON = %01111100 // Sets up the internal oscillator
OSCTUNE.6 = 1  // Enables PLL


Note - You must define the Clock speed to be 32 not 8! This will ensure the compiler generates the right delays for other functions/procedures

From now on your PIC will be running at 8 MIPS (equiv to 32Mhz without PLL) with no external oscillator components attached. Something to keep in mind is that INTIO67 not only enables the internal OSC, but allows PORTA.6 and PORTA.7 to be used as normal I/O's.

You might be wondering how the PIC can boot up without setting the OSC speed with OSCCON first. Well the answer is that the PIC's internal OSC defaults too 1Mhz on a reset, so long as the above code is right at the start of your program, it will be changed to 8Mhz with PLL enabled in no time.

The internal oscillator does have one draw back, and that is that its not as accurate as an external crystal. This is not an issue for most applications, and the error is that small that you can still send/receive USART information accurately.

Forum Activity

Member Access