Polling a Button
...................................................................................................................................
This chapter gives an idea of how flexible the I/O pins are, despite their simple design. The Program given below polls a button connected to one of the I/O pins (which pull the line to ground from its normal pulled-up state) and passes the state of the pin to another one and lights an LED when the button is being pressed.
Connect the following circuit in the bread board
Program to poll a button and turns on an LED
ORG 0000H ; Execution starts here
LOOP:SETB P0.3 ; Make port P0.3 as input port
MOV C,P0.3 ; Get the button state to carry bit
MOV P1.6,C ; Display button state on LED
SJMP LOOP ; do it continuously
END
Program to turn on the LED when the button is pressed once and to turn off the LED when it is pressed again
ORG 0000H ; Execution starts here
LOOP:SETB P0.3 ; Make port P0.3 as input port
MOV C,P0.3 ; Get the button state to carry bit
JC LOOP ; check if button is pressed
CPL P1.6 ; if pressed compliment the port
JNB P0.3,$ ; Wait until the button is released
SJMP LOOP ; do it continuously
END