Keil Logo

C51: DIRECTING PRINTF OUTPUT TO SECOND SERIAL PORT


Information in this article applies to:

  • C51 Version 5.50

QUESTION

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

ANSWER

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

EXAMPLE

The 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

  • Refer to printf in the Cx51 User's Guide.
  • Refer to putchar in the Cx51 User's Guide.
  • Refer to Basic I/O in the Cx51 User's Guide.

SEE ALSO

FORUM THREADS

The following Discussion Forum threads may provide information related to this topic.

Last Reviewed: Friday, January 30, 2015


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  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.