Serial Port Complete - Latest Microcontroller projects

(lily) #1
Ports for Embedded Systems

 The getsUSART function can read multiple characters but requires specifying
how many characters to read and blocks until all of the characters arrive:
char received_data[8];


getsUSART(received_data, 8);
To read a string without blocking, firmware can call the getcUSART function
repeatedly to read individual bytes into a char array. A task loop can call the
routine below to read a specified number of bytes into an array:
byte index;

void receive_serial_data(void)
{
byte bytes_to_read;
char received_data[8];

bytes_to_read = 8;

if DataRdyUSART()
{
// A byte is available. Read it into the array.

received_data[index] = getcUSART();

// Increment the position in the array.

index++;

if (index == bytes_to_read)
{
// Do something with the received bytes.

// Reset the index for the set of received bytes.

index = 0;
}
}

      
 $

A defined terminating character can indicate the end of a command, string, or
other block of data. The examples below check for a CR character (0Dh) in
received data.
Free download pdf