8051 (AT89S52/51) UART SIMULATION IN PROTEUS

Photo of author

By Jackson Taylor

Home » Programming » 8051 (AT89S52/51) UART SIMULATION IN PROTEUS

As an Amazon Associate we earn from qualifying purchases.

The UART is the simplest way to get an 8051 talking to a PC, a GSM module, an RFID reader or another microcontroller. Before you wire anything on a breadboard, you can test the whole thing in Proteus. This guide shows how to simulate 8051 (AT89S52/51) UART communication in Proteus, from the circuit to the code to the settings that most often cause garbled output.

The video above shows the simulation running. The rest of this article explains how it works so you can build it yourself.

How the 8051 UART works

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

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
  • P3.0 (RXD): receives serial data.
  • P3.1 (TXD): transmits serial data.

Data goes through a single register called SBUF. Writing to SBUF sends a byte, and reading from SBUF returns the last byte received. Two flags tell you what is happening: TI is set when a byte has finished sending, and RI is set when a byte has been received. Your code must clear both flags in software.

What you need

  • Proteus (ISIS) for drawing and simulating the circuit.
  • A compiler such as Keil µVision to produce a HEX file.
  • An 8051-family microcontroller in Proteus. The library normally includes AT89C51 and AT89C52, which behave the same as the AT89S51/S52 for this purpose, so use those if you cannot find an AT89S part.
  • A Virtual Terminal from the Proteus instruments toolbar.

Building the circuit in Proteus

  1. Place the 8051 microcontroller on the schematic.
  2. Add the Virtual Terminal from the Virtual Instruments Mode (the monitor icon on the left toolbar).
  3. Connect the microcontroller TXD (P3.1) to the terminal RXD pin.
  4. Connect the microcontroller RXD (P3.0) to the terminal TXD pin. Transmit always goes to receive, and the other way round.
  5. Double-click the microcontroller and set the clock frequency to 11.0592 MHz. Proteus does not need a physical crystal drawn on the schematic, but this property must match the value your code assumes.
  6. Double-click the Virtual Terminal and set the baud rate to 9600, 8 data bits, no parity and 1 stop bit.
See also
PIC16F877A-HITECH C - UART

You do not need a MAX232 in the simulation, because the Virtual Terminal works at logic level. A level converter is only needed when you connect the real chip to a PC serial port.

Setting the baud rate

For the common 8-bit UART mode (mode 1), the baud rate comes from Timer 1 running in auto-reload mode (timer mode 2). The formula is:

Baud rate = (2SMOD / 32) × (crystal frequency / 12 / (256 − TH1))

With an 11.0592 MHz crystal and SMOD = 0, loading TH1 with 0xFD gives exactly 9600 baud. That is why 11.0592 MHz is so popular for 8051 serial projects: it divides cleanly into standard baud rates. With a 12 MHz crystal the result is slightly off, and errors can show up as odd characters.

Register setup

  • TMOD = 0x20: Timer 1, mode 2 (8-bit auto-reload).
  • TH1 = 0xFD: 9600 baud at 11.0592 MHz.
  • SCON = 0x50: serial mode 1 (8-bit UART) with the receiver enabled.
  • TR1 = 1: starts Timer 1.

Example code in C

These short functions cover most simple tests. Compile them in Keil with the reg51.h or reg52.h header.

  • void uart_init(void) { TMOD = 0x20; TH1 = 0xFD; SCON = 0x50; TR1 = 1; }
  • void uart_tx(unsigned char c) { SBUF = c; while (!TI); TI = 0; }
  • unsigned char uart_rx(void) { while (!RI); RI = 0; return SBUF; }

To send a string, loop through its characters and call uart_tx() for each one until you reach the end. For an echo test, call uart_rx() in a loop and pass the result straight to uart_tx(). Whatever you type in the Virtual Terminal should appear back on screen.

Running the simulation

  1. Build the project in Keil and generate the HEX file.
  2. In Proteus, double-click the microcontroller and browse to the HEX file in the Program File field.
  3. Press the play button. The Virtual Terminal window opens.
  4. Check that the text sent by the microcontroller appears. Then click in the terminal window and type to test reception.
See also
ANDROID LAND ROVER

Common problems

  • Garbage characters: the baud rate in the terminal, the clock frequency in Proteus and the TH1 value in your code do not agree. Check all three.
  • Nothing appears: TX and RX are probably not crossed, or the HEX file was not loaded.
  • Output stops after the first byte: you forgot to clear TI after sending.
  • Received data repeats: RI was not cleared after reading.
  • Typing does nothing: the Virtual Terminal window must have focus, and REN must be enabled in SCON.

Where to go next

Once basic UART works, you can show received data on a display. Our guide on LCD interfacing with 8051 covers the display side, and keypad interfacing with the 8051 shows how to build input that you can send over serial. Many RFID readers also talk over UART, so RFID interfacing with 8051 is a natural next step.

If you work with other boards, the same idea applies elsewhere. See the Arduino UART and LCD simulation for the Arduino version, or the PIC18F4550 UART simulation with a real-time clock for a PIC example.

You can find the original project page here: MORE AT

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

FAQ

Which baud rate should I use for 8051 UART?

9600 baud is the usual choice. With an 11.0592 MHz crystal and TH1 = 0xFD it is exact. Higher rates are possible by setting SMOD or changing TH1, but 9600 is a good starting point.

Do I need a MAX232 in Proteus?

No. The Virtual Terminal accepts logic-level signals directly. You only need a MAX232 or similar converter when connecting the real circuit to a PC RS-232 port.

Can I use the AT89S52 in Proteus?

If your library does not list the AT89S52 or AT89S51, use the AT89C52 or AT89C51. The UART registers and behaviour are the same for this kind of simulation.

Why does the simulation show wrong characters?

Almost always a baud rate mismatch. Make sure the clock frequency set in Proteus, the TH1 value and the Virtual Terminal baud rate all match.

See also
power factor measurement demostration using matlab -simulink (simulation)

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