Page 426 - Programming Microcontrollers in C
P. 426
Serial Input/Output 411
/* read in a character from the serial port. Do not
check for the character ready. This routine should
be used with kbhit(). */
BYTE getce(void)
{
return U1RX.DATA;
}
/* return TRUE when a key has been hit and FALSE otherwise */
int kbhit(void)
{
return U1RX.CHARRDY;
}
Listing 8-7: Direct I/O Functions
The last two functions shown in Listing 8-7 are useful when you
need to exit a function if there is an asynchronous keyboard hit. The
function kbhit() returns a logical TRUE or FALSE. Its return should
be used as the argument to an if() or while() test. Whenever the
keyboard is touched, kbhit() returns a TRUE. That means that a
character has been entered into the keyboard. If you need to read and
use the value entered, the test to determine if a character is ready is
not necessary. Therefore, the function getce() was written to read
in the value contained in the data register without involving a test to
show that there is a character ready to be read. These two functions,
kbhit() and getce() should be used together.
The next four functions shown below also access the serial input
and output, but they all use the functions above for the direct access
and have no computer specific code. The function putchar() checks
to determine if the parameter x is a ‘\n’. If it is, the function
put() is called twice with a ‘\n’ and a ‘\r’ argument. Otherwise,
the character passed to the function is sent to put() where it is sent
to the serial port.
/* Send a character to the serial port when the port is
available. If a ‘\n’ is received send a ‘\n’ followed by
a ‘\r’ sequence. */
void putchar(BYTE x)
{
if(x==’\n’)