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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
ORG 0000H
	
	BACK:MOV P0 , #00H
	
	ACALL DELAY
	MOV P0 , #0FFH
	ACALL DELAY
	
	SJMP BACK
	
	DELAY: MOV R2,#200
	AGAIN: MOV R3,#250
	HERE: NOP
		  NOP
	DJNZ R3, HERE
	DJNZ R2,AGAIN
	RET
END

Explanation

Main Loop

  1. MOV P0, #00H: Turn all LEDs ON (assuming active low configuration)
  2. ACALL DELAY: Wait for a period
  3. MOV P0, #0FFH: Turn all LEDs OFF
  4. ACALL DELAY: Wait for a period
  5. 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.