Interfacing switch with AT89S52

Thread Starter

@vajra

Joined May 2, 2018
154
I have connected switch and LED to AT89S52. I want to turn ON LED when switch is ON and turn of LED when switch is OFF. I have written code in Keil for interfacing switch but the problem is when switch is ON, LED doesn't turn ON and when switch is OFF, LED doesn't turn OFF. I measured the output voltage of pin P0.1 It show zero volt. I am trying to solve this problem from last 4-5 hours but nothing happening

Please check out these codes, wiring and help me
Code:
#include <REG51.h>     
                     
sbit Switch  = P0^1;    //switch connected to P0.1
sbit LED     = P2^0;    //LED connected to p2.0

#define  ON    1
#define  OFF   0

void main (void)

{
         while(1)     
       {
                if(Switch == ON)      //If switch is ON
                {
                 LED = ON;          // Turn ON LED
                }

                else
                {
                     LED = OFF;         // Turn OFF LED
                }
       }
}
AT89S52 wiring with switch

switch2.jpg
 
Last edited:

MrChips

Joined Oct 2, 2009
22,867
Read the AT89S52 datasheet and the tutorial posted by Dana.

You need to send a 1 to P0.0 to turn off the open drain output.
You need to provide a strong pull-up to the LED sufficient to drive current to the LED.

Come back and ask if you do not understand the meaning of open-drain, current source, and current sink.
 

Thread Starter

@vajra

Joined May 2, 2018
154
.You need to send a 1 to P0.0 to turn off the open drain output.
Hi MrChips I don't understand why I need to send a 1 to P0.0. there is nothing connected with P0.0 port pin. Switch is connected to P0.1 and LED connected to p2.0
.You need to provide a strong pull-up to the LED sufficient to drive current to the LED.
I have connected 550 ohms series resistor with green LED
 

MrChips

Joined Oct 2, 2009
22,867
Port P0 has open-drain outputs. Sending 0 to the output activates the FET and creates a short to GND. You want to disable the output FET by sending a 1 and allowing the internal pull-up resistor to give a logic HI at the output.

The 550Ω resistor should be connected between +5V and the LED.
The output of P2.0 is used to sink current and divert the current from the LED, i.e. pull the output to LOW.
 

AnalogKid

Joined Aug 1, 2013
8,925
This will go a lot faster if you post a schematic of your circuit, with a reference designator and value for each component.

I'm not familiar with the innards of this chip. Is it possible to set the port 0 pins to a totem pole output configuration?

ak
 

Thread Starter

@vajra

Joined May 2, 2018
154
This will go a lot faster if you post a schematic of your circuit, with a reference designator and value for each component.

ak
I don't have schematic of circuit but I can explain component value and wiring
  1. DIP switch
  2. 10 K ohm resistor
  3. LED
  4. 550 ohm resistor
Blue wire connected to 5v power supply
Black wire connected to negative of supply

Yellow wire connected to port Pin P2.0 and goes to one end of 550 ohm resistor and other end of resistor connected to positive end of led and other end of LED connected to ground

one end of 10 k ohm resistor connected to 5v and other goes to input of DIP switch and the output of DIP switch connected to ground

One end of green wire connected to port P0.1 and other end connected to middle of resistor and input of DIP switch

switch .jpg
 

Thread Starter

@vajra

Joined May 2, 2018
154
Connect 550Ω to 5V.
Connect P2.0 to LED.
MrChips I didn't understand why 550Ω should be connected to 5V ?

Why P2.0 should be connect directly to LED without series resistor ?

I think If I don't place 550Ω series resistor, LED can damage ?

Are you sure, it's only hardware problem there is nothing wrong in code ?
 

MrChips

Joined Oct 2, 2009
22,867
You have problems in both code and hardware.
You need to understand the meaning of current source and current sink.
You need to understand the meaning of open-collector and open-drain.

All of this is already mentioned in post #2 and #3.
 

MrChips

Joined Oct 2, 2009
22,867
Another way to connect the LED is shown below as "current sinking".

+5V---/\/\/\/\------l>|------P2.0

+V ---Resistor---- +LED - ---- Port


 

Thread Starter

@vajra

Joined May 2, 2018
154
Have you not even considered that the chip may not be running.... First test.. Reprogram to flash the LED!!
I have tested chip before there is no problem its working
Code:
#include <REG51.h>
#define LED_Port P2
void Delay (unsigned int loop);
void main(void)
{
while (1)
  {
    Delay(60000);      // Wait 60 ms
    LED_Port =~ LED_Port;        // turn on/off All LED's
  }
}
void Delay (unsigned int loop)  // Delay function
{
    unsigned int i;
  for  (i = 0; i <loop; i++);
  {
  }
}
I am not able to upload test video. I am uploading pictures

switch4.jpg
 

Thread Starter

@vajra

Joined May 2, 2018
154
You have to send a 1 to P0.1.
Have you looked post # 13 ?

Now Is it correct connection ?

As per my knowledge I don't see any problem in code. As you told I did the same connection but still there is no output result

Where is the problem in connection or code ?
 
Last edited:

Ian Rogers

Joined Dec 12, 2012
819
The mod mrChip wants is this
C:
#include <REG51.h>   
                   
sbit Switch  = P0^1;    //switch connected to P0.1
sbit LED     = P2^0;    //LED connected to p2.0


#define  ON    1
#define  OFF   0


void main (void)


{
       Switch = 1; // make P0.1 an input!!
        while(1)   
       {
             if(Switch == ON)      //If switch is ON
                {
             LED = ON;          // Turn ON LED
              }


              else
               {
                    LED = OFF;         // Turn OFF LED
               }
      }
}
 

Thread Starter

@vajra

Joined May 2, 2018
154
The mod mrChip wants is this
Thanks a lot mrChip and Ian, I am very happy. I can turn ON/OFF LED using switch. It's what I wanted to do

Now I went for further steps. I wanted to turn ON/OFF LED's using switches

Switch1------l>|------LED1
Switch2------l>|------LED2

Code:
#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;
        while(1) 
       {
            if(Switch1 == ON)        //If switch1 is ON
                {
                   LED1 = ON;        // Turn ON LED1
                }
              else
               {
                    LED1 = OFF;      // Turn OFF LED
               }
                        if(Switch2 == ON)        //If switch2 is ON
                {
                   LED2 = ON;        // Turn ON LED2
                }
              else
               {
                    LED2 = OFF;         // Turn OFF LED2
               }
      }
}
My switch1 is working but the second switch2 is not working. I think there is no problem in connection so I think there may be problem in code. As per my knowledge I have written code to perform task.

What's the wrong ?
 
Last edited:
Top