1.4 Port Access

Digital I/O device pins may be multiplexed with peripheral I/O pins. To ensure that you are using digital I/O only, disable the other peripheral(s). Do this by using the predefined C variables that represent the peripheral registers and bits. These variables are listed in the device-specific header file in the compiler include directory. To determine which peripherals share which pins, refer to your device data sheet.

For the example in this section, Port A pins are multiplexed with peripherals that are disabled by default. By default the port pins are analog, so you must set them to digital I/O. For Port A:

ANSELA = 0x0; // set to digital I/O (not analog)

A device pin is connected to either a digital I/O port (PORT) or latch (LAT) register in the device. For the example, LATA is used. For Port A:

LATA = portValue; // write to port latch RA [4:7] = LED[2:5], RA7 CLKIN if EXTOSC enabled

In addition, there is a register for specifying the directionality of the pin - either input or output - called a TRIS register. For the example in this section, TRISA is used. Setting a bit to 0 makes the pin as output, and setting a bit to 1 makes the pin as input. For Port A:

TRISA = 0x0; // set all port bits to be output