Aim
To write Embedded C programs that flash LEDs with a time delay, flash LEDs alternately, implement an N-bit counter and a ring counter, and read eight keys and display their status on LEDs connected to Port 3.
Learning objectives
Write bytes to a port to drive LEDs, and use #define for readable names.
Write and calibrate a millisecond delay function.
Produce alternate (55H/AAH), binary-count and ring (rotating 1) patterns.
Configure a port as input by writing 1s and read switch states.
Convert active-low switch readings to active-high LED outputs with ~.
Use breakpoints and slow motion to verify timing.
What this experiment demonstrates
- The basic I/O loop of every embedded system: read inputs, compute, write outputs.
- How C types (unsigned char) wrap around, and how this can create endless loops.
- Quasi-bidirectional 8051 ports: the same pin is an output latch and an input.
Sub-tasks
- 7.1 Flash LEDs with a time delay
- 7.2 Flash LEDs alternately
- 7.3 N-bit counter and ring counter
- 7.4 Read 8 keys and show them on Port 3 LEDs
Ports and pins
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.
Patterns
Board wiring (this lab)
| Device | Port | Logic |
|---|---|---|
| 8 LEDs | P3.0āP3.7 | 1 = ON (buffered) |
| 8 keys / DIP switches | P1.0āP1.7 | ON = 0 (pull-up) |
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 |
| LED module | 8 LEDs with 330 Ī© resistors | 1 |
| Switch module | 8 DIP switches / keys with pull-ups | 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 LED module to Port 3 and the switch module to Port 1 with the board switched off.
- Create the Keil project (W78E052D, 11.0592 MHz, Create HEX).
- Type the program, build it, and test it in the µVision simulator (Peripherals ⸠I/O Ports).
- Program the board with the Nuvoton ISP tool and observe the LEDs.
- Measure the blink period with a stopwatch (10 periods Ć· 10) and compare it with the calculated delay.
- For 7.4, press each key and record the LED pattern.
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.
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
- 7.1: replace 115 with 1275 (the classic value). How long is the period now on a 12T core?
- 7.1: switch to the 1T core: compare the period.
- 7.2: delay_ms(5): the LEDs look half-bright. Check it in 1/100Ć slow motion.
- 7.3: change the loop to
for(count=0; count<300; count++): read the C294 warning. - 7.4: remove the
~: LEDs show the inverse. - 7.4: write
KEYS = 0x00: keys can no longer be read.
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.