Rx UART interrupt triggering on PIC24F

mplabpicuart

I'm trying to receive data to a PIC24FJ256DA210 from another device using UART communication.

I set up the UART as described below:

void InitUART(void)
{
    uint32_t baudRate = 312500;
    int brg = FCY/(16*baudRate)-1;

     /* Assign UART 1 signal onto pin 4 of PortF (RP10) using PPS */
    TRISFbits.TRISF4 = 1;
    __builtin_write_OSCCONL(OSCCON&0xbf); // clear bit to allow re-map
    RPINR18bits.U1RXR = 10; //Assign U1RX to RP10
    __builtin_write_OSCCONL(OSCCON | 0x40); // lock pin re-map

    CloseUART1(); //disable UART if enabled previously

    /*Enable UART interrupts*/
    IEC0bits.U1RXIE = 1; // interrupt on reception allowed
    IEC0bits.U1TXIE = 0; // no interrupt on transmition
    IEC4bits.U1ERIE = 1; // interrupts on errors allowed
    IPC2bits.U1RXIP = 5; // interrupt level of reception
    IPC16bits.U1ERIP = 6; // interrupt level on errors
    U1STAbits.URXISEL = 0; // interrupt when any character is received

    /* Initialize UART1 */
    U1BRG = brg;
    U1MODEbits.STSEL = 0; // 1 stop bit
    U1MODEbits.PDSEL = 0; //8 bit data, no parity
    U1MODEbits.BRGH = 0; //Standard speed mode
    U1MODEbits.RXINV = 0; //UxRX idle state is '1'
    U1MODEbits.ABAUD = 0; //Auto-Baud disabled
    U1MODEbits.LPBACK = 0; //Loopback disabled
    U1MODEbits.WAKE = 1; //Wake up on start bit detect
    U1MODEbits.UEN = 0; //UTx and URx enabled
    U1MODEbits.IREN = 0; //IrDA disabled
    U1STAbits.ADDEN = 0; //Adress detec mode disabled
    U1RX_Clear_Intr_Status_Bit; //clear RX interrupt falg
    U1MODEbits.UARTEN = 1; //UART enable
} 

My problem is that every time I launch debug (I'm working with MPLAB X IDE), the U1 RX Interrupt keeps getting triggered even though nothing is yet sent to the PIC.

Did I do something wrong while initializing UART? It migth be a silly mistake but I really can't figure it out.

Thank you.

Best Answer

That's normal behaviour. As soon as you enable the receive interrupt it will fire because the Overrun Error flag in the status register will be set.

You will need to clear this error flag in software before you get any more RX interrupts.

See the datasheet for details: http://ww1.microchip.com/downloads/en/DeviceDoc/39708a.pdf