Kit - Using Switches as Inputs
Up until now, all the
microcontroller projects have just done what
they were programmed to do without any input
from us. You have probably already thought that
it would be nice to have the microcontroller do
something based on inputs it receives from the
real world. This tutorial shows how to use a
switch as an input so that you can control what
the microcontroller is doing.
In this tutorial, we
will use the 7 segment display as we connected
it in the previous tutorial (Using
a 7 segment display). The switch will be
used to change what is shown on the display.
The type of switch we
will be using is called a normally open switch.
This is often abbreviated as NO switch where NO
stands for Normally Open. The switch has 2 legs
(connectors). Normally Open means that the
connectors are not connected (forming an open
circuit) unless you press the switch. When you
press the switch, a connection is made between
the two connectors, creating a short circuit (or
closed circuit). You can also get Normally
Closed switches (NC) which means that they are
closed, forming a short circuit, unless you are
pressing the button.
Our switch is really a
button rather than a switch because you have to
hold it down to maintain the connection between
the two points. These types are more useful
with microcontrollers than a true switch. A
true switch lets you flip between open and
closed without having to hold down the button.
Inputs to the Microcontroller
To use one of the input/output pins of the
8051 microcontroller as an input, you must write
a '1' to that pin.
Then in the software, you can check to see if
the input is a '0' or a '1' and do different
operations based on that input.
Use the same hardware configuration that you
have after the 7 segement display tutorial. Add
in the switch by connecting one wire to Pin 2 of
the 2051 (that is Port 3, Bit 0) and connecting
the other wire to ground. Now when the button
is not pressed, pin 2 will be left unconnected
to anything and when the software checks (reads)
pin 2, it will see the '1' that we wrote there
to make it an input pin. When the button is
pressed, pin 2 will be connected to ground and
when the software checks pin 2, it will see a
Our first program will constantly check the
input to see if the input (P3.0) is a '0' or a
'1'. This is called polling.
We will use the command JB (Jump
if Bit is set) to check if pin 2 is a '1' or a
'0'. (If pin 2 is '1' then we say that the
bit is set. In this case the bit is P3.0 so we
would say P3.0 is set.) Below is the
little bit of assembly code we will use to
monitor the input and change the display.
;This is required to use
P3.0 as an input
NOT_PRESSED ; If
the button is not pressed, skip the next line
;Display '0' on the 7 segment display
;Jump back up to LOOP:
'1' on the 7 segment display
;go to LOOP(always jump back to point labeled
The first line sets up pin 2 as an input by
writing a '1' to P3.0. Then "JB P3.0,
NOT_PRESSED" checks to see if P3.0 is a '1'
which means the button is not pressed. If the
button is not pressed, it jumps to that label
(NOT_PRESSED) and calls the routine to make a
'1' on the display. If the button is pressed,
it will not jump but instead will go to the next
line which calls the routine to make a '0' on
the display. So the display will show the value
Note: Another command you may want to use is
JNB (Jump if Not Bit set) which is the opposite
The program is switch.asm.
Compile the program using
TASM and load the hex file into the 2051.
Put the 2051 into the circuit and connect the
power. The display should say '1' until you
press the button and then should say '0' while
the button is being pressed.