3.4.9 How Do I Setup the Oscillator in My Code?

All PIC devices have several oscillator modes that must be selected by programming the device’s configuration bits in your project. See your device data sheet for information on the modes and 5.2.4 Configuration Bit Access for assistance with programming the configuration bits.

Some devices have an OSCCON register, which further controls such runtime attributes as clock sources and internal clock frequencies. In C source, this register can be written to in the usual way, based on information in your device data sheet.

Some devices allow the internal oscillator to be tuned at runtime via the OSCTUNE register. Other devices allow for calibration of their internal oscillators using values pre-programmed into the device. The runtime startup code generated by the compiler, (see 5.10.2 Runtime Startup Code), will by default provide code that performs oscillator calibration. This can be disabled, if required, using an option (see 4.6.1.15 Osccal Option).

If you intend to use some of the compiler’s built-in delay functions, you will need to set the _XTAL_FREQ macro, which indicates the system frequency to the compiler. This macro in no way affects the operating frequency of the device (see, for example 9.2.14 __delay_ms Builtin).