LCD -8051

Photo of author

By Jackson Taylor

As an Amazon Associate we earn from qualifying purchases.

A character LCD is one of the easiest ways to get readable output from a microcontroller. This tutorial shows how to connect a standard 16×2 LCD to an AT89S51 and print a line of text using MikroC, then test the whole thing in a Proteus simulation before building it on hardware.

The LCD runs in 4-bit mode, so it needs only six port pins instead of eleven. That leaves more pins free for other parts of your project. If you want the same idea on the AT89S52, see our guide to LCD interfacing with the AT89S52.

What you need

  • MICROCONTROLLER – AT89S51
  • COMPILER – MIKROC
  • SIMULATION – PROTEUS 8.1
  • CRYSTAL – 4MHz

You also need a 16×2 character LCD (the common HD44780-compatible type) and a small potentiometer for contrast. On a breadboard you will want a 5V supply and the usual two small capacitors for the crystal.

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

Video demonstration

DOWNLOAD SIMULATION FILES

How the LCD connects to the 8051

The 8051 microcontroller has four 8-bit ports. This project uses six pins of Port 2 to drive the display. If you are new to the 8051 microcontroller family, it helps to know that each port pin can be used as a general input or output pin, which is all the LCD needs here.

Pin connections

The connections below match the sbit definitions in the program:

  • RS (LCD pin 4) goes to P2.0. It tells the LCD whether the byte being sent is a command or a character.
  • EN (LCD pin 6) goes to P2.1. A pulse on this pin makes the LCD read the data lines.
  • D4 to D7 (LCD pins 11 to 14) go to P2.2, P2.3, P2.4 and P2.5.
  • D0 to D3 (LCD pins 7 to 10) are left unconnected in 4-bit mode.
  • RW (LCD pin 5) is connected to ground, because we only write to the display.
  • VSS goes to ground and VDD goes to +5V.
  • VEE (contrast) goes to the middle pin of a potentiometer wired between +5V and ground.
See also
ACCELEROMETER & GYRO

If your module has a backlight, connect its A and K pins as the module’s datasheet describes, usually through a current-limiting resistor if one is not already on the board.

CIRCUIT

Proteus circuit diagram of an AT89S51 microcontroller connected to a 16x2 LCD in 4-bit mode

The circuit above is the Proteus schematic. The crystal sets the clock at 4MHz. With the classic 8051 core, which takes 12 clock cycles per machine cycle, that gives one machine cycle per microsecond.

PROGRAM

// Lcd module connections
sbit LCD_RS at P2_0_bit;
sbit LCD_EN at P2_1_bit;
sbit LCD_D4 at P2_2_bit;
sbit LCD_D5 at P2_3_bit;
sbit LCD_D6 at P2_4_bit;
sbit LCD_D7 at P2_5_bit;
// End Lcd module connections
void main()
{
    Lcd_Init(); // Initialize Lcd
    Lcd_Cmd(_LCD_CLEAR); // Clear display
    Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
    Lcd_Out(1,6,"haii"); // Write text in first row
    while(1) ;
}

How the code works

MikroC includes a ready-made LCD library, so you do not have to write the low-level timing yourself. The code has two parts.

  1. Pin definitions. The sbit lines tell the library which port pins are wired to RS, EN and D4 to D7. If you move a wire to another pin, change the matching line.
  2. The main function. Lcd_Init() sets up the display in 4-bit mode. Lcd_Cmd(_LCD_CLEAR) clears it, and Lcd_Cmd(_LCD_CURSOR_OFF) hides the cursor. Lcd_Out(1,6,"haii") writes the text starting at row 1, column 6.

The final while(1); keeps the microcontroller in an endless loop. Without it, the program would end after printing and the 8051 could restart from the beginning of the code.

How to run the simulation

  1. Create a new MikroC project for the AT89S51 and set the clock to 4MHz.
  2. Paste the program, then build it to produce the HEX file.
  3. Open the Proteus file from the download link and double-click the microcontroller.
  4. Load the HEX file in the program file field and set the clock frequency to 4MHz.
  5. Press play. The text should appear on the first row of the LCD.

Changing what the display shows

You can edit the Lcd_Out line to try different positions. The first number is the row (1 or 2) and the second is the column (1 to 16 on a 16×2 display). To write on the second row, change it to something like Lcd_Out(2,1,"Hello");. To print a single character, use Lcd_Chr with a row, column and character.

See also
KEYPAD INTERFACING WITH 8051(AT89S52) WITH SIMULATION IN PROTEUS

Once the display works, it becomes a handy output for other projects. You can show the state of buttons with keypad interfacing with 8051 or read inputs with switch interfacing with 8051. The same LCD library approach also works on other chips, for example PIC16F877A with MikroC.

Troubleshooting

  • Nothing appears on screen. On real hardware, adjust the contrast potentiometer first. A wrong contrast setting is the most common reason for a blank display.
  • Black boxes on the first row only. The LCD is powered but not initialized. Check the EN and RS wiring and make sure the program was actually loaded.
  • Garbled characters. Check that D4 to D7 are wired in the right order and match the sbit lines.
  • Works in Proteus but not on hardware. Check that RW is grounded, the crystal and its capacitors are fitted, and the EA pin is tied to +5V when running from internal flash.

FAQ

Why use 4-bit mode instead of 8-bit mode?

4-bit mode sends each byte as two halves over four data lines. It is slightly slower, but the difference is not noticeable for text, and it saves four microcontroller pins.

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

Why is the RW pin connected to ground?

Grounding RW puts the LCD in write mode permanently. The MikroC LCD library only writes to the display, so it does not need to control this pin.

Can I use a different port for the LCD?

Yes. Change the sbit definitions to the port pins you want, for example P1 or P3 pins, and wire the LCD to match. Some pins on Port 3 have other functions, so check they are free in your design.

Does this work with the AT89S52?

Yes. The pin layout and LCD library calls are the same, so you only need to pick the correct chip in MikroC and Proteus.

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