Interfacing switch with AT89S52

Thread Starter

@vajra

Joined May 2, 2018
154
Where is the GND connection on the second switch?
You caught my mistake. Thanks Mr.Chips I appreciate you. My current program is able to turn ON/OFF two LED's using two switches.

I don't want to stop here. Now I want to turn ON only one LED. If LED1 is ON then LED2 should be OFF and If LED2 is ON then LED1 should be OFF.

I have written code to perform new task.

C:
#include <REG51.h>
        
sbit Switch1  = P0^1;    //switch connected to P0.1
sbit Switch2  = P0^2;    //switch connected to P0.2

sbit LED1     = P2^1;    //LED connected to p2.1
sbit LED2     = P2^2;    //LED connected to p2.2

#define  ON    1
#define  OFF   0
#define  input 1
void main (void)

{
      Switch1 =  input;  // make P0.1 and P0.2 an input!!
      Switch2 =  input;

        LED1 = OFF;
        LED2 = OFF;

        while(1)
       {
            while(Switch1 == ON)        //If switch1 is ON
                {
                   LED1 = ON;        // Turn ON LED1
                }
            while(Switch2 == ON)        //If switch2 is ON
                {
                   LED2 = ON;        // Turn ON LED2
                }
  
      }
}
I know my program is not working as I want to do but I don't understand where is problem in code. It would be good if you can point out my mistake
 
Last edited:

Ian Rogers

Joined Dec 12, 2012
1,136
if(Switch1 == 1 )
{ LED1 = 1; LED2 = 0;}
if(Switch2 == 1 )
{ LED1 = 0; LED2 = 1;}

But you'll have a priority issue if switch 2 is on, switch1 is ignored..
 

MrChips

Joined Oct 2, 2009
30,806
There are four possible combinations of Switch1 and Switch2.
Draw a truth table of all combinations and the desired outcome of LED1 and LED2.

Switch1 Switch2 LED1 LED2
Off Off
On Off
Off On
On On

All you need is one additional line of code for the case when both Switch1 and Switch2 are On.
 

Thread Starter

@vajra

Joined May 2, 2018
154
There are four possible combinations of Switch1 and Switch2.
Draw a truth table of all combinations and the desired outcome of LED1 and LED2.

All you need is one additional line of code for the case when both Switch1 and Switch2 are On.
I wrote code for following table
Code:
S1   S2   LED1  LED2
Off  Off   off   off
On   Off   On   Off
Off  On    off   On
On   On    On    On
C:
#include <REG51.h>
    
sbit Switch1  = P0^1;    //switch connected to P0.1
sbit Switch2  = P0^2;    //switch connected to P0.2
sbit LED1     = P2^1;    //LED connected to p2.1
sbit LED2     = P2^2;    //LED connected to p2.2
#define  ON    1
#define  OFF   0
#define  input 1
void main (void)
{
      Switch1 =  input;  // make P0.1 and P0.2 an input!!
      Switch2 =  input;
        LED1 = OFF;
        LED2 = OFF;
        while(1)
       {
            if(Switch1 == ON && Switch2 == ON)              //If both is ON
                {
                   LED1 = LED2 = ON;                        // Turn ON LED1
                }
   
               else if(Switch1 == ON && Switch2 == OFF)       //If switch1 is ON
                {
                   LED1 = ON;        // Turn ON LED1
                                }
                        
                else if(Switch1 == OFF && Switch2 == ON)       //If switch2 is ON
                {
                   LED2 = ON;                                   // Turn ON LED2
                }
         
                 else
                   {
                            LED1 = LED2 = OFF;                            // Turn OFF Both
                   }
           }
}
This program is only working if I turn ON both switches before switch ON the power supply.

Program is not working if one of them switch is ON or OFF before switch ON the power supply.
 

Thread Starter

@vajra

Joined May 2, 2018
154
That doesn't even need processing!!!
C:
while(1)
  {
P2 = ~( P0 >>1);
  }
Read the switches and shift right, then output onto the led's
My calculation prove me wrong

ON = 1 and OFF = 0

When both switches ON
P2 = ~(00000110 >> 1) = ~ (00000011)
P2 = 11111100
It means both LED's will OFF

When switch2 ON
P2 = ~(00000010 >> 1) = ~ (00000001)
P2 = 11111110
It means both LED2 will ON

When switch1 ON
P2 = ~(00000001 >> 1) = ~ (00000000)
P2 = 11111111
It means both LED's will ON

When both switch OFF
P2 = ~(00000000 >> 1) = ~(00000000)
P2 = 11111111

It means both LED's will ON
 
Top