Interfacing switch with AT89S52

Discussion in 'Embedded Systems and Microcontrollers' started by @vajra, May 20, 2018.

  1. @vajra

    Thread Starter Member

    May 2, 2018
    122
    1
    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 (Text):
    1. #include <REG51.h>    
    2.                      
    3. sbit Switch  = P0^1;    //switch connected to P0.1
    4. sbit LED     = P2^0;    //LED connected to p2.0
    5.  
    6. #define  ON    1
    7. #define  OFF   0
    8.  
    9. void main (void)
    10.  
    11. {
    12.          while(1)    
    13.        {
    14.                 if(Switch == ON)      //If switch is ON
    15.                 {
    16.                  LED = ON;          // Turn ON LED
    17.                 }
    18.  
    19.                 else
    20.                 {
    21.                      LED = OFF;         // Turn OFF LED
    22.                 }
    23.        }
    24. }
    AT89S52 wiring with switch

    switch2.jpg
     
    Last edited: May 20, 2018
  2. danadak

    Distinguished Member

    Mar 10, 2018
    2,886
    625
  3. MrChips

    Moderator

    Oct 2, 2009
    18,451
    5,849
    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.
     
  4. @vajra

    Thread Starter Member

    May 2, 2018
    122
    1
    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
    I have connected 550 ohms series resistor with green LED
     
  5. MrChips

    Moderator

    Oct 2, 2009
    18,451
    5,849
    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.
     
  6. AnalogKid

    AAC Fanatic!

    Aug 1, 2013
    7,757
    2,183
    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
     
  7. @vajra

    Thread Starter Member

    May 2, 2018
    122
    1
    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
     
  8. MrChips

    Moderator

    Oct 2, 2009
    18,451
    5,849
    My typo error. Should have been P0.1
     
  9. MrChips

    Moderator

    Oct 2, 2009
    18,451
    5,849
    Connect 550Ω to 5V.
    Connect P2.0 to LED.
     
  10. @vajra

    Thread Starter Member

    May 2, 2018
    122
    1
    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 ?
     
  11. MrChips

    Moderator

    Oct 2, 2009
    18,451
    5,849
    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.
     
  12. MrChips

    Moderator

    Oct 2, 2009
    18,451
    5,849
    Another way to connect the LED is shown below as "current sinking".

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

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


    [​IMG]
     
    @vajra likes this.
  13. @vajra

    Thread Starter Member

    May 2, 2018
    122
    1
    Okay.. As you told me I have connected +5V ---Resistor---- +LED - ---- Port P2.0

    switch3.jpg

    What's the wrong in code ?
     
  14. Ian Rogers

    Well-Known Member

    Dec 12, 2012
    608
    162
    Have you not even considered that the chip may not be running.... First test.. Reprogram to flash the LED!!
     
  15. MrChips

    Moderator

    Oct 2, 2009
    18,451
    5,849
    You have to send a 1 to P0.1.
     
  16. @vajra

    Thread Starter Member

    May 2, 2018
    122
    1
    I have tested chip before there is no problem its working
    Code (Text):
    1. #include <REG51.h>
    2. #define LED_Port P2
    3. void Delay (unsigned int loop);
    4. void main(void)
    5. {
    6. while (1)
    7.   {
    8.     Delay(60000);      // Wait 60 ms
    9.     LED_Port =~ LED_Port;        // turn on/off All LED's
    10.   }
    11. }
    12. void Delay (unsigned int loop)  // Delay function
    13. {
    14.     unsigned int i;
    15.   for  (i = 0; i <loop; i++);
    16.   {
    17.   }
    18. }
    I am not able to upload test video. I am uploading pictures

    switch4.jpg
     
  17. @vajra

    Thread Starter Member

    May 2, 2018
    122
    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: May 21, 2018
  18. Ian Rogers

    Well-Known Member

    Dec 12, 2012
    608
    162
    The mod mrChip wants is this
    Code (C):
    1.  
    2.  
    3. #include <REG51.h>  
    4.                    
    5. sbit Switch  = P0^1;    //switch connected to P0.1
    6. sbit LED     = P2^0;    //LED connected to p2.0
    7.  
    8.  
    9. #define  ON    1
    10. #define  OFF   0
    11.  
    12.  
    13. void main (void)
    14.  
    15.  
    16. {
    17.        Switch = 1; // make P0.1 an input!!
    18.         while(1)  
    19.        {
    20.              if(Switch == ON)      //If switch is ON
    21.                 {
    22.              LED = ON;          // Turn ON LED
    23.               }
    24.  
    25.  
    26.               else
    27.                {
    28.                     LED = OFF;         // Turn OFF LED
    29.                }
    30.       }
    31. }
    32.  
    33.  
     
    @vajra likes this.
  19. @vajra

    Thread Starter Member

    May 2, 2018
    122
    1
    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 (Text):
    1. #include <REG51.h>
    2.                  
    3. sbit Switch1  = P0^1;    //switch connected to P0.1
    4. sbit Switch2  = P0^2;    //switch connected to P0.2
    5.  
    6. sbit LED1     = P2^1;    //LED connected to p2.1
    7. sbit LED2     = P2^2;    //LED connected to p2.2
    8. #define  ON    1
    9. #define  OFF   0
    10. #define  input 1
    11. void main (void)
    12.  
    13. {
    14.       Switch1 =  input;  // make P0.1 and P0.2 an input!!
    15.         Switch2 =  input;
    16.         while(1)
    17.        {
    18.             if(Switch1 == ON)        //If switch1 is ON
    19.                 {
    20.                    LED1 = ON;        // Turn ON LED1
    21.                 }
    22.               else
    23.                {
    24.                     LED1 = OFF;      // Turn OFF LED
    25.                }
    26.                         if(Switch2 == ON)        //If switch2 is ON
    27.                 {
    28.                    LED2 = ON;        // Turn ON LED2
    29.                 }
    30.               else
    31.                {
    32.                     LED2 = OFF;         // Turn OFF LED2
    33.                }
    34.       }
    35. }
    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: May 21, 2018
  20. Ian Rogers

    Well-Known Member

    Dec 12, 2012
    608
    162
    Don't thank me.. MrChips spotted the input pin problem..

    The new code works fine... It must be a hardware issue..
     
Loading...