---
title: "Blink 8 LEDs at port 2"
subject: "Embedded System"
chapter: 9
key: "semester_6/embedded_system:8051-blink-leds-port2"
source: https://www.bctengineer.com/q/semester_6/embedded_system/8051-blink-leds-port2
site: BCT Engineering
---

# Blink 8 LEDs at port 2

Embedded System · Chapter 9: Microcontrollers in Embedded Systems

- Asked in: 2072 Magh, 4 marks; 2075 Baisakh, 4 marks (asked 2 times)
- Video: [LED blinking using Keil- 8051 Microcontroller Embedded C program](https://www.youtube.com/watch?v=k4eKz4YO2kY) — CircuitSimulations
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_6/embedded_system/8051-blink-leds-port2
- Whole chapter: https://www.bctengineer.com/md/semester_6/embedded_system/chapter-9

## Question

Write an assembly language program to blink the 8 LEDs connected at Port 2 of the 8051 microcontroller.

## Short answer

Apply 0FFH to P2 (A-L LEDs ON), call a software delay, then 00H (OF-F), and repeat. Delay uses nested loops: outer 4, middle 250, inner 250 creating ~0.5 s at 12 MHz. Code: MOV P2,#0FFH; ACALL DEAY; MOV P2,#00H; ACALL DEAY; SJMP MAI.

## Answer

The LEDs on Port 2 are turned all on, a delay is generated, they are turned all off, and the cycle repeats. The delay routine uses three nested loops (about 0.5 s at 12 MHz).

```asm
        ORG 0000H
MAIN:   MOV P2,#0FFH      ; all LEDs ON (active-high)
        ACALL DELAY
        MOV P2,#00H       ; all LEDs OFF
        ACALL DELAY
        SJMP MAIN
DELAY:  MOV R5,#4         ; about 0.5 s at 12 MHz
D1:     MOV R6,#250
D2:     MOV R7,#250
D3:     DJNZ R7,D3
        DJNZ R6,D2
        DJNZ R5,D1
        RET
        END
```

Explanation:
- `MOV P2,#0FFH` / `MOV P2,#00H` switch all eight LEDs on and off (active-high LEDs).
- DELAY: R5 x R6 x R7 loop = 4 x 250 x (250 x 2 + 3) us, which is about 0.50 s at a 12 MHz crystal (machine cycle 1 us). In simulation the delay measured 0.503 s.
- To blink alternately, use patterns 0AAH and 55H instead.

In C:
```c
#include <reg51.h>
void delay(unsigned int ms) { unsigned int i, j; for (i = 0; i < ms; i++) for (j = 0; j < 120; j++); }
void main(void)
{
    while (1) {
        P2 = 0xFF;  delay(500);
        P2 = 0x00;  delay(500);
    }
}
```

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. How does the program blink the LEDs?
   A. Writes FFH, delays, writes 00H, delays and repeats
   B. Writes FFH once
   C. Reads Port 2 only
   D. Uses only a delay
   Answer: A. The port is alternately set to all on and all off with a delay between. (Key point: Blink logic)

2. How does the DELAY routine create a delay?
   A. Using the stack pointer
   B. By timer interrupts only
   C. By nested DJNZ loops
   D. By reading a port
   Answer: C. Three nested DJNZ counting loops burn machine cycles. (Key point: Delay loop)

3. Approximate delay for R5=4, R6=250, R7=250 at 12 MHz (DJNZ = 2 us)?
   A. About 5 ms
   B. About 50 ms
   C. About 5 s
   D. About 0.5 s
   Answer: D. 4 x 250 x (250 x 2 + 3) us is about 0.5 s. (Key point: Delay calculation)

4. Which patterns give alternate LED blinking?
   A. 0FFH and 00H
   B. 0AAH and 55H
   C. 00H and 00H
   D. 0FFH and 0FFH
   Answer: B. AAH and 55H light alternate LEDs. (Key point: Alternate blinking)

## Related in Embedded System

- [Purpose of port 3 and port 2 of 8051](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-port3-port2-purpose) — Embedded System, chapter 9, asked 2×
- [Port 3 of 8051](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-port3-explain) — Embedded System, chapter 9
- [Microcontroller vs microprocessor; 8051 features](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Amicro-vs-microprocessor-8051-features) — Embedded System, chapter 9, asked 2×
- [Microprocessor vs microcontroller; 8051 addressing](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-addressing-modes-micro-vs-processor) — Embedded System, chapter 9
- [Read switches at port 1, show on port 2](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-switch-port1-led-port2) — Embedded System, chapter 9, asked 2×
- [Precise 5 ms delay in 8051](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-5ms-delay) — Embedded System, chapter 9

## Related in other subjects

- [Address decoding for 8 LEDs output port](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Adecoder-output-port-8-leds) — Microprocessor, chapter 4
- [ADC at 80H with 8255A: display on port B LEDs](https://www.bctengineer.com/question.md?key=semester_5%2Finstrumentation_ii%3Aadc-8255-80h-leds) — Instrumentation II, chapter 2
- [8086 assembly language operators](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3A8086-operators) — Microprocessor, chapter 3, asked 3×
- [8255 mode 0 DIP switches and LEDs](https://www.bctengineer.com/question.md?key=semester_5%2Finstrumentation_ii%3Appi-mode0-dip-leds) — Instrumentation II, chapter 2
