3 UART Receive

The following steps outline the reception process:

  1. Configure the UxBRGH:UxBRGL register pair and BRGS bit to achieve the desired baud rate.
  2. Set the MODE<3:0> bits of UxCON0 to the desired operation mode.
  3. Set the RXPOL bit of UxCON2 if an inverted RX input is desired.
  4. Load the RXPPS register with the value associated with the desired RX pin.
  5. Clear the associated ANSEL bit (if applicable).
  6. Set the associated TRIS bit.
  7. If UART interrupts are desired, set the appropriate interrupt enable bits, clear the associated interrupt flag bits, and set the global interrupts.
  8. Enable the UART module by setting the ON bit of UxCON1.
  9. Set the RXEN bit of UxCON0 to enable reception.
  10. When a received byte is transferred from the RSR to UxRXB, the UxRIF bit becomes set, and an interrupt will be generated if the UxRXIE bit is set.
  11. Read the UxERRIR register to check for any errors.
  12. Read the UxRXB register to get the received byte and clear UxRXIF.
  13. Repeat steps 10-12 until all data has been received.

UART Receive Mode Initialization for PIC18(L)FXXK42 shows the initialization code used to configure the UART in receive mode.

UART Receive Mode Initialization for PIC18(L)FXXK42

void UART1_Initialize(void)
{
    U1CON0 = 0x80;            // BRGS normal speed; 8-bit mode; ABDEN disabled;
    U1CON1 = 0x80;            // ON enabled;
    U1CON2 = 0x00;            // RXPOL 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;
    U1CON0bits.RXEN = 1;         // Enable receiver
}