Page 399 - Programming Microcontrollers in C
P. 399
384 Chapter 7 Advanced Topics
data are stored in the memory location a and sent out with the
instruction putchar(a) before it is returned to the calling program.
BYTE getchar(void)
{
BYTE a;
while(!SC0SR1.RDRF)
;/* Wait for data ready */
a=SC0DRL;
putchar(a); /* echo the data and */
return a; /* then return it*/
}
Finally, the puts() function from the standard library does not
terminate transmission when it detects a new line character. For proper
operation in this program, it should, so the following function was
written. Notice that this function terminates whenever either a new
line character or a zero character is detected in the input string. Like
the standard library puts() it outputs a new line character regardless
of the termination of the data.
void puts(char *s)
{
char *sp;
sp=s;
while(*sp!=’\n’ && *sp!=’\0')
putchar(*sp++);
putchar(‘\n’);
}
An interesting observation: If you look in Chapters 4, 5, 6, and 8
you will find similar routines for other chips. In every case, the
functions, even though they are for a broad range of different chips,
are the same—a testimonial to the use of a high-level language like
C to program our microcontrollers. A listing of these three functions
is collected together in Listing 7-17 shown below.
#include “HC12.H”
#define BAUD9600 52