Microchip Technology SW006022-2N Data Sheet

Page of 338
Device-Related Features
 2012 Microchip Technology Inc.
DS52071B-page 87
4.6
USING SFRS
The Special Function Registers (SFRs) are registers which control aspects of the MCU 
operation or that of peripheral modules on the device. These registers are device 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. These variables can be accessed like any ordinary C variable 
so that no special syntax is required to access SFRs.
The SFR variable identifiers are predefined in header files and are accessible once you 
have included the <xc.h> header file (see Section 4.3 “Device Header Files”) into 
your source code. Structures with bit-fields are also defined so you may access bits 
within a register in your source code.
A linker script file for the appropriate device must be linked into your project to ensure 
the SFR variable identifiers are linked to the correct address. MPLAB IDE will link in a 
default linker script, but a linker script file must be explicitly specified if you are driving 
the command-line toolchain. Linker scripts have a .gld extension (e.g. 
p30F6014.gld)
 and basic files are provided with the compiler.
The convention in the processor header files is that each SFR is named, using the 
same name that appears in the data sheet for the part – for example, CORCON for the 
Core Control register. If the register has individual bits that might be of interest, then 
there will also be a structure defined for that SFR, and the name of the structure will be 
the same as the SFR name, with “bits” appended. For example, CORCONbits for the 
Core Control register. The individual bits (or bit fields) are named in the structure using 
the names in the data sheet – for example PSV for the PSV bit of the CORCON 
register.
Here is the complete definition of CORCON (subject to change):
/* CORCON: CPU Mode control Register */
extern volatile unsigned int CORCON 
__
attribute
_ _
((
_ _
sfr
_ _
));
typedef struct tagCORCONBITS {
  unsigned IF      :1;    /* Integer/Fractional mode             */
  unsigned RND     :1;    /* Rounding mode                       */
  unsigned PSV     :1;    /* Program Space Visibility enable     */
  unsigned IPL3    :1;
  unsigned ACCSAT  :1;    /* Acc saturation mode                 */
  unsigned SATDW   :1;    /* Data space write saturation enable  */
  unsigned SATB    :1;    /* Acc B saturation enable             */
  unsigned SATA    :1;    /* Acc A saturation enable             */
  unsigned DL      :3;    /* DO loop nesting level status        */
  unsigned         :4;
} CORCONBITS;
extern volatile CORCONBITS CORCONbits _ _attribute_ _((_ _sfr_ _));
See MPLAB Assembler, Linker and Utilities for PIC24 MCUs and dsPIC DSCs User’s 
Guide (DS51317)
 for more information on using linker scripts.
 For example, the following is a sample real-time clock. It uses an SFR, e.g. TMR1, as 
well as bits within an SFR, e.g. T1CONbits.TCS. Descriptions for these SFRs are 
found in the p30F6014.h file (this file will automatically be included by <xc.h> so you 
do not need to include this into your source code). This file would be linked with the 
device specific linker script which is p30F6014.gld.
Note:
The symbols CORCON and CORCONbits refer to the same register and will 
resolve to the same address at link time.