Chapter 11
Firmware can check for framing or overrun errors on the port. The following
examples show how to check for these errors. To avoid repetition, the additional
code examples in this book don’t check for overrun or framing errors, but most
working code should include the code below or an equivalent when reading
data from a port.
This statement causes the code to automatically clear any overrun errors:
DEFINE HSER_CLROERR
Code can instead detect and clear overrun errors as they occur:
if (RCSTA.1 = 1) then
' Overrun error. Clear and set CREN to clear the error and re-enable the receiver.
RCSTA.4 = 0
RCSTA.4 = 1
' (Perform any other required actions.)
endif
To check for framing errors, firmware must read the RCSTA register after the
byte has arrived at the port and before firmware reads the byte from RCREG:
if (PIR1.5 = 1) then
' A byte has arrived.
if (RCSTA.2 = 1) then
' Framing error.
' Read RCREG to clear the error but don't use the data.
hserin [serial_in]
else
' No framing error occurred. The data is valid.
hserin [serial_in]
' Do something with the received byte.
endif
endif