How to Interface Button with 8051

Introduction

Hi guys in this article we will see how to interface switch with 8051 microcontroller. Whenever the switch is pressed LED glows. Let’s see how to do it.

Components Required

  • 8051 Microcontroller – 1
  • 8051 Program Dumper- 1
  • LED – 1
  • Push Button – 1
  • Breadboard – 1
  • Jumper wires

Switch

  • Input/output devices are critical components of an embedded system.
  • Switches are the primary input devices for an embedded system.
  • Switch allows the human to input binary information into the Micro Controller.

There are two ways to connect the switches

  1. Negative Logic
  2. Positive Logic

Switch Interfacing

Device Mapping

In Negative logic the default value of the input is 1(HIGH) when we press the button it will become 0(LOW)

In Positive logic the default value of the input is 0(LOW) when we press the button it will become 1(HIGH).

Code

#include <AT89X51.H>

/* Device Mapping */

sbit LED=P2^7;
sbit SW=P2^0;

void Delay(unsigned int t)
{
unsigned int i;
while(t--)
for(i=0;i<1257;i++);
}


main()
{
/* Device Initialization */

LED=0;
SW=1;

/* Superloop */
while(1)
{
if(SW==0)
{
Delay(5);
LED=1;
}
else
LED=0;
}
}

Thanks for reading this article for any assistance or doubts comment below.

Download Code

You may also like...

Leave a Reply

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

%d bloggers like this: