Keil Logo

Technical Support

On-Line Manuals

C251 User's Guide

C251 Introduction Compiling Programs Language Extensions Preprocessor Advanced Programming Error Messages Library Reference Library Files Source Code Standard Types jmp_buf va_list Macros CBYTE CVAR CARRAY DARRAY DBYTE DVAR HARRAY HBYTE HVAR NARRAY NBYTE NVAR XARRAY XBYTE XVAR Routines by Category Buffer Manipulation Routines Character Routines Data Conversion Routines Intrinsic Routines Math Routines Memory Allocation Routines Stream I/O Routines String Routines Variable Length Argument Routines Miscellaneous Routines Include Files SFR Definitions absacc.h assert.h ctype.h float.h intrins.h limits.h math.h setjmp.h stdarg.h stddef.h stdio.h stdlib.h string.h Reference _chkdouble_ _chkfloat_ _crol_ _cror_ _getkey _irol_ _iror_ _lrol_ _lror_ _nop_ _testbit_ _tolower _toupper abs acos asin assert atan atan2 atof atoi atol cabs calloc ceil cos cosh exp fabs fcalloc ffree finit_mempool floor fmalloc fmemcpy fmemset frealloc free getchar gets hmemccpy hmemchr hmemcmp hmemcpy hmemmove hmemset hstrcmp hstrcpy hstrlen hstrncpy init_mempool isalnum isalpha iscntrl isdigit isgraph islower isprint ispunct isspace isupper isxdigit labs log log10 longjmp malloc memccpy memchr memcmp memcpy memmove memset modf offsetof pow printf putchar puts rand realloc scanf setjmp sin sinh sprintf sqrt srand sscanf strcat strchr strcmp strcpy strcspn strlen strncat strncmp strncpy strpbrk strpos strrchr strrpbrk strrpos strspn tan tanh toascii toint tolower toupper ungetchar va_arg va_end va_start vprintf vsprintf xcalloc xfree xinit_mempool xmalloc xmemcpy xmemset xrealloc Appendix

Stream I/O Routines

The 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.

Routine Attributes Description
getchar reentrant Reads and echoes a character using the _getkey and putchar routines.
_getkey   Reads a character using the 251 serial interface.
gets   Reads and echoes a character string using the getchar routine.
printf   Writes formatted data using the putchar routine.
putchar reentrant Writes a character using the 251 serial interface.
puts reentrant Writes a character string and newline ('\n') character using the putchar routine.
scanf   Reads formatted data using the getchar routine.
sprintf   Writes formatted data to a string.
sscanf   Reads formatted data from a string.
ungetchar   Places a character back into the getchar input buffer.
vprintf   Writes formatted data using the putchar function.
vsprintf   Writes formatted data to a string.

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.

  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.