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 Signal | AT89S52 Pin | Port Pin |
|---|---|---|
| TXD | Pin 11 | P3.1 |
| RXD | Pin 10 | P3.0 |
1) Requirements
Hardware
- AT89S52 or 8051 Development Board
- USB-to-TTL Adapter
- Jumper wires
⚠️ 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-TTL | AT89S52 |
|---|---|
| TXD | RXD (P3.0 / Pin 10) |
| RXD | TXD (P3.1 / Pin 11) |
| GND | GND |
| VCC | 5V (Only if powering from adapter, optional) |
✅ Remember: TX ↔ RX crossed
3) Create a New Keil Project
- Open Keil uVision
- Go to Project → New uVision Project
- Select device: Atmel → AT89S52
- Click Yes for startup file
- Add
main.cto 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);
}
}