|
|||||||||||
|
Technical Support Support Resources
Product Information |
C51: DIRECTING PRINTF OUTPUT TO SECOND SERIAL PORTInformation in this article applies to:
QUESTIONHow do I modify printf so that it uses the second serial port rather than the first. I cannot find the source code to printf anywhere in my installation. ANSWERThe printf function calls the putchar function to output individual characters. It is putchar which must be modified. Copy the PUTCHAR.C source file from the LIB folder into your project directory and add it to your project. Modify the source code appropriately. For example to use the 2nd serial port you would probably change SBUF to SBUF1, TI to TI_1 and RI to RI_1. EXAMPLEThe following example code defines the putchar and getkey functions for use with the second serial port of the Dallas Semiconductor 320 and 520.
//-----------------------------------------------
#include "stdio.h"
sfr SBUF1 = 0xC1; /* SBUF for SIO 1 */
sbit RI_1 = 0xC0; /* RI for SIO 1 */
sbit TI_1 = 0xC1; /* TI for SIO 1 */
sfr SCON1 = 0xC0; /* SCON for SIO 0 */
sfr TMOD = 0x89;
sfr TH1 = 0x8D;
sbit TR1 = 0x8E;
sfr WDCON = 0xD8; /* Watchdog Control */
//-----------------------------------------------
char putchar (char c)
{
while (!TI_1); /* wait until transmitter ready */
TI_1 = 0;
SBUF1 = c; /* output character */
return (c);
}
//-----------------------------------------------
char _getkey (void)
{
char c;
while (!RI_1); /* wait for a character */
c = SBUF1;
RI_1 = 0; /* clear receive interrupt bit */
return (c);
}
//-----------------------------------------------
void main (void)
{
SCON1 = 0x50; /* SCON: mode 1, 8-bit UART, enable rcvr */
TMOD |= 0x20; /* TMOD: timer 1, mode 2, 8-bit reload */
WDCON |= 0x80; /* SMOD_1: double baud rate bit */
TH1 = 0x71; /* TH1: reload value for 1200 baud */
TR1 = 1; /* TR1: timer 1 run */
TI_1 = 1; /* TI: set TI to send first char of UART */
while (1)
{
printf ("Type a character\r\n");
printf ("You entered \'%c\'\r\n", (unsigned char) _getkey ());
}
}
//-----------------------------------------------
MORE INFORMATION
SEE ALSO
FORUM THREADSThe following Discussion Forum threads may provide information related to this topic.
Last Reviewed: Tuesday, August 09, 2005 | ||||||||||
|
|||||||||||