How to interface DIP switch with AT89C51

Thread Starter

@vajra

Joined May 2, 2018
154
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?
 

Attachments

JohnInTX

Joined Jun 26, 2012
4,787
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:

Thread Starter

@vajra

Joined May 2, 2018
154
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
 

Thread Starter

@vajra

Joined May 2, 2018
154
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:
#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 = OFF;
      LED    = OFF;

      while(1)               
    {
        if(Switch == ON)        //If switch is ON
              {
           LED = ON;          // Turn ON LED
        }
                else
                {
                     LED = OFF;          // Turn ON LED
                }
    }
}
Please help me What's wrong here
 
Top