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.
RxyPPS | Pin Rxy Output Source | PORT to Which Output can be Directed | |||||||
---|---|---|---|---|---|---|---|---|---|
28-Pin Devices | 40-Pin Devices | ||||||||
0x0C | EUSART2 (DT) | — | B | C | — | B | — | D | — |
0x0B | EUSART2 (TX/CK) | — | B | C | — | B | — | D | — |
0x0A | EUSART1 (DT) | — | B | C | — | B | C | — | — |
0x09 | EUSART1 (TX/CK) | — | B | C | — | B | C | — | — |
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;
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;
/* Configure RE0 as output. */
TRISEbits.TRISE0 = 0;
Function | Pin |
---|---|
EUSART2 TX | RD0 |
EUSART2 RX | RD1 |
LED0 | RE0 |
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;
}