Configuration Bit Access

Configuration bits, or fuses, are used to set up fundamental device operation, such as the oscillator mode, watchdog timer, programming mode and code protection. These bits must be correctly set to ensure your program executes correctly.

Use the configuration pragma, which has the following forms, to set up your device.

#pragma config setting = state|value
#pragma config register = value

Here, setting is a configuration setting descriptor, e.g., WDT, and state is a textual description of the desired state, e.g., OFF.

The settings and states associated with each device can be determined from an HTML guide. Open the pic_chipinfo.html file (or the pic18_chipinfo.html file) that is located in the docs directory of your compiler installation. Click the link to your target device and the page will show you the settings and values that are appropriate with this pragma. Review your device data sheet for more information.

The value field is a numerical value that can be used in preference to a descriptor. Numerical values can only be specified in decimal or in hexadecimal, the latter radix indicated by the usual 0x prefix. Values must never be specified in binary (i.e., using the 0b prefix).

Consider the following examples.

#pragma config WDT = ON      // turn on watchdog timer
#pragma config WDTPS = 0x1A  // specify the timer postscale value

One pragma can be used to program several settings by separating each setting-value pair with a comma. For example, the above could be specified with one pragma, as in the following.

#pragma config WDT=ON, WDTPS = 0x1A

It is recommended that the setting-value pairs be quoted to ensure that the preprocessor does not perform macro substitution of these tokens, for example:

#pragma config "BOREN=OFF"

You should never assume that the OFF and ON tokens used in configuration macros equate to 0 and 1, respectively, as that is often not the case.

Rather than specify individual settings, each half of the configuration register can be programmed with one numerical value, for example:

#pragma config CONFIG1L = 0x8F

The config pragma does not produce executable code and ideally it should both be placed outside function definitions.

All the bits in the Configuration Words should be programmed to prevent erratic program behavior. Do not leave them in their default/unprogrammed state. Not all Configuration bits have a default state of logic high; some have a logic low default state. Consult your device data sheet for more information.

If you are using MPLAB X IDE, take advantage of its built-in tools to generate the required pragmas, so that you can copy and paste them into your source code.