The port direction is set by assigning a value to TRIS*, where * is the port letter. For example:
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.
The port can be treated as a variable. For example:
The above assigns the value of PORTA to the variable x.
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 STATEthen
Combining these examples, we have:
Example 5.9. Setting and testing pin state
TRISA = 0 TRISB = 255if
PORTA.3 ishigh
then
{ PORTB.5 =low
}else
{ PORTB = PORTA + 15 }