4.2.6 Using SFRs From C Code

The Special Function Registers (SFRs) are memory mapped registers that can be accessed from C programs. Each register can be accessed using a macro that is available once you include <xc.h>. For example:

#include <xc.h>
if(EEDR == 0x0)
  PORTA = 0x55;

Bits within SFRs can be accessed via a special macro, _BV(), and other macros which represent the bit you wish to access. For example, to set bit #1 in PORTB, use the following.

PORTB |= _BV(PB1);

To clear both bits #4 and #5 in EECR, use the following.

EECR &= ~(_BV(EEPM4) | _BV(EEPM5));

In both these examples, the compiler will use the device’s single bit set and clear instructions whenever possible.