|
||||||||||||||||||||||||||||||||||||||||
|
Technical Support On-Line Manuals C251 User's Guide |
C251 User's GuideStream I/O RoutinesThe stream I/O routines are implemented as functions and are prototyped in the stdio.h include file. The stream input and output routines allow you to read and write data to and from a user-defined I/O interface. Characters are read using the _getkey routine and are written using the putchar routine. The default _getkey and putchar functions in the C251 library read and write characters using the 251 serial interface.
The source code for _getkey and putchar is available in the \KEIL\C251\LIB folder. You may modify these files and substitute them for the default library routines. The stream functions then perform input and output using your modified _getkey and putchar routines. To use the existing _getkey and putchar routines, you must first initialize the 251 serial port. If the serial port is not properly initialized, the default stream functions do not function. Initializing the serial port requires manipulating several special function registers (SFRs) of the 251. The include file REG251SB.H contains definitions for the required SFRs. The following example code must be executed immediately after reset, before any stream functions are invoked.
.
.
.
#include <reg251s.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 auto reload timer */
TH1 = 0xFD; /* Set auto reload 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. All functions are prototyped in the STDIO.H include file. | |||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||