Configuration Bits for 16-bit PIC® MCUs

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

Configuration Bits are a collection of binary data located in the Flash program memory of a PIC® microcontroller (MCU). Configuration bits are programmed into the PIC MCU with the application code. These bits are not executable code as their addresses are not accessible by the program counter. Configuration bits are derived from compiler directives placed into the code by you, the application developer.

Configuration bits are read by the MCU when exiting a reset and cannot be modified during run-time. Upon exiting reset, the configuration bits are used to complete circuitry which enables or disables hardware features of the MCU.

The Special Features of MCU operations controlled by the configuration bits include:

  1. System Clocking
  2. Power Management
  3. Device Security
  4. Operating Characteristics

The configuration bits and settings vary across devices due to different feature sets. Check your datasheet for the specifics of the PIC MCU configuration bits you are using.

This page describes what features are controlled by configuration bits and how to generate them in source code.

Location and Format

The configuration bits for the PIC24/dsPIC33 MCU family are combined into two 24-bit words called CONFIG1 and CONFIG2. The configuration words are located beyond the reach of the program counter in the upper half of the MCU's program memory space (Configuration Memory Space) starting at 0xF80000 as shown in the accompanying image:

Map of Program Memory on PIC24 Showing Location of Configuration Registers

Configuration bits are generated by compiler directives inserted into the application's source code. When a PIC MCU project is built, the configuration bit settings are embedded in the HEX output file. The configuration bits are programmed into the PIC along with the application program.

The following CONFIG1 & CONFIG2 registers are defined for the PIC24FJ128GA010 MCU:

CONFIG1 Register Bit Definitions


CONFIG2 Register Bit Definitions

Detailed descriptions of the PIC24FJ128GA010 MCU's configurable hardware options are provided in the "Special Features" section of the device PIC24FJ128GA010 Family Datasheet.

Generating Configuration Bits in C Code

Microchip's MPLAB® XC16 C compiler accepts #pragma directives to set the configuration bits.

​The syntax for generating configuration bits:
#pragma config CONFIG_BIT_NAME = CONFIG_VALUE

Sample of Setting Configuration Bits in C

#include <xc.h>

#pragma CONFIG FNOSC = PRI
#pragma CONFIG POSCMOD = XT
#pragma CONFIG ICS = PGx2
#pragma CONFIG FWDTEN = OFF

When setting configuration bits using C, it is not necessary to know the word that contains the bit being set.

All that is needed is the configuration bit name and the desired value.

Unspecified configuration options are programmed with MCU defaults (per the datasheet).

The header files for each PIC24/dsPIC33 device contain the CONFIG_BIT_NAME and CONFIG_VALUE.

A full list of the settings for all 16-bit MCUs/DSCs can be found in XC16's documentation folder (C:\Program Files\Microchip\xc16\<version>\docs) in the file config_index.html.

Once written, the compiler directives must be added to the PIC MCU project in one of three ways:

  1. In a stand-alone C source file that is added to the project.
  2. In a header (.h) file placed in the project with an #include statement.
  3. Placed directly inside one of the source code files already in the project.

Developers using the XC16 compiler can refer to the "Configuration Bits" section of the MPLAB X IDE Tutorial to see shortcuts for generating the code needed to set the configuration bits.