šŸ’”
MPMC Lab Ā· Experiment 7 Ā· 8051 Ā· Embedded C

LED and Switch Interfacing Using Embedded C

Flash LEDs, alternate them, N-bit and ring counters, and mirror 8 keys onto LEDs on Port 3.

☰ All experiments

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

1
Port output
Write bytes to a port to drive LEDs, and use #define for readable names.
2
Software delay
Write and calibrate a millisecond delay function.
3
Patterns
Produce alternate (55H/AAH), binary-count and ring (rotating 1) patterns.
4
Port input
Configure a port as input by writing 1s and read switch states.
5
Active-low logic
Convert active-low switch readings to active-high LED outputs with ~.
6
Debugging
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

8051 P3.x 330 Ī© GND (pin = 1 → LED ON) P1.x 10k pull-up → VCC switch closed → pin reads 0 Reading an input pin 1. Write 1 to the pin first (P1 = 0xFF): it now floats high through the internal pull-up. 2. Read the pin: 1 = switch open, 0 = closed.If the latch holds 0, the pin is clamped lowand always reads 0.
Fig 3. Driving an LED and reading a switch on 8051 port pins.

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.

Patterns

Flash: P3 = 0xFF; delay; P3 = 0x00; delay; Alternate: 0x55 = 0101 0101 ↔ 0xAA = 1010 1010 N-bit up-counter: 0 … 2^N āˆ’ 1, mask = (1 << N) āˆ’ 1 Ring counter: 0000 0001 → 0000 0010 → … → 1000 0000 → repeat Keys → LEDs: LEDS = ~KEYS; (pressed key = 0 → LED = 1)

Board wiring (this lab)

DevicePortLogic
8 LEDsP3.0–P3.71 = ON (buffered)
8 keys / DIP switchesP1.0–P1.7ON = 0 (pull-up)

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
LED module8 LEDs with 330 Ī© resistors1
Switch module8 DIP switches / keys with pull-ups1

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 LED module to Port 3 and the switch module to Port 1 with the board switched off.
  2. Create the Keil project (W78E052D, 11.0592 MHz, Create HEX).
  3. Type the program, build it, and test it in the µVision simulator (Peripherals ā–ø I/O Ports).
  4. Program the board with the Nuvoton ISP tool and observe the LEDs.
  5. Measure the blink period with a stopwatch (10 periods Ć· 10) and compare it with the calculated delay.
  6. For 7.4, press each key and record the LED pattern.
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.

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. 7.1: replace 115 with 1275 (the classic value). How long is the period now on a 12T core?
  2. 7.1: switch to the 1T core: compare the period.
  3. 7.2: delay_ms(5): the LEDs look half-bright. Check it in 1/100Ɨ slow motion.
  4. 7.3: change the loop to for(count=0; count<300; count++): read the C294 warning.
  5. 7.4: remove the ~: LEDs show the inverse.
  6. 7.4: write KEYS = 0x00: keys can no longer be read.

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: how many states does a 5-bit binary counter have?

Self-evaluation

Tick what you can now do confidently.

Overall confidence:

Feedback summary

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