LCD
MPMC Lab · Experiment 10 · 8051 · Embedded C

LCD Interfacing Using Embedded C

Initialise an HD44780 16×2 LCD in 8-bit mode and display messages.

☰ All experiments

Aim

To interface a 16×2 character LCD (HD44780) with the 8051 and write an Embedded C program to display messages on the LCD.

Learning objectives

1
LCD pins
Identify RS, RW, EN and D0–D7 and their roles.
2
Initialisation
Send the init sequence 38H, 0CH, 01H, 06H in the right order with correct delays.
3
Commands vs data
Write command and data functions using RS.
4
Cursor addressing
Place text on line 1 (80H) and line 2 (C0H).
5
Strings
Send C strings with a pointer loop.
6
Timing
Respect busy times: 40 µs per write, 1.64 ms for clear/home.

What this experiment demonstrates

  • A parallel bus protocol with a strobe signal (EN).
  • Why timing matters: a write while the controller is busy is lost.
  • Converting numbers to ASCII for display.

Sub-tasks

  • 10.1 A Display two messages
  • 10.1 B Scrolling message
  • 10.1 C Live counter (number → ASCII)

LCD interface

MPMC LAB8051 LCD DEMO VSSVDDV0RSRWEND0D1D2D3D4D5D6D7AK RS=0 command · RS=1 data · RW=0 write · data latched on the falling edge of EN Common commands 38H 8-bit, 2 lines 0EH display+cursor 0CH display, no cursor 01H clear (1.64 ms) 06H cursor → 80H line 1 col 1 C0H line 2 col 1 18H shift display left DDRAM address: line 1 = 00H–0FH (visible), line 2 = 40H–4FH. Command = 80H | address. Write timing EN D0–D7, RS latched here
Fig 3. 16×2 HD44780 LCD: pins, commands and write timing.

Software delay in C

Machine cycle T = 12 / f_osc = 12 / 11.0592 MHz = 1.085 µs (classic 12T core) Inner loop for(j=0; j<115; j++); ≈ 8 machine cycles per pass 115 × 8 × 1.085 µs ≈ 1.0 ms delay_ms(500) ≈ 0.5 s

The exact count depends on the compiler's optimisation level, so calibrate it with Keil's stopwatch (t1 in the status bar) or this simulator's clock. The often-quoted value 1275 was calibrated for a 1-clock DS89C4x0 chip. On a classic 12-clock 8051 it gives about 11 ms per count.

Initialisation sequence

lcd_cmd(0x38); // function set: 8-bit bus, 2 lines, 5×7 dots lcd_cmd(0x0C); // display ON, cursor OFF (0x0E: cursor ON) lcd_cmd(0x01); // clear display, cursor home (needs 1.64 ms!) lcd_cmd(0x06); // entry mode: increment, no shift lcd_cmd(0x80); // DDRAM address 00H = line 1, column 1

Each write is a pulse on EN: put the data on D0–D7, set RS (0 = command, 1 = data) and RW = 0, then EN = 1 → EN = 0. The LCD latches the data on the falling edge.

Board wiring (this lab)

LCD8051
D0–D7P1.0–P1.7
RSP2.0
RWP2.1 (kept 0)
ENP2.2
V0 (contrast)10 k pot

Apparatus & environment

ItemSpecificationQty
PC with Keil µVisionC51/A51 tool chain (evaluation version is fine)1
8051 trainer boardNuvoton W78E052D, 11.0592 MHz crystal1
Nuvoton ISP-ICP utilityAPROM programming over USB-UART (CH340)—
USB cableUSB-A to the board's serial/ISP port1
Power supply+5 V regulated (board adapter)1
LCD module16×2 HD44780 with backlight1
Potentiometer10 kΩ for contrast1

Safety rules & good practice

  • ⚡ Switch the board off before connecting or removing modules, ribbon cables or jumpers.
  • 🔌 Check the supply: +5 V only. Reverse polarity or more than 6 V destroys the controller.
  • 🧲 Handle ICs by the edges and touch a grounded surface first (ESD).
  • 🔁 Do not drive a port pin connected to a switch with 0 while the switch is pressed to VCC. Read inputs only after writing 1s.
  • 🔥 If a component gets hot or smells, switch off immediately and inform the instructor.
  • 🔆 Adjust the contrast pot until the top row just shows faint blocks before running the program.

Procedure (hardware / real tools)

  1. Connect D0–D7 to Port 1, RS/RW/EN to P2.0/P2.1/P2.2 and the contrast pot, with the power off.
  2. Power on without a program: the top row should show dark blocks (an uninitialised LCD).
  3. Build and program 10.1 A. Check both lines.
  4. Remove the delay after the clear command and observe lost characters.
  5. Build 10.1 B and 10.1 C (scrolling and counter).
Click a step to tick it off. Your progress is saved in this browser.

Doing it in this virtual lab

  1. Choose a sub-task chip. The program opens in the µVision window.
  2. Press 🔨 Build (F7). Errors are listed in Build Output: click one to jump to the line.
  3. Press ▶ Run (F5) and watch the board. Use ¼× to 1/1000× slow motion to see fast events.
  4. Use Step (F11) and Step Over (F10), and click line numbers to set breakpoints.
  5. Edit the code (delay, pattern, pins), rebuild, and observe the change. Press ● Record to log results.
  6. The LCD card shows a log of every command the controller receives. Busy violations are flagged.

Pre-Lab Test 6 questions · diagnostic

Check your prerequisite knowledge. Each answer gets instant feedback. Score at least 4/6 before using the simulator.

Observation table

Rows are added by ● Record in the simulator: inputs, the expected (theoretical) value and the simulator result. Type your own hand calculation in "Your value" to check it. Add rows by hand for hardware-bench readings. The table is saved in this browser.

#Sub-taskInputsExpectedSimulatorYour valueCheckNote

Edge cases to test

  1. Remove delay_ms(2) from lcd_cmd(): characters are lost after the clear command.
  2. Remove lcd_cmd(0x38): line 2 stays blank (1-line mode).
  3. Use 0x0E or 0x0F instead of 0x0C to show the cursor or make it blink.
  4. Send a digit as a number (lcd_data(7)) instead of ASCII.
  5. Write more than 16 characters on line 1 without scrolling: where do they go?
  6. Set RW = 1 during a write.

Analysis & conclusion

Saved automatically in this browser.

Post-Lab Test 9 questions · instant hints

A wrong answer shows a hint so you can try again. Only first attempts count towards the score.

9. Numerical: which command (hex) moves the cursor to line 1, column 10?

Self-evaluation

Tick what you can now do confidently.

Overall confidence:

Feedback summary

Finish the post-lab test to see your personalised summary.