This article is all about how to interface push button switches to an 8051 microcontroller. Push button switches are widely used in embedded system projects and the knowledge about interfacing them to 8051 is very essential in designing such projects. A typical push button switch has two active terminals that are normally open and these two terminals get internally shorted when the push button is depressed. Images of a typical pushbutton switch is shown below.
Pushbutton switch
Circuit diagram.
The circuit diagram for interfacing push button switch to 8051 is shown above. AT89S51 is the microcontroller used here. The circuit is so designed that when push button S1 is depressed the LED D1 goes ON and remains ON until push button switch S2 is depressed and this cycle can be repeated. Resistor R3, capacitor C3 and push button S3 forms the reset circuitry for the microcontroller. Capacitor C1, C2 and crystal X1 belongs to the clock circuitry. R1 and R2 are pull up resistors for the push buttons. R4 is the current limiting resistor for LED.
The Logic
The first instruction – MOV P0 #83H - is to turn LED off (Hex 83 in binary = 10000011) and to initialize switches 1 and 2. Switch 1 is connected to port 0.0 and switch 2 is connected to port 0.1. Also note that LED is connected to port 0.7.
Note:- Po.0 = 1 means switch 1 is OFF and Po.1 = 1 means switch 2 is OFF. P0.0 = o means switch 1 is ON and p0.1 = o means switch 2 is ON. LED turns ON when P0.7 = 0 and turns OFF when P0.7 = 1
The program has two labels – READSW and NXT. It’s all about reading switch values – that is P0.0 and P0.1. We are using RRC instruction to read switch values. The values of port 0 is moved to accumulator. Since port 0 and 1 are used to interface switches 1 and 2, we can get the values of both port bits in LSB”s 0 and 1 of accumulator by using MOV A,P0 instruction. RRC – means – rotate right through carry. You can learn more about this instruction here – 8051 programming tutorial 1 . What RRC do is simple – it will move value of port 0.0 to the carry bit. Now we can check the carry bit using instruction JC – which means “jump if carry is set” . If carry is SET – then it means port0.0 =1 and this means switch 1 is OFF. If switch 1 is OFF then we have to check status of switch 2 and that is why we jump to label NXT.
In the mean time if switch 1 is pressed – then value of port 0.0 will be equal to zero. This will get moved to accumulator and hence an RRC will result in carry bit = o. If carry bit = 0 then result of executing JC instruction is negative and it will not jump. The next instruction will get executed – that is CLR P0.7. This clears port 0.7 to zero and hence LED will turn ON. Once turned On- LED will be kept On until switch 2 is pressed.
The status of switch 2 is checked in NXT label. When NXT is executed, we are using RRC for the second time consecutively. This means, the carry bit now holds the value of P0.1 – which is status of switch 2. If carry bit = 1 then switch 2 is OFF. This means LED should not be turned OFF. If carry bit = 0 then LED should be turned OFF (The instruction SETB P0.7 turns LED OFF)