Push button interfacing with AT89S52

Thread Starter

@vajra

Joined May 2, 2018
154
.
I am connecting push button to the first bit of PORT 0 (P0.0). The LED is connected to the first bit of PORT 2 (P2.0). I am trying to get an LED to light on when i press a push button. I am using AT89S52 to tests Push Button operation.

I have connected yellow wire to positive and Black wire connected to negative end of breadboard for 5V DC.
Blue connected to Port P0.0 and Green connected to P0.0

Push Button with Resistor = 10 K ohms

push button.jpg
C:
#include <reg51.h>

sbit PushButton  = P0^0;    //PushButton connected to P0.0
sbit LED         = P2^0;    //LED connected to p2.0

#define  ON      1
#define  OFF     0

#define  press   1

void main()
{

PushButton = 1;                   // make P0.0 an input!!

while(1)  // For ever
  {
     if(PushButton == press)         // if press Push button
     {
        LED = OFF;                  
    
         while(PushButton == press);  // Wait here for release of push button
     }
        LED = ON;
  }
}
I am not getting expected result. I think there should be no problem with hardware connection. I have doubt in program. May be my program is wrong
 
Last edited:

Thread Starter

@vajra

Joined May 2, 2018
154
What is wrong with this:
Nothing wrong with that code. I checked it when I press push button LED glow. Every pushing it glow
C:
#include <reg51.h>
sbit PushButton  = P0^0;    //PushButton connected to P0.0
sbit LED         = P2^0;    //LED connected to p2.0

void main()
{

PushButton = 1;                   // make P0.0 an input!!
  while (1)
{
    LED = PushButton;
}
}
I am just little bit confused about debouncing. When to use button debouncing. I searched on internet about button debouncing but I didn't understand.

I want to see the effect of debouncing on hardware (LED with Push Button). I want to see practically with code

what happen when we don't use debounce and what happen when we use debounce ?
 
Last edited:

MrChips

Joined Oct 2, 2009
30,701
Switch debouncing is essential (i.e. absolutely necessary) when you need to count the number of times a button is pressed.

For your simple application, LED = Pushbutton, you do not need switch debounce.

Here is an example where you need switch debounce: Push ON - Push OFF.
Press the button and LED goes ON. Release the button and the LED stays ON.
Press the button again and the LED goes OFF.

You can do the debounce in software. Bear in mind that switch bounce can last for 10ms. You need to test the switch about 50ms after the last action to make sure the bounce is over.

See if you can write the code for this.
 

Thread Starter

@vajra

Joined May 2, 2018
154
See if you can write the code for this.
I wrote and tested code with AT89S52 but it's not working as you wanted.
C:
#include<reg51.h>

sbit PushButton  = P0^0;              //PushButton connected to P0.0
sbit LED         = P2^0;              //LED connected to p2.0

#define PushButton_Not_Pressed   0
#define PushButton_Pressed       1

#define LED_OFF                  0
#define LED_ON                   1

//Function for debouncing time
void Debounce(unsigned int count)
    {
         for (count = 0; count < 50000; count++);
         {
         }
        }

//Function to check the status of Push button
int check_PushButton(void)
{
  int PushButton_status;

     PushButton_status = PushButton_Not_Pressed;
  if(PushButton == PushButton_Pressed )                               // check if Button is pressed
     {
        Debounce(50);                                                          //Wait for bouncing period        
        if(PushButton == PushButton_Pressed )                           // check Push Button is pressed again
                {
           PushButton_status  = PushButton_Pressed  ;
                }
     }
     return PushButton_status;
}

    
//Program start from here
void main(void)
{
   LED = 0;             //configuring as output pin
   PushButton = 1;       //Configuring as input pin
   while(1)
   {
     if( PushButton == check_PushButton()) //Check the Push Button status
       {
         LED = LED_ON;     //Led On
       }
      else
       {
         LED = LED_OFF;    //Led off
       }
   }
}
 

shteii01

Joined Feb 19, 2010
4,644
I have configured P2
Code:
   LED = 0;             //configuring as output pin
   PushButton = 1;       //Configuring as input pin
I noticed another thing.

You are using LED=LED_ON. But earlier in the code it is commented out. So it does not really mean anything.

I will fire up Keil simulator when I get home and do a simple program instead of fancy labels like you are doing. This simple task should not be taking this much work.
 

Ian Rogers

Joined Dec 12, 2012
1,136
In "Debounce()" You pass a variable count, but the count is then changed and counts to 5000... so Debounce() isn't variable!!

C:
void Debounce(unsigned int M_count)
  {
 int count;
  for (count = 0; count < M_count; count++);
  }
Why are you doing this " if(pushbutton == check_pushbutton())" As it will always be true!!
if(check_pushbutton()) is enough!!
 

Thread Starter

@vajra

Joined May 2, 2018
154
In "Debounce()" You pass a variable count, but the count is then changed and counts to 5000... so Debounce() isn't variable!!
Why are you doing this " if(pushbutton == check_pushbutton())" As it will always be true!!
if(check_pushbutton()) is enough!!
I did the modification in code but it's not happening as I want it.
C:
#include<reg51.h>

sbit PushButton  = P0^0;              //PushButton connected to P0.0
sbit LED         = P2^0;              //LED connected to p2.0

#define PushButton_Not_Pressed   0
#define PushButton_Pressed       1

#define LED_OFF                  0
#define LED_ON                   1

#define DEBOUNCE_VALUE  50

//Function for debouncing time
void Debounce(unsigned int count)
    {
         for (count = 0; count <DEBOUNCE_VALUE ; count++);
         {
         }
        }

//Function to check the status of Push button
int check_PushButton(void) 
{
  int PushButton_status;
   
     PushButton_status = PushButton_Not_Pressed;
    if(PushButton == PushButton_Pressed )                               // check if Button is pressed
     {
        Debounce(DEBOUNCE_VALUE);                                              //Wait for bouncing period           
        if(PushButton == PushButton_Pressed )                           // check Push Button is pressed again
                {
           PushButton_status  = PushButton_Pressed  ;
                }
     }
     return PushButton_status;
}

       
//Program start from here
void main(void)
{
   LED = 0;             //configuring as output pin
   PushButton = 1;       //Configuring as input pin
   while(1) 
   {
     if(check_PushButton()) //Check the Push Button status
       {
         LED = LED_ON;     //Led On
       }
      else
       {
         LED = LED_OFF;    //Led off
       }
   }
}
 

Ian Rogers

Joined Dec 12, 2012
1,136
Mine does...

C:
#include<reg51.h>
sbit PushButton  = P0^0;
sbit LED         = P2^0;

#define PushButton_Not_Pressed  0
#define PushButton_Pressed  1
#define LED_OFF  0
#define LED_ON  1
//Function for debouncing time
void Debounce(unsigned int M_count)
  {
 int count;
  for (count = 0; count < M_count; count++);
  }
//Function to check the status of Push button
int check_PushButton(void)
{
 int PushButton_status;
 PushButton_status = PushButton_Not_Pressed;
 if(PushButton == PushButton_Pressed )  // check if Button is pressed
  {
  Debounce(50);  //Wait for bouncing period 
  if(PushButton == PushButton_Pressed )  // check Push Button is pressed again
  {
  PushButton_status  = PushButton_Pressed  ;
  }
  }
  return PushButton_status;
}
 
//Program start from here
void main(void)
{
  LED = 0;  //configuring as output pin
  PushButton = 1;  //Configuring as input pin
  while(1)
  {
  if( check_PushButton()) //Check the Push Button status
  {
  LED = LED_ON;  //Led On
  }
  else
  {
  LED = LED_OFF;  //Led off
  }
  }
}
 

ArakelTheDragon

Joined Nov 18, 2016
1,362
I am not very familiar with this processor coding, but you can not use 50 as a 1 time test. You need to test every 7ms for 8 samples or 56times for 56ms. If the result is different 1, you restart the test.
 

Thread Starter

@vajra

Joined May 2, 2018
154
Mine does...
How did you test your? I tested your code on my 8051 development board. It's not working for me.

When I press the button LED goes ON. and When I release the button LED goes OFF

My program should do this
Press the button and LED goes ON. Release the button and the LED stays ON.
Press the button again and the LED goes OFF
 

Ian Rogers

Joined Dec 12, 2012
1,136
Press the button and LED goes ON. Release the button and the LED stays ON.
Press the button again and the LED goes OFF
That isn't what your code is doing....
You need to toggle the status like this..

Also your debounce is tiny!!! I use a debounce of 100000 ( @12Mhz crystal) it is about 140mS

C:
#include<reg51.h>

sbit PushButton  = P0^0;
sbit LED  = P2^0;

#define PushButton_Not_Pressed  0
#define PushButton_Pressed  1
 
#define LED_OFF  0
#define LED_ON  1
 
//Function for debouncing time
void Debounce(unsigned int M_count)
  {
 int count;
  for (count = 0; count < M_count; count++);
  }
 
//Function to check the status of Push button
int check_PushButton(void)
{
 static int PushButton_status;
 if(PushButton == PushButton_Pressed  &&  PushButton_status == PushButton_Not_Pressed )  // check if Button is pressed
  {
  Debounce(10000);  //Wait for bouncing period  
  if(PushButton == PushButton_Pressed )  // check Push Button is pressed again
  {
 PushButton_status  = PushButton_Pressed  ;
  }
  }
 else if (PushButton == PushButton_Pressed  &&  PushButton_status == PushButton_Pressed )  
 {
 Debounce(10000);  //Wait for bouncing period  
  if(PushButton == PushButton_Pressed )  // check Push Button is pressed again
  {
 PushButton_status  = PushButton_Not_Pressed  ;
  }
 }
  return PushButton_status;
}
 
  
//Program start from here
void main(void)
{
  LED = 0;  //configuring as output pin
  PushButton = 1;  //Configuring as input pin
  while(1)
  {
  if( check_PushButton()) //Check the Push Button status
  {
  LED = LED_ON;  //Led On
  }
  else
  {
  LED = LED_OFF;  //Led off
  }
  }
}
 

Thread Starter

@vajra

Joined May 2, 2018
154
That isn't what your code is doing....
You need to toggle the status like this..Also your debounce is tiny!!! I use a debounce of 100000 ( @12Mhz crystal) it is about 140mS
Hi Ian, I tested your program on my board. When I run your program. Led is continuously blinking.

Have you tested your code ? Which microcontroller and compiler IDE you are using ?
 

Ian Rogers

Joined Dec 12, 2012
1,136
Remember if you have a pullup resistor you are checking for a 0 on the port!! Just swap it over to wait for a 0!!

pushButton = 1... !pushButton = 0;

C:
int check_PushButton(void)
{
 static int PushButton_status;
 if(!PushButton == PushButton_Pressed  &&  PushButton_status == PushButton_Not_Pressed )  // check if Button is pressed
  {
  Debounce(10000);  //Wait for bouncing period  
  if(!PushButton == PushButton_Pressed )  // check Push Button is pressed again
  {
 PushButton_status  = PushButton_Pressed  ;
  }
  }
 else if (!PushButton == PushButton_Pressed  &&  PushButton_status == PushButton_Pressed )  
 {
 Debounce(10000);  //Wait for bouncing period  
  if(!PushButton == PushButton_Pressed )  // check Push Button is pressed again
  {
 PushButton_status  = PushButton_Not_Pressed  ;
  }
 }
  return PushButton_status;
}
 

Thread Starter

@vajra

Joined May 2, 2018
154
Remember if you have a pullup resistor you are checking for a 0 on the port!! Just swap it over to wait for a 0!!
Thanks Ian. Your code is working but I am curious to know what's the wrong in my function.
C:
#define PushButton_Pressed           0

#define PushButton_Not_Pressed       1

int check_PushButton(void)
{
  int PushButton_status;

  PushButton_status = PushButton_Not_Pressed;

   if(PushButton == PushButton_Pressed)          // check if Button is pressed
     {
          Debounce(10000);                            //Wait for bouncing period
     
          if(PushButton == PushButton_Pressed)  // check Push Button is pressed again
                 {                                   
                      PushButton_status  = PushButton_Pressed ;
                 }
     }

    return PushButton_status;
}
 
Last edited:
Top