Ports for Embedded Systems
A device that is receiving commands that end with a CR can check for the char-
acter on receiving data and take action when received:
byte index;
char received_data[80];
void receive_serial_data(void)
{
if DataRdyUSART()
{
// A byte is available.
received_data[index] = getcUSART();
if (received_data[index] == 0x0d)
{
// It's a CR code. Do something with the received data.
// Reset the index.
index = 0;
}
else
{
// Increment the index, resetting to 0 if the buffer is full.
if (index < 79 )
index++;
else
index = 0;
}
}
}
.
When data arrives at unpredictable times, firmware can use the receive inter-
rupt to read bytes as they arrive.
With the receive interrupt enabled, the CPU jumps to the interrupt vector (a
defined location in program memory) every time a byte arrives at the port. An
ISR can read the received byte and perform any other needed actions.
In a similar way, with the transmit interrupt enabled, the CPU jumps to the
interrupt vector every time TXREG is newly empty. The ISR can then write