© 2004 Microchip Technology Inc. DS70058C-page 11-3
Section 11. I/O Ports
I/O Ports
11
11.2 I/O Port Control Registers
All I/O ports have three registers directly associated with the operation of the port, where ‘x’ is a
letter that denotes the particular I/O port:
• TRISx: Data Direction register
• PORTx: I/O Port register
• LATx: I/O Latch register
Each I/O pin on the device has an associated bit in the TRIS, PORT and LAT registers.
11.2.1 TRIS Registers
The TRISx register control bits determine whether each pin associated with the I/O port is an
input or an output. If the TRIS bit for an I/O pin is a ‘1’, then the pin is an input. If the TRIS bit for
an I/O pin is a ‘0’, then the pin is configured for an output. An easy way to remember is that a ‘1’
looks like an I (input) and a ‘0’ looks like an O (output). All port pins are defined as inputs after a
Reset.
11.2.2 PORT Registers
Data on an I/O pin is accessed via a PORTx register. A read of the PORTx register reads the
value of the I/O pin, while a write to the PORTx register writes the value to the port data latch.
Many instructions, such as BSET and BCLR instructions, are read-modify-write operations.
Therefore, a write to a port implies that the port pins are read, this value is modified, and then
written to the port data latch. Care should be taken when read-modify-write commands are used
on the PORTx registers, when some I/O pins associated with the port are configured as inputs.
If an I/O pin configured as an input is changed to an output at some later time, an unexpected
value may be output on the I/O pin. This effect occurs because the read-modify-write instruction
reads the instantaneous value on the input pin and loads that value into the port data latch.
11.2.3 LAT Registers
The LATx register associated with an I/O pin eliminates the problems that could occur with
read-modify-write instructions. A read of the LATx register returns the values held in the port
output latches, instead of the values on the I/O pins. A read-modify-write operation on the LAT
register, associated with an I/O port, avoids the possibility of writing the input pin values into the
port latches. A write to the LATx register has the same effect as a write to the PORTx register.
The differences between the PORT and LAT registers can be summarized as follows:
• A write to the PORTx register writes the data value to the port latch.
• A write to the LATx register writes the data value to the port latch.
• A read of the PORTx register reads the data value on the I/O pin.
• A read of the LATx register reads the data value held in the port latch.
Any bit and its associated data and control registers that are not valid for a particular device will
be disabled. That means the corresponding LATx and TRISx registers, and the port pin, will read
as zeros.
Note: The total number of ports and available I/O pins will depend on the device variant.
In a given device, all of the bits in a port control register may not be implemented.
Refer to the specific device data sheet for further details.