Login

Lost your password?
Don't have an account? Sign Up

UART in 8051 Microcontroller (AT89C52)

Would you like to join our comprehensive practical video course on the AT89C52 Microcontroller?

Introduction

UART in 8051: Serial communication allows the microcontroller to exchange data with external devices such as computers, Bluetooth modules, GSM modems, and other microcontrollers. The AT89C52 microcontroller includes a built-in UART (Universal Asynchronous Receiver and Transmitter) module for full-duplex serial communication. This tutorial explains how UART works in AT89C52 and how to configure it using registers and embedded C code.

Prerequisites

Before reading this blog, make sure you are familiar with how to install the Keil µVision IDE, create a new project, and compile the code successfully.
If you haven’t done this yet, check out our detailed guide:

Basics of Serial Communication

In serial communication, data is transmitted one bit at a time through a single line, making it more efficient for long-distance transmission than parallel communication.
There are two main types of serial communication:

  • Synchronous: Sender and receiver share a common clock signal.
  • Asynchronous: No clock is shared; both ends agree on the same baud rate.
    The AT89C52 UART operates in asynchronous mode by default.
UART Features in AT89C52
  • Full-duplex communication (simultaneous transmit and receive)
  • Configurable baud rate
  • 1 Start bit, 8 Data bits, 1 Stop bit format
  • Interrupt-driven or polling mode operation
  • Baud rate generation using Timer 1 or Timer 2
UART Pins
PinPortDescription
TXDP3.1Transmit data (output)
RXDP3.0Receive data (input)

These two pins are connected internally to the serial communication hardware module of AT89C52.

Registers Used in Serial Communication

SCON (Serial Control Register)

Controls serial port operation and mode selection.
Address: 98H

SCON Register Bit Format:

Bit76543210
SymbolSM0SM1SM2RENTB8RB8TIRI
FunctionSerial mode bit 0Serial mode bit 1Multiprocessor communication enableReceive enableTransmit bit 8 (for 9-bit mode)Receive bit 8 (for 9-bit mode)Transmit interrupt flagReceive interrupt flag

Common Setting for Mode 1 (8-bit UART):

SCON = 0x50; → SM0=0, SM1=1 (Mode 1), REN=1, others=0

TMOD (Timer Mode Control Register)

Used to configure Timer 1 in Mode 2 (Auto-reload) to generate the baud rate.
TMOD Register Bit Format:

Bit76543210
FunctionGATE1C/T1M1_1M0_1GATE0C/T0M1_0M0_0

TH1 (Timer 1 High Byte Register)

Holds reload value to generate the desired baud rate.
Example: For 9600 bps at 11.0592 MHz → TH1 = 0xFD

PCON (Power Control Register)

Controls power management and serial baud rate doubling.
Address: 87H
Key Bit:

  • SMOD (Bit 7): Doubles the baud rate when set to 1.
    Example:
    PCON |= 0x80; → Enables SMOD = 1

Serial Communication Modes

AT89C52 supports four serial communication modes via the SCON register:

ModeBitsFunctionBaud Rate
08-bit shift registerSynchronous modefosc / 12
110-bit UART1 start + 8 data + 1 stopVariable
211-bit UART (fixed baud rate)1 start + 8 data + 1 stop + 9th bitfosc / 64 or fosc / 32
311-bit UART (variable baud rate)Same as Mode 2Variable

Most applications use Mode 1 for standard UART communication.

Baud Rate Calculation

Baud rate in Modes 1 and 3 is derived from Timer1 overflow rate.

Example: For 11.0592 MHz crystal, the desired baud rate = 9600 bps
Substituting values,

Value to be loaded to the register, TH1 = 0xFD

Example: For 11.0592 MHz crystal, desired baud rate = 9600 bps
Substituting values,

UART in 8051

#include <REGX52.H>

void UART_Init(void)
{
  TMOD = 0x20;     // Timer1 Mode2
  TH1  = 0xFD;     // Baud rate 9600
  SCON = 0x50;     // 8-bit UART, enable receiver
  TR1  = 1;        // Start Timer1
}

void UART_TxChar(char ch)
{
  SBUF = ch;       // Load data to transmit buffer
  while(TI == 0);  // Wait for transmission to complete
  TI = 0;          // Clear transmit flag
}

char UART_RxChar(void)
{
  while(RI == 0);  // Wait for reception
  RI = 0;          // Clear receive flag
  return SBUF;     // Return received character
}

void main(void)
{
  char data;
  UART_Init();
  while(1)
  {
    UART_TxChar('H');
    UART_TxChar('i');
    UART_TxChar('\n');
    data = UART_RxChar();  // Echo received data
    UART_TxChar(data);
  }
}
C

Explanation of the Code
  • Timer1 is used to generate the baud rate.
  • SBUF register holds transmitted or received data.
  • TI and RI Flags indicate completion of transmit and receive, respectively.
  • The program continuously sends “Hi” and echoes received data.
Common Applications of UART in AT89C52
  • Serial communication with PC through USB-to-UART converter
  • Interfacing Bluetooth (HC-05/HC-06) modules
  • GSM or GPS module interfacing
  • Communication between two microcontrollers
Summary

Serial communication (UART) is one of the most useful features of the AT89C52 microcontroller. It enables data exchange between the MCU and external devices efficiently. By configuring SCON, TMOD, and TH1 registers correctly, reliable full-duplex serial communication can be achieved at standard baud rates like 9600 or 19200 bps.

Upcoming AT89C52 programming tutorials, including peripheral interfacing and project development.

Do you want to go beyond just configuration and build real embedded projects?
🎓 Join our Complete Practical Video Course to Master the AT89C52 Microcontroller.

Get step-by-step guidance on Timers, Interrupts, UART, I/O Interfacing, ADC (using ADC0804), and I²C/SPI (Software Implementation) — with real circuits, simulations, and hardware demonstrations!

Downloading and Installing Keil µVision IDE
  • Visit the official Keil website at https://www.keil.com or KEIL IDE
  • Navigate to the Downloads → C51 Compiler section.
  • Register (if required) and download the latest version of Keil µVision for 8051 (C51).
  • Run the installer and follow the on-screen instructions.
  • Once installed, launch the Keil µVision IDE from the Start Menu.

# UART in 8051#KEIL #Keil IDE for programming 8051 #UART # AT89C52

Home » Recent posts » Learn › 8051>UART in 8051

https://bitziga.com

Leave a Comment

Your email address will not be published. Required fields are marked *

*
*