Aim
To interface a stepper motor with the 8051 and write programs to rotate it clockwise and anticlockwise, and to rotate it by a specified number N of steps.
Learning objectives
Explain how energising coils in sequence turns the rotor in discrete steps.
Generate wave, full-step and half-step sequences from look-up tables.
Reverse the direction by walking the sequence backwards.
Calculate the angle for N steps and the speed in rpm from the step delay.
Explain the role of the ULN2003 Darlington driver.
Recognise missed steps when stepping faster than the motor can follow.
What this experiment demonstrates
- Open-loop position control: no sensor is needed to turn exactly N × 1.8°.
- The torque/speed trade-off of the step delay.
- Look-up-table driven output sequences.
Sub-tasks
- 11.1 A Clockwise then anticlockwise
- 11.1 B Direction selected by a switch
- 11.2 Rotate by N steps
Excitation sequences
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.
Formulas
Board wiring (this lab)
| Coil | Pin |
|---|---|
| A | P2.0 |
| B | P2.1 |
| C | P2.2 |
| D | P2.3 |
| Direction switch | P3.0 (OFF = CW) |
The ULN2003 inverts: a pin at 1 turns its Darlington on and energises the coil. At reset P2 = FFH energises all coils at once, so programs write 0x00 first.
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 |
| Stepper motor | Unipolar, 1.8°/step, 5–12 V | 1 |
| Driver | ULN2003 module | 1 |
| Supply | Motor supply (separate from logic) | 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.
- 🌀 Keep fingers and loose clothing away from the shaft.
- 🔌 Use a separate motor supply. Never power the motor from the 5 V logic regulator.
- 🌡 Coils get hot when held energised. Release them (P2 = 0) when idle.
Procedure (hardware / real tools)
- Connect the ULN2003 inputs to P2.0–P2.3, the motor to the driver, and the motor supply (power off).
- Build and program 11.1 A. Mark the shaft and check one revolution clockwise, then one anticlockwise.
- Build 11.1 B and toggle the direction switch.
- Build 11.2 with N = 50 and measure the angle (should be 90°). Repeat with other values of N.
- Reduce the step delay until steps are missed and note the limit.
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
- Set the step delay to 1 ms: missed steps (the rotor cannot follow).
- Use the sequence 01H, 04H, 02H, 08H: invalid jumps make the rotor jitter.
- Remove
MOTOR = 0x00;: all coils are on at reset. - Use half-step with N = 50: what angle results?
- Change 200 to 100 in 11.1 A: how far does it turn?
- 11.1 B with the DIR latch written to 0: the direction switch no longer works.
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.