Serial Port Complete - Latest Microcontroller projects

(lily) #1

Chapter 13


 void receive_serial_data(void)
{
// Process received bytes.


if (DataRdyUSART())
{
// The serial port has received a byte.
// Read a received character.

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.
// Reading a byte clears the error.
// Read the byte but don't use it.

serial_in = getcUSART();
}
else
{
// A character was received without a framing error.

serial_in = getcUSART();

switch (serial_in)
{
case 0x0a:
{
// A LF character was received, indicating the end
// of a command.

respond_to_command();

break;
}
Free download pdf