Microchip Technology XC8 Standard Compiler (Workstation) SW006021-1 SW006021-1 User Manual

Product codes
SW006021-1
Page of 518
C Language Features
 2012 Microchip Technology Inc.
DS52053B-page 137
5.3.6
Using SFRs From C Code
The Special Function Registers (SFRs) are registers which control aspects of the MCU 
operation or that of peripheral modules on the device. Most of these registers are mem-
ory mapped, which means that they appear at, and can be accessed using, specific 
addresses in the device’s data memory space. Individual bits within some registers 
control independent features. Some registers are read-only; some are write-only. See 
your device data sheet for more information.
Memory-mapped SFRs are accessed by special C variables that are placed at the 
address of the register. (Variables that are placed at specific addresses are called 
absolute variables and are described in Section 5.5.4 “Absolute Variables”.) These 
variables can be accessed like any ordinary C variable so that no special syntax is 
required to access SFRs.
The SFR variables are predefined in header files and are accessible once you have 
included the <xc.h> header file (see Section 5.3.3 “Device Header Files”) into your 
source code. Both bit variables and structures with bit-fields are defined, so you may 
use either of them in your source code to access bits within a register.
The names given to the C variables that map over registers and bits within those reg-
isters are based on the names specified in the device data sheet. However, as there 
can be duplication of some bit names within registers, there may be differences in the 
nomenclature.
The names of the structures that hold the bit-fields will typically be those of the corre-
sponding register followed by bits. For example, the following shows code that 
includes the generic header file, clears PORTA as a whole, sets bit 0 of PORTA using 
a bit variable and sets bit 2 of PORTA using the structure/bit-field definitions.
#include <xc.h>
void main(void)
{
PORTA = 0x00;
RA0 = 1;
PORTAbits.RA2 = 1;
}
To confirm the names that are relevant for the device you are using, check the 
device-specific header file that <xc.h> will include for the definitions of each variable. 
These files will be located in the include directory of the compiler and will have a 
name that represents the device. There is a one-to-one correlation between device and 
header file name that will be included by <xc.h>, e.g., when compiling for a 
PIC16LF1826 device, <xc.h> will include the header file <pic16lf1826.h>. 
Remember that you do not need to include this chip-specific file into your source code; 
it is automatically included by <xc.h>.
Care should be taken when accessing some SFRs from C code or from assembly inline 
with C code. Some registers are used by the compiler to hold intermediate values of 
calculations, and writing to these registers directly can result in code failure. The com-
piler does not detect when SFRs have changed as a result of C or assembly code that 
writes to them directly. The list of registers used by the compiler and further information 
can be found in Section 5.7 “Register Usage”.
SFRs associated with peripherals are not used by the compiler to hold intermediate 
results and can be changed as you require. Always ensure that you confirm the oper-
ation of peripheral modules from the device data sheet.