Using an Internal OSC with Swordfish

When configuring your device to use an internal oscillator, it is very important to understand how modules are initialised when a PIC is reset. Take a look at the following code

device = 18F1320
clock = 8
config 
   osc = INTIO2

include "USART.bas"
include "EEPROM.bas"
include "Convert.bas"

// program entry point
OSCCON = %01110110
SetBaudrate(br19200)
Write("Program starting...",13,10)
while true
   Write("High",13,10)
   delayms(500)
   Write("Low",13,10)
   delayms(500)
wend 

The above shows how an 18F1320 is configured for an 8MHz internal clock. However, there is a problem. Many Swordfish modules have initialisation code which is executed before the main program block starts to run. The line

OSCCON = %01110110

will not be set until the module initialisation code has completed. For some modules (such as LCD) the initialisation code is quite substantial - OSCCON needs to be set before any module initialisation code is executed.

To ensure this happens, create a module called 'intosc.bas' and place in your 'UserLibrary' folder. For the 18F1320, the module code would look like this

// Internal oscillator module...
module IntOSC
config osc = INTIO2
OSCCON = %01110110

Now you just include this module in your main program, before any other modules to ensure OSCCON is initalised before any other module code. For example,

device = 18F1320
clock = 8
include "IntOSC.bas"
include "USART.bas"
include "EEPROM.bas"
include "Convert.bas"

SetBaudrate(br19200)
Write("Program starting...",13,10)
while true
   Write("High",13,10)
   delayms(500)
   Write("Low",13,10)
   delayms(500)
wend