On this page
LED Blinking with Delay Loop
This program demonstrates LED blinking using a software delay loop on the 8051 microcontroller.
Description
The program alternately turns all LEDs connected to Port 0 ON and OFF with a delay between transitions, creating a blinking effect.
Code
| |
Explanation
Main Loop
- MOV P0, #00H: Turn all LEDs ON (assuming active low configuration)
- ACALL DELAY: Wait for a period
- MOV P0, #0FFH: Turn all LEDs OFF
- ACALL DELAY: Wait for a period
- SJMP BACK: Repeat infinitely
Delay Subroutine
The delay is created using nested loops:
- Outer loop: R2 = 200 iterations
- Inner loop: R3 = 250 iterations
- Total delay ≈ 200 × 250 × (2 NOP + DJNZ cycles)
Approximate delay: ~0.5 seconds (depends on crystal frequency)
Delay Calculation
With 11.0592 MHz crystal:
- Machine cycle = 12/11.0592 MHz ≈ 1.085 µs
- Each iteration ≈ 4 machine cycles
- Total delay ≈ 200 × 250 × 4 × 1.085 µs ≈ 217 ms
This creates a visible blinking effect for LEDs connected to Port 0.