LEDs Controlling thorugh Switches

Read the status of all momentary switches SW’s connected to PORT 0 and display their status of being pressed on corresponding LEDs connected to PORT 2 infinitely.

Required components

  • 8051 MCU
  • 8 LEDs
  • 8 Registers 330 ohm
  • Push to on switches
  • Jumpers

Circuit diagram

LEDs Controlling thorugh Switches circuit diaram

8 RED LEDs are connected to port2. 8 switches are connected to 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 pins.

Code

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 the ground Port2.5 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 LEDs, which in turn will glow the LED.

So when switch 0 is pressed.

Po will receive

P0 = 1111 1110

LEDP2 =~ SWP0; this line of code will give complement data to port2.

which means

P2 = ~P1;

which is

the complement of 1111 1110 is 0000 0001 which will turn on LED0. Same way respective Switch will turn on the respective LED.

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 *