NADBYTE

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

Switch

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
Exit mobile version