Serial Port Complete - Latest Microcontroller projects

(lily) #1

Chapter 11


 The putcUSART function writes a byte to the port. The BusyUSART function
returns 1 when the port is busy transmitting. To ensure that the port is avail-
able, place a while(BusyUSART()) statement before attempting to send data.
The WriteUSART function is identical to putcUSART.
Both of the putcUSART statements below write the same value (41h, the code
for the character “A”) to the port:
while(BusyUSART());
putcUSART('A')


while(BusyUSART());
putcUSART(0x41)
The putsUSART and putrsUSART functions can write strings to a port. This
example uses putrsUSART to write a string, including a null terminator (0),
from program memory to the port:
putrsUSART((rom char*) "Hello World!" );
This example uses putsUSART to write a character array from data memory up
to and including the null terminator:
unsigned char my_string[3] = {'\0'};

my_string[0] = '0';
my_string[1] = 'K';
my_string[2] = '\0';

putsUSART((char*)my_string);

  8 
$


Firmware that reads bytes at the serial port can check for errors, store received
bytes in an array, and perform other tasks while waiting for data to arrive. A
protocol can define a terminating character to indicate the end of a command
or string.

5    &
When receiving data, firmware can read the RCIF bit in the PIR1 register to
find out if a byte is available for reading.
Free download pdf