Stream Input and Output

Routine

Attributes

Description

getchar

reentrant

Reads and echoes a character using the _getkey and putchar routines

gets

Reads and echoes a character string using the getchar routine

printf

Writes formatted data using the putchar routine

printf517

Writes formatted data using the putchar routine and the high-speed features of the 80C517

putchar

Writes a character using the 8051 serial interface

puts

reentrant

Writes a character string and newline (\n) character using the putchar routine

scanf

Reads formatted data using the getchar routine

scanf517

Reads formatted data using the getchar routine and the high-speed features of the 80C517

sprintf

Writes formatted data to a string

sprintf517

Writes formatted data to a string using the high-speed features of the 80C517

sscanf

Reads formatted data from a string

sscanf517

Reads formatted data from a string using the high-speed features of the 80C517

ungetchar

Places a character back into the getchar input buffer

_getkey

Reads a character using the 8051 serial interface

The stream input and output routines allow you to read and write data to and from the 8051 serial interface or a user-defined I/O interface. The default _getkey and putchar functions found in the C51 library read and write characters using the 8051 serial interface. You can find the source for these functions in the \FSI\LIB  directory. You may modify these source files and substitute them for the library routines. When this is done, other stream functions will then perform input and output using the new _getkey and putchar routines.

If you want to use the existing _getkey and putchar functions, you must first initialize the 8051 serial port. If the serial port is not properly initialized, the default stream functions will not function. Initializing the serial port requires manipulating special function registers SFRs of the 8051. The include file REG51.H  contains definitions for the required SFRs. The following example code must be executed immediately after reset, before any stream functions are invoked.
.
.
.
#include <reg51.h>
.
.
.
SCON = 0x50; /* Setup serial port control register */
/* Mode 1: 8-bit uart var. baud rate */
/* REN: enable receiver */

PCON &= 0x7F; /* Clear SMOD bit in power ctrl reg */
/* This bit doubles the baud rate */

TMOD &= 0xCF /* Setup timer/counter mode register */
/* Clear M1 and M0 for timer 1 */
TMOD |= 0x20; /* Set M1 for 8-bit autoreload timer */

TH1 = 0xFD; /* Set autoreload value for timer 1 */
/* 9600 baud with 11.0592 MHz xtal */

TR1 = 1; /* Start timer 1 */

TI = 1; /* Set TI to indicate ready to xmit */
.
.
.

The stream routines treat input and output as streams of individual characters. There are routines that process characters as well as functions that process strings. Choose the routines that best suit your requirements.

All of these routines are implemented as functions. Most are prototyped in the STDIO.H  include file. The printf517, scanf517, sprintf517, and sscanf517 functions are prototyped in the 80C517.H  include file.