ARM Technical Support Knowledge Articles

8051 SERIAL I/O IN C

Applies to: C51 C Compiler

Answer


Information in this article applies to:


QUESTION

How do I write a C program to echo characters coming in the 8051's serial port?

ANSWER

The functions _getkey and putchar perform serial I/O using the on-chip serial port. These routines are included in the C51 libraries and source for each is included in the C51LIB directory. Refer to your C51 User's Guide for complete documentation on these routines.

The following example program sets up the on-chip serial port, sets the baud rate, and waits for a character (using the _getkey function). When a character is received, it is output using the putchar function.

#include "reg51.h"
#include "stdio.h"

void main (void)
{
SCON  = 0x50;                   /* SCON: mode 1, 8-bit UART, enable rcvr    */
TMOD |= 0x20;                   /* TMOD: timer 1, mode 2, 8-bit reload      */
TH1   = 0xf3;                   /* TH1:  reload value for 2400 baud         */
TR1   = 1;                      /* TR1:  timer 1 run                        */
TI    = 1;                      /* TI:   set TI to send first char of UART  */

while(1)
  {
  unsigned char aaa;
  aaa = _getkey();
  putchar(aaa);
  }
}

MORE INFORMATION

Article last edited on: 2005-07-15 10:36:45

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