3 UART Receive
The following steps outline the reception process:
- Configure the UxBRGH:UxBRGL register pair and BRGS bit to achieve the desired baud rate.
- Set the MODE<3:0> bits of UxCON0 to the desired operation mode.
- Set the RXPOL bit of UxCON2 if an inverted RX input is desired.
- Load the RXPPS register with the value associated with the desired RX pin.
- Clear the associated ANSEL bit (if applicable).
- Set the associated TRIS bit.
- If UART interrupts are desired, set the appropriate interrupt enable bits, clear the associated interrupt flag bits, and set the global interrupts.
- Enable the UART module by setting the ON bit of UxCON1.
- Set the RXEN bit of UxCON0 to enable reception.
- 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.
- Read the UxERRIR register to check for any errors.
- Read the UxRXB register to get the received byte and clear UxRXIF.
- 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
}