Ports for Embedded Systems
If the firmware uses hserin to read multiple bytes or to wait for a byte, there is
no way to detect overrun and framing errors because firmware has no way to
read RCSTA after executing hserin but before reading RCREG. For some appli-
cations, detecting framing and overrun errors isn’t essential.
After determining that a byte is available and before reading the byte, code can
check for overrun and framing errors.
unsigned char serial_in;
if DataRdyUSART()
{
// A character is available.
if (RCSTAbits.OERR == 1)
{
// Overrun error.
// Clear the error and re-enable the receiver.
RCSTAbits.CREN = 0;
RCSTAbits.CREN = 1;
}
if (RCSTAbits.FERR == 1)
{
// Framing error.
// Read the byte to clear the error but don't use the byte.
serial_in = getcUSART();
}
else
{
// A byte was received without error.
serial_in = getcUSART();
// Do something with the byte.
}
}
! '
A task loop is an endless loop that can perform serial-communications tasks and
any other tasks a device is responsible for. Firmware performs each task in