Finding the Names of Device Registers

Last modified by Microchip on 2024/02/08 11:24

The following tip reliably confirms the names you should use for special function registers in C code. This is useful since the C identifiers used to access registers might not always be identical to those shown in the datasheet.

Create a trivial test program which includes <xc.h>. For example, main.c:

1
2
3
4
5
#include <xc.h>

int main(void)
{
}

Compile the program above on the command line for your device with a command such as:
xc8 --chip=18f4520 --pre main.c if you are using MPLAB® XC8.

If you are using MPLAB XC16 or XC32, the command might look like this:
xc32-gcc -mprocessor=32MX795F512L -E -o main.i main.c.

These commands use the --PRE or -E options to stop compilation after the preprocessing stage so that you will be able to examine the pre-processed output file (main.pre or main.i, respectively for this example). This output file will contain the definitions for all the device-specific special function registers and bits within those registers.

Using this technique means you do not need to find the relevant header files for your device and determine if conditional code is appropriate for your build. However, if you would like a list of all the files included, do the following:
For XC8 compile as above, but replace the --PRE with --DEP=list,sys,stop. For XC16 or XC32, replace -E with -MD. These options will create, for example, a main.dep or main.d dependency file which will list the full path of all the headers that were included in your source.