PIC I/O

Port Direction

The port direction is set by assigning a value to TRIS*, where * is the port letter. For example:

Example 5.7. Setting port directions

TRISB = b'01111001'

The above sets pins RB1, RB2 and RB7 on PORTB as outputs, and the other pins on PORTB as inputs. In this example, b'01111001' is a binary representation of the output type. The 1 on the right represents an output on RB0, and the 0 on the left represents an input on RB7.

Port I/O

The port can be treated as a variable. For example:

Example 5.8. Writing to a port

x = PORTA

The above assigns the value of PORTA to the variable x.

Pin I/O

Each pin on a port is obtained by prefixing the pin number by the port name; e.g. Pin 2 (starting from Pin 0) on PORTA is known as PORTA.0. The syntax to set a pin state is:

PORTX.N = STATE

where STATE can be high or low. The syntax to test a pin state is:

if PORTX.N is STATE then

Combining these examples, we have:

Example 5.9. Setting and testing pin state

TRISA = 0
TRISB = 255
if PORTA.3 is high then
{
	PORTB.5 = low
}
else
{
	PORTB = PORTA + 15
}