How to interface DIP switch with AT89C51

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

  1. @vajra

    Thread Starter Member

    May 2, 2018
    80
    1
    Hi everyone

    I have DIP switch and I want to interface as input to AT89C51. I don't understand how to connect it with AT89c51 I don't want to damage AT89C51. Which pins goes to 5v supply and Which pins goes to ground?. What size of resistor's would be safe for DIP Switch?
     
  2. JohnInTX

    Moderator

    Jun 26, 2012
    3,295
    1,639
    Usually, you add a pull-up resistor to 5V to each port pin and use each individual switch section to pull that port pin/resistor to ground. When the switch is closed (on) you will read a ‘0’ on the pin. Some Microcontrollers have built-in pull-up resistors that you can enable in software or in the programming options.

    External resistors aern’t too critical. Anything from 10K to 68K would work fine. Smaller would consume more power. Too much bigger and you have to start worrying about port bias currents and stuff.
     
    Last edited: May 18, 2018
  3. Cubix Kits and Projects

    New Member

    May 17, 2018
    1
    0
    You can refer this circuit
    [​IMG]

    Mod edit: deleted site promotion link
     
    Last edited by a moderator: May 18, 2018
  4. danadak

    Well-Known Member

    Mar 10, 2018
    1,242
    257
    This part has internal pullups, so no need for external.


    Regard
     
  5. @vajra

    Thread Starter Member

    May 2, 2018
    80
    1
    I have done connection on breadboard. I have connected led's to port P2 and DIP switch's to Port P0

    single black wire connected to negative of 5V supply
    single Red wire connected to positive of 5V supply

    Is it correct connection ?

    DIP_switch.jpg
     
  6. danadak

    Well-Known Member

    Mar 10, 2018
    1,242
    257
    So your leds are turned on when output goes high.

    Connections look OK.

    Regards, Dana.
     
  7. @vajra

    Thread Starter Member

    May 2, 2018
    80
    1
    I tried check switch connection weather its right or wrong I wrote program to turn ON led using the switch but led doesn't turn ON. I measured output voltage at Port pin P0.1. Its show only 0V. I directly connected to port pin p0.1 to ground but no success

    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.       Switch = OFF;
    12.       LED    = OFF;
    13.  
    14.       while(1)              
    15.     {
    16.         if(Switch == ON)        //If switch is ON
    17.               {
    18.            LED = ON;          // Turn ON LED
    19.         }
    20.                 else
    21.                 {
    22.                      LED = OFF;          // Turn ON LED
    23.                 }
    24.     }
    25. }
    Please help me What's wrong here
     
Loading...