7.4 Using SFRS From C Code

The Special Function Registers (SFRs) are registers that control aspects of the MCU, or that of peripheral modules, on the device. These registers are memory-mapped, which means that they appear at specific addresses in the device memory map. With some registers, the bits within the register control independent features.

The SFRs may be read or modified using a C language interface. The SFR interface definitions are accessible by including the <xc.h> header file in your source code (see 7.2 Device Header Files).

The names used in the C interface for SFRs and SFR fields/bits are based on the names specified in the device data sheet. Each peripheral component's registers are accessed through a fixed-base address defined as, for example, WDT_BASE_ADDRESS, which is the address of a structure containing all memory-mapped registers for the component, for example WDT_CR.

Multiple ways are provided to work with fields or bits of each SFR. The register field in the component structure can reference the complete register value, or individual bits or fields of the register by name. Macro definitions are also provided to allow accessing individual fields using bit operations. 7.4.1 SFR Register Definitions provides more information on the SFR interface.

The <xc.h> header will include a device-specific header file for the device you are using. These device-specific headers can be found in architecture-specific directories under the pic32c/include/proc directory of your compiler distribution; however they should not be explicitly included into your code, as this will reduce portability.

To check the interface(s) for SFRs for the device you are using, inspect the headers in the component subdirectory of the device-specific header directory. These headers are automatically included by the device-specific header file and named based on the component names in the device data sheet, e.g., wdt.h. Remember that no device-specific headers need to be included directly into your source code – including the <xc.h> header will ensure all required headers are included.

In additional to peripheral modules, SFRs controlling aspects of the MCU are also accessible through the same interface. Core SFRs are defined in header files automatically included by all device-specific header files, so including <xc.h> is sufficient to allow access to all core SFRs. Core-specific header files are located in the pic32c/include directory.