Crossware

Table of Contents        Previous topic       Next topic       

C COMPILER->Compiler Support Files->Console I/O

getch() and putch() are the two target specific functions which perform console i/o.  printf(), puts(), fgetc() etc all use these functions and so once these two functions are correct for your target system, all of the other i/o functions will work correctly for i/o streams stdin, stdout and stderr.

The versions of putch() and getch() that are included in the library make use of the 8051 serial port.  You will need to correctly set up the 8051 serial port in order to use these and to do this you should call the library function _InitSerialPort().

The source code for getch(), putch() and _InitSerialPort() is contained in file SERIO.C in directory STARTUP.  Also in this directory is a small program MAIN.C which illustrates the use of these functions.

The library version is interrupt driven and so after  the call to _InitSerialPort() interrupts will be enabled and i/o will be interrupt driven.  putch() will return immediately after it has placed the output character in the serial port output buffer.  getch() will return with the last character received or will wait for the next byte if one has not been received since the last getch() call.  You can check to see if a character is waiting in the input buffer by calling kbhit().  This will return a non-zero value if a character is waiting.

Note that register bank 2 is used by the interrupt routine that monitors the serial port.

To remove putch() and getch() from the library so that you can replace them with your own versions, use the library manager with the command line:

Lib    -serio <library name>;