PIC32MX Oscillator Configuration

Last modified by Microchip on 2023/11/09 09:09

There are two ways to configure the desired oscillator settings:

  • Core Configuration Bits Registers
  • Oscillator Special Function Registers (SFRs)

Configuration Bits Registers

These settings are saved in a special portion of Flash memory and are loaded at the moment power is applied to the microcontroller. By having them instantly available at power-on, we don’t need to waste time and processor cycles setting up the oscillator in our application code.

The configuration bits are defined using #pragma config statements using the MPLAB® C Compiler for PIC32 MCUs.

It is important to first define the signal source for the Primary Oscillator and the target system clock frequency. Following the guidelines in the datasheet, you can then determine the divider values and Phase-Locked Loop (PLL) multipliers required.

The #pragma config statements do not generate any executable code. They define a word that is to be written into the appropriate configuration register address that matches the desired configuration option. This word will be written into the PIC32's Flash memory when the device is programmed and stored in the application's HEX file generated by MPLAB IDE.

To determine which options are allowed with the #pragma config statements, refer to the hlpPIC32MXConfigSet.chm file, and search for the exact PIC32 microcontroller you are using.

// Configuration Bit settings
// SYSCLK = 80 MHz
//(8MHz Crystal/ FPLLIDIV * FPLLMUL / FPLLODIV)
// PBCLK = 40 MHz
// Primary Osc w/PLL (HS+PLL)
// WDT OFF, Peripheral Bus is CPU clock÷8
// Other options are default as per datasheet

#pragma config FPLLMUL = MUL_20
#pragma config FPLLIDIV = DIV_2
#pragma config FPLLODIV = DIV_1, FWDTEN = OFF
#pragma config POSCMOD = HS, FNOSC = PRIPLL
#pragma config FPBDIV = DIV_8

Refer to C:\Program Files\Microchip\xc32\<ver>\docs\PIC32ConfigSet.html for a listing of all #pragma config macros available for your MCU.

Refer to the Special Features section of the MCU datasheet to learn about all the configurable hardware options on the device.

OSCCON and OSCTUN SFRs

During run-time, it is possible to change the clock source, modify the oscillator settings, check the status of the various oscillator sources, and determine whether the PLLs are locked.

The Oscillator Family Reference Manual provides basic C-code examples that show how to configure these registers for specific applications.

Alternatively, you can use the MPLAB Harmony library Application Programming Interfaces (APIs) to dynamically change oscillator configuration.