4.3.3 How to Configure the Pins

In this example, the pin used as TX is RD0 and it must be configured using the Peripheral Pin Select (PPS). To find the value that needs to be written to the RD0PPS register, inspect the Peripheral PPS Output Selection Codes table below, taken from the device data sheet.

Table 4-2. Peripheral PPS Output Selection Codes
RxyPPSPin Rxy Output SourcePORT to Which Output can be Directed
28-Pin Devices40-Pin Devices
0x0CEUSART2 (DT)BCBD
0x0BEUSART2 (TX/CK)BCBD
0x0AEUSART1 (DT)BCBC
0x09EUSART1 (TX/CK)BCBC

The following line will direct the EUSART2 TX to RD0:

/* RD0 is TX2 */
RD0PPS = 0x0B; 

The pin direction is set by default output, but if it were not, the following line sets it.

/* Configure RD0 as output. */
TRISDbits.TRISD0 = 0;
The pin used as RX is RD1, so using PPS, EUSART2 RX must be routed to this pin. For this, the RX2PPS register is used. The value to be written is determined based on the xxxPPS register definition:
Figure 4-3. Peripheral xxx Input Selection

Therefore, the following line routes RX2 to pin RD1.

RX2PPS = 0b00011001;

Since the pin is not a digital input by default, this needs to be configured (enable digital input buffers using the ANSELD register and set as input using the TRISD register).

/* Configure RD1 as input. */
TRISDbits.TRISD1 = 1;
/* Enable RD1 digital input buffers.*/
ANSELDbits.ANSELD1 = 0;
The LED pin, RE0 in this example, must be configured as a digital output.
/* Configure RE0 as output. */
TRISEbits.TRISE0 = 0;
Table 4-3. EUSART Pin Locations
FunctionPin
EUSART2 TXRD0
EUSART2 RXRD1
LED0RE0
Before sending data, the user needs to check if the previous transmission is complete by checking the PIR3.TXnIF bit field. The following code example waits until the transmit buffer is empty, then writes a character to the TXnREG register:
static void EUSART2_write(uint8_t txData)
{
    while(0 == PIR3bits.TX2IF)
    {
        ;
    }
    TX2REG = txData;
}

Before reading the data, the user must wait for it to be available by polling the PIR3.RCnIF flag.

uint8_t EUSART2_read(void)
{
    while(0 == PIR3bits.RC2IF)
    {
        ;
    }

    return RC2REG;
}