ARM Technical Support Knowledge Articles

DIRECTING PRINTF OUTPUT TO SECOND SERIAL PORT

Applies to: C51 C Compiler

Answer


Information in this article applies to:


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
");
  printf ("You entered '%c'
", (unsigned char) _getkey ());
  }
}
//-----------------------------------------------

MORE INFORMATION

SEE ALSO

Article last edited on: 2005-08-09 11:03:08

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential