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
Derive common-anode segment codes for 0–F.
Store codes in code memory (unsigned char code table[]).
Scan digits one at a time and explain persistence of vision.
Calculate the refresh rate and the flicker threshold.
Blank the digits during switching to avoid ghost segments.
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
Software delay in C
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
Board wiring (this lab)
| Signal | Pins | Active level |
|---|---|---|
| Segments a…g, dp | P2.0…P2.7 | 0 = ON (common anode) |
| Digit 1…4 select | P1.0…P1.3 | 0 = ON (PNP driver) |
Apparatus & environment
| Item | Specification | Qty |
|---|---|---|
| PC with Keil µVision | C51/A51 tool chain (evaluation version is fine) | 1 |
| 8051 trainer board | Nuvoton W78E052D, 11.0592 MHz crystal | 1 |
| Nuvoton ISP-ICP utility | APROM programming over USB-UART (CH340) | — |
| USB cable | USB-A to the board's serial/ISP port | 1 |
| Power supply | +5 V regulated (board adapter) | 1 |
| Display module | 4-digit common-anode 7-segment with PNP digit drivers | 1 |
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)
- Connect the segment lines to Port 2 and the digit drivers to P1.0–P1.3 with the power off.
- Build and program 9.1 (lamp test): all segments should light.
- Build and program 9.2: digits 0–F on digit 1.
- Build and program 9.3: "1234" multiplexed. Vary the per-digit delay (1, 3, 10, 20 ms) and record the flicker.
- Measure the refresh rate on an oscilloscope at a digit-select pin, if available.
Doing it in this virtual lab
- Choose a sub-task chip. The program opens in the µVision window.
- Press 🔨 Build (F7). Errors are listed in Build Output: click one to jump to the line.
- Press ▶ Run (F5) and watch the board. Use ¼× to 1/1000× slow motion to see fast events.
- Use Step (F11) and Step Over (F10), and click line numbers to set breakpoints.
- Edit the code (delay, pattern, pins), rebuild, and observe the change. Press ● Record to log results.
- 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-task | Inputs | Expected | Simulator | Your value | Check | Note |
|---|
Edge cases to test
- 9.3: delay_ms(20): the display flickers. Calculate the refresh rate.
- 9.3: remove
DIG = 0xFF;and look for ghosting in slow motion. - 9.3: delay_ms(0): too fast? Look at the brightness.
- 9.2: change a table entry and see a wrong digit.
- 9.3: show "dEAd" by changing num[] and adding codes.
- 9.2: access hexseg[16] (out of range): the simulator stops; a real chip reads garbage.
Analysis & conclusion
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.
Self-evaluation
Tick what you can now do confidently.