4 Switch status to single LED

Read the status of 4 momentary switches SW’s connected to lower nibble of PORT 0 and display their status on an LED connected to P2.0 only.

Required components

  • 8051 MCU
  • 1 LED
  • Registers 330 ohm
  • 4 Push to on switches
  • Jumpers

Circuit diagram

4SwitchStatustoLED

1 RED LED is connected to the 0th at port2. 4 switches are connected to the lower nibble of port0.

As the cathode of LED is connected to the Ground terminal, we just need to provide a positive voltage to LED`s Anode terminal. So in order to turn on the LED we just need to give HIGH logic (1) to port pin(P2.0).

Code

Header files required:- delay.h

Download it from here

Code description

To turn on the LED when the switch is pressed, we just need to read the switch status, and based on the switch status just have to turn on LEDs one by one.

Initially, Port0 is initialized with 0xff. so when the switch is pressed and as another terminal of the switch is connected to ground port 0 will receive logic 0, port 2 is initialized with 0x00 which will make all LEDs to off at the beginning.

Once the switch is pressed, it will give high logic to LED, which in turn will glow the LED.

When switch 0 is pressed it will give value 1 to count variable. so function LED_On(1) will be called which will continuously flash the LED one time before taking a pause, same way it will work for switch 1,2, and 3.

Note:- Code can be improved a lot by interrupt handling that you will see in the upcoming post.

Download Proteus Circuit and Hex File

Video

Thank you for reading…

Leave a Comment

Your email address will not be published. Required fields are marked *