Push button interfacing with AT89S52

be80be

Joined Jul 5, 2008
2,395
If you set it like that the rest of your code may not happen
Your setting a input to 0 there no need to
You don't need that
Code:
PushButton_status = PushButton_Not_Pressed;
 

Thread Starter

@vajra

Joined May 2, 2018
154
If you set it like that the rest of your code may not happen Your setting a input to 0 there no need to You don't need that
I tested modified function but it's not worked
C:
#define PushButton_Pressed           0
#define PushButton_Not_Pressed       1

int check_PushButton(void)
{
  static int PushButton_status;

   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;
}
 

Ian Rogers

Joined Dec 12, 2012
1,136
Right!!!! Its a toggle..

if button is pressed and LED is on, turn LED off.
if button is pressed and LED is off, turn it on..

Burt!! 10,000 equates to 140mS which I thought was enough... If you use 50 it would read the switch 100's of times...
 

Thread Starter

@vajra

Joined May 2, 2018
154
Right!!!! Its a toggle..

if button is pressed and LED is on, turn LED off.
if button is pressed and LED is off, turn it on..

Burt!! 10,000 equates to 140mS which I thought was enough... If you use 50 it would read the switch 100's of times...
I checked my code with 100 times but its not working. What's wrong in post #23. I am getting confuse with only pushbutton function.
C:
#include<reg51.h>
sbit PushButton  = P0^0;
sbit LED  = P2^0;
#define PushButton_Not_Pressed  1
#define PushButton_Pressed      0
#define LED_OFF  0
#define LED_ON  1

#define PushButton_Pressed           0
#define PushButton_Not_Pressed       1
//Function for debouncing time
void Debounce(unsigned int M_count)
  {
int count;
  for (count = 0; count < M_count; count++);
  }
  
int check_PushButton(void)
{
   static int PushButton_status;
  
   if(PushButton == PushButton_Pressed)          // check if Button is pressed      
     {
             Debounce(100);                            //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
If you have a 12Mhz crystal... Each clock will be 1uS.. It takes about 14 cycles to do a integer for loop in C!!

14uS if you do this 50 times it will take 700uS.... Now seeing as you cannot press and release a button under 50mS the debounce period of 700uS is useless....

Nominally they say at least 20mS BUT!! this is for a switching device.. A human is really slow... 100mS is still too fast

so... 10,000 * 14uS is 140mS.... I normally give 250mS for a human to press and release a button, even then I do a beep to acknowledge a button press.

10,000 is still to small...

When we interface to a micro it is normal to see a button with a pullup resistor... This means when we press the button a LOW will be seen at the port... So I put " !pushButton " the '!' is NOT!!!
 

Thread Starter

@vajra

Joined May 2, 2018
154
If you have a 12Mhz crystal... Each clock will be 1uS.. It takes about 14 cycles to do a integer for loop in C!!
I have checked again code with 10000 times but led goes ON forever
Is it wrong function to return two value 0 or 1? I don't think because it should be return two value 0 (PushButton_Pressed ) and 1 (PushButton_Not_Pressed)
C:
int check_PushButton(void)
{
   static int PushButton_status;

   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;
}
 

be80be

Joined Jul 5, 2008
2,395
You can't use stuff like this your trying to set a input to a value you only read inputs
PushButton =1;//Configuring as input pin
 

Ian Rogers

Joined Dec 12, 2012
1,136
I have checked again code with 10000 times but led goes ON forever
What is you crystal speed???

You said that with my first code, the LED just flashed.... so the debounce(10000) must have been working fine..

This code works with your hardware..

C:
#include <reg52.h>  
  
sbit PushButton =   P0^0 ; //switch connected to P0.1
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 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
  }
  }
}
 

be80be

Joined Jul 5, 2008
2,395
So with a 8051 this is to set input PushButton =1;

This would do what I think your trying to do
Code:
int SWsave;
     SWsave = 1;
     if (PushButton == 0 )
     {
       SWsave = 0;
      Debounce(10000);
     }
     if (PushButton && SWsave == 0 )
     {
         LED =~ LED;  //Led On
  }
If your using button that's low going high you have to change it around but I would use a 10 k to vdd and read the button when it goes to ground that let's you use weak pullup's of the and then to can skip the 10 k using weak pullups uC.
But even using the 10 k it seems easier to read a button going to ground logic seem right to me.
 
Last edited:
Top