Skip to content

UART with P89V51RD2

Introduction

The microcontroller P89V51RD2 has an inbuilt UART for carrying out serial communication. A serial port, like other PC ports, is a physical interface to establish data transfer between a computer and external hardware or device.

Problem Statement

Transmit serially "HELLO_WORLD" using 8051 microcontroller.

Requirements

  1. P89V51RD2
  2. USB to RS-232 Converter / DB9 RS232 Serial Cable

Circuit Diagram

Pin diagram

Code

  • Copy and paste the following code into main.c or download it from here:
c
#include <reg51.h>  

void UART_Init()
{
    TMOD = 0x20;        // Timer 1, 8-bit auto reload mode
    TH1 = 0xFD;         // Load value for 9600 baud rate
    SCON = 0x50;        // Mode 1, reception enable
    TR1 = 1;            //Start timer 1
}

void Transmit_data(char tx_data)
{
    SBUF = tx_data;      // Load char in SBUF register
    while (TI==0);      //Wait until stop bit transmit
    TI = 0;             // Clear TI flag
}

void String(char *str)
{
    int i;
    for(i=0;str[i]!=0;i++)  // Send each char of string till the NULL
    {
        Transmit_data(str[i]);  // Call transmit data function
    }
}  

void main()
{
    UART_Init();        // UART initialize function
    String("HELLO_WORLD");   
    while(1);
}

Output

HELLO_WORLD will be printed on the Flashmagic Terminal whenever reset button of the 8051 microcontroller is pressed. Goto Tools menu >> Select Terminal >> Flash Magic Terminal will appear >> Click Reset button on the hardware - 8051 Development board. Pin diagram
Pin diagram

Conclusion

Interfacing UART with the 8051 microcontroller provides a straightforward and efficient method for establishing serial communication with external devices. The UART functionality inherent in the 8051 microcontroller, coupled with its flexibility and reliability, makes it a suitable choice for various embedded systems and applications.