---
title: "Read switches at port 1, show on port 2"
subject: "Embedded System"
chapter: 9
key: "semester_6/embedded_system:8051-switch-port1-led-port2"
source: https://www.bctengineer.com/q/semester_6/embedded_system/8051-switch-port1-led-port2
site: BCT Engineering
---

# Read switches at port 1, show on port 2

Embedded System · Chapter 9: Microcontrollers in Embedded Systems

- Asked in: 2073 Magh, 4 marks; 2079 Jestha, 4 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-switch-port1-led-port2
- Whole chapter: https://www.bctengineer.com/md/semester_6/embedded_system/chapter-9

## Question

Write an assembly language programming for 8051 microcontroller to read the data from switches connected at port 1 and send it to port 2 for display in LED.

## Short answer

Set P1 as input (all 1s), read a switch into A, and output to P2 in a loop. Code: MOV P1,#0FFH; LOOP: MOV A,P1; MOV P2,A; SJMP LOOP. For active-low LEDs, complement A before the output.

## Answer

Port 1 is configured as an input by writing 1s to it; the switch value is read into A and written to Port 2 where the LEDs are connected (active high).

```asm
        ORG 0000H
        MOV P1,#0FFH      ; make Port 1 an input port
LOOP:   MOV A,P1          ; read the switches into A
        MOV P2,A          ; send to LEDs on Port 2
        SJMP LOOP         ; repeat forever
        END
```

Explanation:
- `MOV P1,#0FFH` writes 1 to every pin so that external switches can pull the pins down (input mode).
- `MOV A,P1` reads the pin levels; a closed switch to ground reads 0.
- `MOV P2,A` copies the pattern, so the LED pattern follows the switch settings. For LEDs wired active-low (anode to VCC) use `CPL A` before the output.
- The program was assembled and tested in an 8051 simulator: P1 = 5AH gives P2 = 5AH.

Same program in C:
```c
#include <reg51.h>
void main(void)
{
    P1 = 0xFF;              /* input port */
    while (1)
        P2 = P1;            /* copy switches to LEDs */
}
```

## Check yourself

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

1. Why is MOV P1,#0FFH executed first?
   A. To turn on LEDs
   B. To clear the port
   C. To send data out
   D. To write 1s so the pins can act as inputs
   Answer: D. Writing 1 to a port pin makes it an input so external switches can pull it down. (Key point: Input configuration)

2. What does MOV A,P1 do?
   A. Reads the pin levels of P1 into A
   B. Writes A to P1
   C. Loads constant P1
   D. Clears P1
   Answer: A. It reads the pin states; a switch closed to ground reads 0. (Key point: Reading switches)

3. Which instruction sends the switch pattern to the LEDs?
   A. MOV P1,A
   B. MOV A,P2
   C. MOV P2,A
   D. SJMP LOOP
   Answer: C. MOV P2,A copies A to Port 2. (Key point: Output to LEDs)

4. What change is needed if the LEDs are wired active-low?
   A. Use MOV P2,#0
   B. Use CPL A before output
   C. Swap ports
   D. Remove the loop
   Answer: B. Complementing A inverts the pattern so a switch state lights the right LED. (Key point: Active-low LEDs)

## Related in Embedded System

- [LED blink rate controlled by two switches](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-blink-rate-two-switches) — Embedded System, chapter 9, asked 2×
- [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
- [Get data from P0, send to P1 (asm and C)](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-p0-to-p1-assembly-c) — Embedded System, chapter 9

## Related in other subjects

- [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×
- [Define programming language](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Adefine-programming-language) — Computer Programming, chapter 1, 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
