Avoid Overrun Error in PIC18F46K80

microchipmicrocontrollerpicuart

I am using PIC18F46K80, at a baud rate of 115200, a Stop Bit of 1 and no parity. I am sending data from a C# application(Baud Rate 115200, Stop Bit-1 and No Parity) to the PIC. After send some bytes of data, I get an over-run error in the PIC. I have inserted delays before receiving the data in the PIC.

T_UINT8 UARTReceive(T_UINT8 *t_ret)
{
    *t_ret = 0;
    T_UINT8 temp;

    while(!PIR1bits.RC1IF)
    {
        /* Wait till the data is received   */
            if((RCSTA1bits.FERR) || (RCSTA1bits.OERR) )
            {
                temp=RCREG1;
                temp=RCREG1;
                temp=RCREG1;
                temp=RCREG1;
                temp=RCREG1;
                // EUSART1 error - restart
                RCSTA1bits.CREN = 0;
                asm("NOP");
                RCSTA1bits.CREN = 1;              
                *t_ret = 1;
                return 0;
            }
    }

    if(RCSTA1bits.OERR==1)
    {
        k=30;
    }

    /* Return the received data  */
    Delay100TCYX(0);
    asm("NOP");
    asm("NOP");

    return RCREG1;
}

Even after inserting large delay, I am getting an over-run error, on maintaining the same baud rate in C# application . How do I avoid over-run errors while receiving data in my PIC?

Best Answer

You are getting overruns, which means the PIC isn't grabbing data out of the receive buffer fast enough. So adding even more delay makes absolutely no sense. Stop and actually think about what is going on.

Test with the PIC emptying RCREG as quickly as it can when RCIF indicates it has data. If the PIC is doing nothing else, it should be easily able to keep up at 115.2 kBaud.

You may have a baud rate mismatch. Test this by sending some characters and looking at their timing carefully on the scope. Then also verify the PC is sending characters at the same baud rate.