15 Receiving Data using the UART Module

To enable the receiver part of the UART module with protocol support, the Receive Enable (RXEN) bit of the UxCON0 register must be set. The MODE[3:0] bits should be programmed with any of the configurations between ‘0000’ and ‘0011’ depending on the application. The Baud Rate Generator (BRG) must also be configured using the UxBRG register and the BRGS bits of UxCON0, to match the baud rate of the other device. Data is received via the RX pin of the microcontroller, which can be remapped using PPS by writing the code for the desired input pin to the RxyPPS register. The UART module must then be enabled by setting the ON bit of UxCON1.

After configuring the UART module as a receiver, when a received byte of data is transferred from the Receive Shift register to the Receive Buffer (UxRXB) register, the UART Receiver Interrupt Flag (UxRIF) bit becomes set. If the UART Receiver Interrupt Enable (UxRXIE) bit has been set, an interrupt will be generated any time the UxRXB register contains received data. The data received and contained within the UART receive buffer must be read from the UxRXB register and the UxRXIF bit should be cleared before more data can be received. The UART Error Interrupt Flag register can be used to monitor if any errors were detected during the data transfer. Example 15-1 demonstrates the initialization of the UART module as a receiver.

UART Receive Mode Configuration

void UART_Initialize(){
    U1CON0 = 0x90;              // BRGS normal; 8-bit mode; RXEN enabled; 
                                // TXEN disabled
    U1CON2 = 0x00;              // TXPOL not inverted; FLO off;
    U1BRGL = 0x19;              // BRGL 25 = 9600 BR @ 1 MHz FOSC
    U1BRGH = 0x00;
    U1FIFO = 0x00;              // STPMD in middle of first Stop bit;
    U1UIR = 0x00;               // Auto-baud not enabled
    U1ERRIR = 0x00;
    U1ERRIE = 0x00;
    RC6PPS = 0x13;              // UART PPS Settings, RC6->UART1:TX1;    
    U1RXPPS = 0x17;             // UART PPS Settings, RC7->UART1:RX1;  
    U1CON1 = 0x80;              // ON enabled;  
}