3.3.3 How to Configure the Pins

Since only the transmission is necessary for this use case, only the TX pin must be configured. The pin used as TX, in this example, is the RD0 pin 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 3-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 otherwise, the following line sets it.

/* Configure RD0 as output. */
TRISDbits.TRISD0 = 0;
Table 3-3. EUSART Pin Locations
FunctionPin
EUSART2 TXRD0

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; 
}