14.4.1 Data Register
PORTx is a bidirectional port, and its corresponding data direction
register is TRISx. Setting a TRISx bit (‘1
’) will
make the corresponding PORTx pin an input (i.e., disable the output driver). Clearing a
TRISx bit (‘0
’) will make the corresponding PORTx
pin an output (i.e., it enables output driver and puts the contents of the output latch on
the selected pin). The example below shows how to initialize PORTA.
Reading the PORTx register reads the status of the pins, whereas writing to it will write to the PORT latch. All write operations 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 (LATx).
The PORT data latch LATx holds the output port data and contains the latest value of a LATx or PORTx write. The examples below show how to initialize PORTA.
Initializing PORTA in assembly
; This code example illustrates initializing the PORTA register. ; The other ports are initialized in the same manner. BANKSEL PORTA ; CLRF PORTA ;Init PORTA BANKSEL LATA ;Data Latch CLRF LATA ; BANKSEL ANSELA ; CLRF ANSELA ;digital I/O BANKSEL TRISA ; MOVLW B'00111000' ;Set RA[5:3] as inputs MOVWF TRISA ;and set RA[2:0] as outputs
Initializing PORTA in C
// This code example illustrates initializing the PORTA register. // The other ports are initialized in the same manner. PORTA = 0x00; // Clear PORTA LATA = 0x00; // Clear Data Latch ANSELA = 0x00; // Enable digital drivers TRISA = 0x38; // Set RA[5:3] as inputs and set others as outputs