8.8
MPMC Lab · Experiment 9 · 8051 · Embedded C

Seven-Segment Display Interfacing Using Embedded C

Four common-anode displays in multiplexed mode: hex digits 0–F and the number 1234.

☰ All experiments

Aim

To interface four common-anode seven-segment displays to the 8051 in multiplexed mode, and to write Embedded C programs that display the hexadecimal digits 0 to F and the number 1234.

Learning objectives

1
Segment codes
Derive common-anode segment codes for 0–F.
2
Look-up tables
Store codes in code memory (unsigned char code table[]).
3
Multiplexing
Scan digits one at a time and explain persistence of vision.
4
Refresh rate
Calculate the refresh rate and the flicker threshold.
5
Ghosting
Blank the digits during switching to avoid ghost segments.
6
Current
Explain why multiplexed digits look dimmer (duty = 1/N).

What this experiment demonstrates

  • Time-division multiplexing: 12 pins drive 32 LEDs.
  • How the human eye integrates fast light pulses into a steady image.
  • Code-memory look-up tables in C.

Sub-tasks

  • 9.1 Interface test: light every segment
  • 9.2 Display hex digits 0 to F
  • 9.3 Display 1234 (multiplexed)

Segments and codes

abcdefgdp Common-anode codes (P2.7…P2.0 = dp g f e d c b a, 0 = ON) 0:C0H1:F9H2:A4H3:B0H4:99H5:92H6:82H7:F8H8:80H9:90HA:88Hb:83HC:C6Hd:A1HE:86HF:8EH Multiplexing 4 digits All digits share segment lines a–dp (P2). Each digit's common anode is switched by a PNP transistor (P1.0–P1.3, active-low). Light one digit at a time for 2–5 ms. If the whole cycle repeats faster than ~50 Hz the eye sees all four digits steadily. Refresh rate = 1 / (digits × time per digit). Each digit is on 1/4 of the time, so drive it with enough current to look bright.
Fig 3. Seven-segment display: segment names, common-anode codes and multiplexing.

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.

Refresh rate

Refresh rate = 1 / (N_digits × t_digit) 4 digits × 3 ms = 12 ms → 83 Hz (steady) 4 digits × 20 ms = 80 ms → 12.5 Hz (visible flicker) Duty cycle per digit = 1/4 = 25 %

Board wiring (this lab)

SignalPinsActive level
Segments a…g, dpP2.0…P2.70 = ON (common anode)
Digit 1…4 selectP1.0…P1.30 = ON (PNP driver)

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
Display module4-digit common-anode 7-segment with PNP digit drivers1

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.

Procedure (hardware / real tools)

  1. Connect the segment lines to Port 2 and the digit drivers to P1.0–P1.3 with the power off.
  2. Build and program 9.1 (lamp test): all segments should light.
  3. Build and program 9.2: digits 0–F on digit 1.
  4. Build and program 9.3: "1234" multiplexed. Vary the per-digit delay (1, 3, 10, 20 ms) and record the flicker.
  5. Measure the refresh rate on an oscilloscope at a digit-select pin, if available.
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. Use 1/100× and 1/1000× slow motion to watch the digits being scanned one at a time.

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. 9.3: delay_ms(20): the display flickers. Calculate the refresh rate.
  2. 9.3: remove DIG = 0xFF; and look for ghosting in slow motion.
  3. 9.3: delay_ms(0): too fast? Look at the brightness.
  4. 9.2: change a table entry and see a wrong digit.
  5. 9.3: show "dEAd" by changing num[] and adding codes.
  6. 9.2: access hexseg[16] (out of range): the simulator stops; a real chip reads garbage.

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: 4 digits with 4 ms per digit. Refresh rate in Hz?

Self-evaluation

Tick what you can now do confidently.

Overall confidence:

Feedback summary

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