Skip to main content

AT89S52 Serial Communication (UART) Tutorial - Keil uVision

This tutorial explains how to use Serial (UART) communication on the AT89S52 (8051) using Keil uVision.

You will learn how to:

  • Configure UART on AT89S52
  • Send data to PC (UART TX)
  • Receive data from PC (UART RX)
  • Test using a USB-to-TTL adapter and Serial Monitor

UART Pins on AT89S52

UART SignalAT89S52 PinPort Pin
TXDPin 11P3.1
RXDPin 10P3.0

1) Requirements

Hardware

⚠️ Use a 5V TTL adapter (not RS232). RS232 uses ± voltage and can damage the chip.

Software

  • Keil uVision (C51)
  • A serial terminal:
    • Arduino Serial Monitor
    • PuTTY
    • Tera Term
    • RealTerm

2) Wiring (USB-TTL to AT89S52)

Connect like this:

USB-TTLAT89S52
TXDRXD (P3.0 / Pin 10)
RXDTXD (P3.1 / Pin 11)
GNDGND
VCC5V (Only if powering from adapter, optional)

✅ Remember: TX ↔ RX crossed


3) Create a New Keil Project

  1. Open Keil uVision
  2. Go to Project → New uVision Project
  3. Select device: Atmel → AT89S52
  4. Click Yes for startup file
  5. Add main.c to Source Group 1

4) UART Settings Used

This tutorial uses:

  • Mode 1 (8-bit UART)
  • Baud rate: 9600
  • Crystal: 11.0592 MHz (recommended)

5) UART Transmit Example (Send Text)

This program sends "Hello from AT89S52" repeatedly.

#include <reg52.h>

void UART_Init(void)
{
SCON = 0x50; // Mode 1: 8-bit UART, REN enabled
TMOD |= 0x20; // Timer1 in Mode2 (8-bit auto-reload)
TH1 = 0xFD; // 9600 baud @ 11.0592MHz
TR1 = 1; // Start Timer1
}

void UART_TxChar(char ch)
{
SBUF = ch;
while(TI == 0); // Wait until transmitted
TI = 0; // Clear TI flag
}

void UART_TxString(char *str)
{
while(*str)
{
UART_TxChar(*str++);
}
}

void delay_ms(unsigned int ms)
{
unsigned int i, j;
for(i=0; i<ms; i++)
for(j=0; j<112; j++);
}

void main(void)
{
UART_Init();

while(1)
{
UART_TxString("Hello from AT89S52\r\n");
delay_ms(1000);
}
}