---
title: "LED blink rate controlled by two switches"
subject: "Embedded System"
chapter: 9
key: "semester_6/embedded_system:8051-blink-rate-two-switches"
source: https://www.bctengineer.com/q/semester_6/embedded_system/8051-blink-rate-two-switches
site: BCT Engineering
---

# LED blink rate controlled by two switches

Embedded System · Chapter 9: Microcontrollers in Embedded Systems

- Asked in: 2076 Baisakh, 6 marks; 2078 Baisakh, 6 marks (asked 2 times)
- 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-rate-two-switches
- Whole chapter: https://www.bctengineer.com/md/semester_6/embedded_system/chapter-9

## Question

Using 8051 instructions, control rate of blink of LED at pin P1.1 by two switches at P2.1 and P2.2. (One switch to increase the rate of blink, another to decrease the rate of blink)

## Short answer

The LED at P1.1 toggles with half-period R2×10 ms. Switches active low: pressing P2.1 decrements R2 (min 1) for faster blink, pressing P2.2 increments R2 (max 255) for slower blink.

## Answer

#### Idea

The LED at P1.1 is toggled forever. The half-period is R2 x 10 ms. Switch S1 (P2.1) decreases R2 (faster blink) and switch S2 (P2.2) increases R2 (slower blink). Switches are active low (pressed = 0, with pull-ups). Limits keep R2 within 1 to 255.

```text
 Start --> R2 = 20 --> toggle P1.1 --> wait R2 x 10 ms
            ^                              |
            |   S2 pressed & R2<255: R2++ |
            +--  S1 pressed & R2>1 : R2-- <+
```

#### Program

```asm
        ORG 0000H
        MOV P2,#0FFH       ; P2 as input (write 1s), switches active low
        MOV R2,#20         ; half-period = 20 x 10 ms = 200 ms
MAIN:   CPL P1.1           ; toggle LED
        MOV A,R2
        MOV R3,A           ; R3 = number of 10 ms units
WAIT:   ACALL DELAY10      ; 10 ms
        DJNZ R3,WAIT
        JB P2.1,CHK2       ; P2.1 = 1 -> "faster" switch not pressed
        CJNE R2,#1,DECR    ; already at minimum (fastest)?
        SJMP CHK2
DECR:   DEC R2             ; shorter delay = faster blinking
CHK2:   JB P2.2,MAIN       ; P2.2 = 1 -> "slower" switch not pressed
        CJNE R2,#0FFH,INCR ; already at maximum (slowest)?
        SJMP MAIN
INCR:   INC R2             ; longer delay = slower blinking
        SJMP MAIN

DELAY10: MOV R6,#20        ; about 10 ms at 12 MHz
L1:     MOV R7,#248
L2:     DJNZ R7,L2
        DJNZ R6,L1
        RET
        END
```

Explanation:
- `MOV P2,#0FFH` makes P2 an input port.
- `CPL P1.1` toggles the LED; the delay is R2 repetitions of `DELAY10` (10 ms), so the half-period is R2 x 10 ms (200 ms initially, 2.5 Hz blinking).
- `JB P2.1,CHK2` skips if S1 is not pressed; if pressed and R2 is not yet 1, `DEC R2` makes the delay shorter, so the blink is faster.
- `JB P2.2,MAIN` skips if S2 is not pressed; if pressed and R2 is not yet 255, `INC R2` makes the blink slower.
- In simulation, holding S1 reduced the half-period 0.20 s to 0.19 s to 0.18 s per toggle and S2 raised it again.

## Check yourself

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

1. What variable sets the blink half-period?
   A. R2, multiplied by 10 ms
   B. The accumulator
   C. P2
   D. DPTR
   Answer: A. The LED stays for R2 x 10 ms between toggles. (Key point: Rate control idea)

2. To blink faster the program should
   A. Increase R2
   B. Toggle P2
   C. Decrease R2
   D. Increase the crystal
   Answer: C. A smaller R2 shortens the half-period. (Key point: Faster blink)

3. Why does JB P2.1,CHK2 skip the decrement when P2.1 = 1?
   A. Switches are active high
   B. The switch is active low, so 1 means not pressed
   C. R2 is zero
   D. P2 is output
   Answer: B. With pull-ups a pressed switch reads 0, so a 1 means not pressed. (Key point: Switch reading)

4. Why does the program compare R2 with 1 and 0FFH?
   A. To blink the LED
   B. To read the ports
   C. To select a bank
   D. To keep R2 within 1 to 255 and avoid wrapping around
   Answer: D. Without limits R2 could wrap to 0 or 255 and give wrong delays. (Key point: Limits)

## Related in Embedded System

- [8051 pin diagram; ports 1 and 2](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-pin-diagram-ports-1-2) — Embedded System, chapter 9
- [8051 minimum circuit and 75% duty pulse](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-min-config-pwm-75) — Embedded System, chapter 9
- [1 kHz square wave on P1.5 using timer](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-square-wave-timer) — Embedded System, chapter 9
- [Blink 8 LEDs at port 2](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-blink-leds-port2) — Embedded System, chapter 9, asked 2×
- [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×

## Related in other subjects

- [Liang-Barsky: P1(-5,3), P2(15,9)](https://www.bctengineer.com/question.md?key=semester_5%2Fcomputer_graphics%3Aliang-barsky-0-0-10-10) — Computer Graphics, chapter 3
- [N-R one iteration, P2 = 0.4 pu](https://www.bctengineer.com/question.md?key=semester_5%2Fpower_system_analysis_ii%3Anr-one-iteration-y40-p0-4) — Power System Analysis II, chapter 2
- [Copy contents of one file to another](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-copy-contents) — Computer Programming, chapter 10, asked 2×
- [Two quadrant armature controlled DC drive; closed loop](https://www.bctengineer.com/question.md?key=semester_7%2Futilization_of_electrical_energy%3Atwo-quadrant-armature-control-loop) — Utilization of Electrical Energy, chapter 3
