UART – 8051

Photo of author

By Jackson Taylor

As an Amazon Associate we earn from qualifying purchases.

The UART (Universal Asynchronous Receiver/Transmitter) is the simplest way to make an 8051 talk to a PC, a GSM module, an RFID reader or another microcontroller. It uses just two signal lines and no shared clock. This article shows a working UART example for the AT89S51 written in MikroC and simulated in Proteus. The program sends a greeting and then echoes back every byte it receives.

If you are new to the chip itself, our 8051 microcontroller basics page is a good place to start.

What you need

  • MICROCONTROLLER – AT89S51
  • COMPILER – MIKROC
  • SIMULATION – PROTEUS 8.1
  • CRYSTAL – 11.0592MHz(use this crystal to get baud rate of 9600)

Watch the demonstration below, then download the simulation files if you want to open the project in Proteus yourself.

Hardware Hack

Stop Buying New Batteries for Your Tech

Electronics are expensive, but their power sources don't have to be. Discover the simple engineering trick to recondition dead batteries and bring your gadgets back to 100% capacity.

Watch the Free Video
DOWNLOAD SIMULATION FILES

How the 8051 UART works

The 8051 has one built-in serial port. It uses two pins of Port 3:

  • RXD (P3.0, pin 10) receives data.
  • TXD (P3.1, pin 11) transmits data.

Data is sent as a frame: one start bit, eight data bits and one stop bit (often written 8N1). Both devices must agree on the speed, called the baud rate, because there is no clock line to keep them in step.

Inside the chip, the serial port is controlled by a few special function registers:

  • SBUF holds the byte to transmit or the byte just received.
  • SCON selects the serial mode and enables reception. It also holds the TI and RI flags that show a byte was sent or received.
  • TMOD, TH1 and TCON configure Timer 1, which normally generates the baud rate.
  • PCON contains the SMOD bit, which can double the baud rate.

MikroC sets all of this up for you when you call UART1_Init(), so you do not have to write the register code by hand.

See also
MECHANICAL / MECHATRONICS

Why an 11.0592 MHz crystal

In the common serial mode, the baud rate comes from Timer 1 running in 8-bit auto-reload mode. With SMOD at 0, the baud rate is the crystal frequency divided by 384 and by the number of timer counts (256 minus the reload value).

With an 11.0592 MHz crystal and a reload value of 0xFD (253), the timer counts 3 steps per bit. That gives 11,059,200 / 384 / 3 = 9600 baud exactly. A 12 MHz crystal would not divide evenly, so it produces a small baud rate error that can cause garbled characters. That is why this project specifies 11.0592 MHz.

Circuit

The circuit connects the AT89S51 to a serial terminal. Check that the crystal sits between the oscillator pins with its two capacitors, and that reset and power are wired as usual. Then cross the serial lines: the microcontroller’s TXD goes to the terminal’s RX, and the microcontroller’s RXD goes to the terminal’s TX.

Circuit diagram 1 Circuit diagram 2

On real hardware, the 8051’s pins work at TTL levels (0 V and 5 V), while a PC serial port uses RS-232 levels. Use a level converter such as a MAX232, or a USB-to-TTL serial adapter, between the microcontroller and the computer. Do not connect the pins directly to an RS-232 port. In Proteus, the virtual terminal can be wired straight to the pins.

Program

The program initializes the UART at 9600 baud and waits one second. It then sends the text “haiii” and loops forever. Whenever a byte arrives, it reads the byte and sends it straight back, so anything you type in the terminal appears on the screen again.

                      //RX PIN 10  (P3_0)
                      //TX PIN 11 (P3_1)
                      char uart_rd;
                      void main() {
                        UART1_Init(9600);               // Initialize UART module at 9600 bps
                        Delay_ms(1000);                  // Wait for UART module to stabilize
                        UART1_Write_Text("haiii");
                        while (1) {                     // Endless loop
                          if (UART1_Data_Ready()) {     // If data is received,
                            uart_rd = UART1_Read();     //   read the received data,
                            UART1_Write(uart_rd);       //   and send data via UART
                          }
                        }
                      }

The original comment on the UART1_Init line mentioned 4800 bps, but the code passes 9600, so the program runs at 9600 baud. Set your terminal to the same value.

Sustainable Tech

Keep Toxic E-Waste Out of Landfills

Don't throw away your failing laptop, phone, or car batteries. Learn how to recondition them at home in just a few hours. It's better for the planet—and your wallet.

Learn How It Works
See also
TRIAC GATE DRIVER (OPTO COUPLER )

What each function does

  • UART1_Init(9600) configures the serial port and Timer 1 for 9600 baud.
  • UART1_Write_Text() sends a string, one character at a time.
  • UART1_Data_Ready() returns a non-zero value when a received byte is waiting.
  • UART1_Read() returns that received byte.
  • UART1_Write() sends a single byte.

Testing the output

Open a terminal program, choose the correct port, and set 9600 baud, 8 data bits, no parity and 1 stop bit. After the one-second delay you should see “haiii”. Anything you type should then be echoed back.

x-ctu screen shot

Terminal programs such as X-CTU can show the received bytes either as text or as hex values. If you are looking at the hex view, a hexadecimal converter turns each byte into decimal or binary, and the ASCII code to text converter decodes the byte values back into characters such as “haiii”.

If you would rather test without hardware, our guide to the 8051 UART simulation in Proteus covers the virtual terminal setup in more detail.

Common problems

  • Garbage characters: the baud rate does not match, or the crystal is not 11.0592 MHz. Also check that the Proteus component’s clock frequency is set to 11.0592 MHz.
  • Nothing received: TX and RX are probably not crossed, or the ground is not shared between the two devices.
  • No output on a real PC: a level converter or USB-to-TTL adapter is missing.
  • Program stops responding to input: the code must check UART1_Data_Ready() before every read.

Where to go next

Once the echo program works, you can build on it. Send commands from the PC to switch outputs on and off, or show received characters by connecting an LCD with the 8051. UART is also how many modules communicate, and you can connect an RFID reader with the 8051 to read tag IDs. If the hardware UART is already in use, a soft UART on any spare pins is another option, though it is less convenient and uses more CPU time.

See also
RFID INTERFACING WITH 8051 (HARDWARE AND SIMULATION)

FAQ

Which pins does the 8051 use for UART?

RXD is on P3.0 (pin 10) and TXD is on P3.1 (pin 11) on the 40-pin package. Those pins cannot be used as normal I/O while the serial port is active.

Why is 11.0592 MHz used for 9600 baud?

Dividing 11.0592 MHz by 384 gives 28,800. With Timer 1 reloading every 3 counts, the result is exactly 9600 baud, so there is no timing error. Other crystals usually give a small error.

Can I use a different baud rate?

Yes. Pass another value to UART1_Init(), such as 4800 or 19200, and set the terminal to match. With an 11.0592 MHz crystal, the common standard rates stay accurate.

Do I need a MAX232 for the 8051 UART?

Only if you connect to a true RS-232 port. A USB-to-TTL adapter works directly with the 5 V logic pins, and Proteus needs neither.

Revive Your Dead Electronics

Got a drawer full of dead gadgets or a failing car battery? Instead of paying a premium for replacements, use this simple trick to easily recondition them right from home.

See How It Works