18.5.1.2 Reception

Address Detect Reception

#include<xc.h>
#define FUART 4000000
#define BAUDRATE 9600
#define BRGVAL (((FUART/BAUDRATE)/16)-1)
uint16_t ReceivedChar[10];
uint8_t i=0;

int main(void) {
                                // Configure oscillator as needed
                                // Configure oscillator as needed
    ANSELB = 0;
    _U1RXR = 24; // Assign U1RX to RP24

    U1CONbits.MODE = 4;         // Asynchronous 9-bit UART with address detect
    U1CONbits.CLKSEL = 0;       // FPB/2 as Baud Clock source
    U1CONbits.BRGS = 0;         // Low-Speed Mode
    U1CONbits.STP = 0;          // 1 stop bit
    U1BRG = BRGVAL;             // BRG setting for 9600 baud rate
    U1PAbits.P2 = 0x45;         // Write parameter 2 with Address to match
    U1PBbits.P3 = 0xFF;         // Write parameter 3 register with mask value
    U1STATbits.RXWM = 0;        // Interrupt when there is one word or more in the buffer
    _U1RXIE = 1;                // Enable Receive interrupt
    U1CONbits.ON = 1;           // Enable UART
    U1CONbits.RXEN = 1;         // Enable UART RX
 
    while(1)
    {
    }
    return 0;
}
void __attribute__((interrupt, no_auto_psv)) _U1RXInterrupt(void)
{
    _U1RXIF = 0; // Clear RX interrupt flag
    while(U1STATbits.RXBE == 0)    // Check if RX buffer has data to read
    {
        ReceivedChar[i++] = U1RXB; // Read a character from RX buffer
    }
}