Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

5.2.

KEYBOARD INTERFACING

Keyboard and LCDs are most widely used input/output devices of the 8051.
The CPU accesses
Keyboards are organized in a matrix of rows and columns.
both rows and columns through ports. Therefore with two 8-bit ports,
an 8 x 8
matrix ofkeys can be connected to the microprocessor.
When a key is pressed, a row and a column make a contact; otherwise there is

no connection between rows and columns.

Scanning and identifying the key

The following figure 5.2 shows the 4 x 4 matrix connected to two ports. The
rows are connected to an output port and the columns are connected to an input

port. If no key has been pressed, reading the input port will yield 1s for all

columns, since they are all connected to high (Vcc).


Microcontrolle
5.6 Microprocessorsand Microcontrollers- E
Flowchart
If all rows are grounded and a key
the Vcc
is pressed, one of thecolumns will have 0 4.7k

since the key pressed provides the path to

ground. It is the function of the w


w
4,7k

w
Do
to scan the keyboard D
microcontroller
continuously to detect and identify the key
pressed.

Grounding rows and readingthe


D2
D
Port
(Out)
1
w
columns Port 2
Dg D2 D, Do
To detect a pressed key, the (In)

microcontroller grounds all rows by


Fig. 5.2. Matrix Keyboard
providing 0 to the output latch and then it

Connection to Ports
reads the columns. If the data read from

the columns is D3- DO = 1111,no key has been pressed and the process continues
a key press is detected. However, if one of the has a zero,
until
means a key press has occurred. For example,
that
column bits
if D3 -D0=
1101, this
this
means
that a key in the DI column has been pressed. After a key press is
detected,the
microcontrollerwill go through the process of identifying the key. Starting with the
top row, the microcontrollergrounds it by providing a low to row D0 only; then it
reads the columns. Ifthe data read is all 1s, no key in that row is activated and the

process is moved to the next row. It grounds the next row, reads the columns, and
checks for any zero.This process continues until the row is identified.

Example 5.1|From the above figure 5.2, identify the row and column of the

pressed key for each ofthefollowing

(a) D3-DO
= 1110for the row, D3 -DO =1011for the column
(b) D3-D0 1101 forthe row, D3- DO 0111 for the column
Solution
From the above figure 5.2 the row and column can be used to identify the key.

(a) The row belongs to DO and the column belongs to D2; therefore,

key number 2was pressed.


(b) The row belongs to D1 and the column belongs to D3; therefore,
key number 7was pressed.
Pr
woller Programming & Applications
5.7
RrOcOmiroller

TOWcNart
Start

Ground all rows


Ground nextrow

Read all
columns
Read all
columns

No Key
press in
All
No thisrow?
keys
Open?
Yes
Yes
Find which key
is pressed

Read allcolumns
scan
Get code
from table

No Any
key down? Return

Yes

Wait for debounce

Read all columns

No
All
keys
pen?
Yes
5.8
Microprocessorsand Microcontrollers-EER

Keyboard subroutlne.This program sends the ASCII code

for pressed key to PO.1

P1.0-P1.3 connected to rows P2.0-P2. connected to columns

MOV P2, #OFFH ;make P2 an input


port

KI MOV PI, #0 ground all rows atonce

MOV A, P2 read all col. ensure all

keys open.

ANL A, #00001111B masked unused bits

CJNE A,#00001111B,K1 check till all keys released

K2: ACALL DELAY call 20 ms delay


MOV A, P2 see if any key is pressed

ANL A,#00001111B mask unused bits

CJNE A,#00001111B,OVER ;keypressed,await closure


SJMP K2 check if key pressed
OVER: ACALL DELAY wait 20 ms debounce time

MOV A, P2 check key closure

ANL A,#00001111B bits mask unused

CJNE A,#00001111B,OVER1 ;key pressed, find row

SJMP K2 ifnone,keep polling


OVER
Cheuk
1

o
MOV
MOV
Pl,#11111110B
A, P2
ground row
;read
0 loRc o
TOw
all columns ckeck oF
ANL A,#00001111B mask unused (Or)
bits
ta othuthcINE A, #00001111B, ROw_0 ;key row 0,findthecol.
ce ko) MOV PI,#11111101B ground row 1

ohowist

od o MOV A, P2 read all columns


Microcontroller
Programming &
Applications
plications
5.9
ANL
A,#00001111B mask unused bits
CJNE A,
#00001111B, ROW_1 ;key row 1, find
MOV Pl,
the col.
#11111011B ground row 2
MOV A, P2
read all columns
ANL A,#00001111B mask unused bits
CJNE A, #000011
MOV
1IB, ROW2; key row 2,find the col.
PI,
#11110111B ground row 3
MOV A, P2
read all columns
ANL A,#00001111B mask unused bits
CJNE A,
#00001111B,ROW_3 ;keyrow 3,findthe col
LJMP F2
ifnone, false input,

repeat
ROW_0: MOV DPTR,#KCODE 0 =
set DPTR start of row 0
SJMP FIND
:find col. key belongs to
ROW 1: MOV DPTR, # KCODE 1 =start of row
set DPTR 1

SJMP FIND
find col. key belongs to
ROW_2: MCV DPTR,#KCODE2 set DPTR= start of row 2
SJMP FIND ;find coi, key belongs to
ROW_3: MCV DPTR,#KCODE 3 set DPTR =start of row 3
FIND: RRC A seeif any CY bit is low
e
chep NC
UNC MATCH ifzero,get the ASCII code
CoTuO INC DPTR
point to next col. address
MATCH: SJMP FIND keep searching
CLR A
set A =O (match is found)
MOVC A,@A+DPTR get ASCII code from table
and Microcontrollers -

5.10 Microprocessors EE

MOV PO, A ; display pressed key

LJMP K get ASCIlcode from table

LOOK-UP TABLE FOR EACH


ASCII ROW locadion
merforcbo
ORG300H assiqn lootp table
KCODE 0: DB 0,1,2,3 ; Row 0

KCODEI: DB 4','5', '6', '7 Row 1

KCODE 2: DB 8,9,A', 'B' Row 2


KCODE 3: DB C,'D', E', F ;Row 3
END

5.3. STEPPER MOTOR CONTROL

A stepper motor is a digital motor used to translate electrical pulses into

mechanical movements. The following figure 5.3 shows the typical 2 phase motor
interfaced using 8255.

Motor shown in the circuit has two phases, with center-tap winding. The center

taps of these windings are connected to the 12V supply. Due to this, motor can be
excited by grounding four terminals of the two windings. Motor can be rotated in

steps by giving proper excitation sequence of these windings. The lower nibble of
port A of the 8255 is used to generate excitation signals in the proper sequence.

The stepper motor has been explained in the interfacing of stepper motor using

8085 section. Some important terms related to the stepper motor are given below.

Rotor

Stepper motor has a Permanent Magnet Rotor. It is also known as shaft. It


is

surrounded by stator.

Step Angle

It is the minimum degree of rotation associated with a single step.


erocontrollerProgramming & Applications 5.11

+12 V

Stepper
motor

X2

Y1 Y2

PA2

Fig. 5.3. StepperMotor Interface

Steps per revolution


360°.
This is the total number of stepsneeded to complete one rotation(or)

rpmxStepsper revoltuon
Steps per second 60

The following table shows the typical excitation sequence. The given excitation

motor in clockwise dircction. To rotate Motorin anticlockwise


sequence rotates the
excite motor in a reverse sequence.
direction we have to
motor has many changes due to change in
The excitation sequence for stepper
it is not desirable to excite both the ends of the
winding connections. However,
This cancels the flux and motor winding may
same winding simultaneously.
5.12 Microprocessors and Microcontrollers EEE
wing circui
damage. To avoid this, digital locking system must be designed. Following circuit
5.4 shows the
simple digital locking system.

-x
Fig. 5.4. Digital Locking Systenm

Excitation Table

Step X Y1 Y2

1 0 1 0 1

2 0 0
3 0 1 0

4 0 1 1 0
0 1

The excitation sequence given in the above table is called a full step excitation
sequence. In which excitation ends of the phase are changed in one step. The
excitation sequence given in the following table takes two steps to change the
excitation ends of the phase. Such a sequence is called half step sequence and in

each step the motor is rotated by0.9°.

Step Y1 Y2
1 0 1

2 0 0 0 1

0 0
4 1 0 0 0
5
6 0 1 0
0 0
& Applications 5.13
Microcontroller Programming

Step X Y Y2 360
7 0 1

8 0 1
0
0 0

Half Step Excitation Sequence


the currents
Stepper motor is stepped from one position to the next by changing
the
through the fields in the motor. The winding inductance opposes changein
current and this puts limit on the stepping rate.
For higher stepping rates and more torque, it is necessary to use a higher voltage
source and current limiting resistors. By adding series resistance,
we deerease
L/R time constant, which allows the current to change more the rapidly in

windings. There is a power loss across series resistor, but designer has to
compromise between power and speed.
+12 V

Free Motor
wheeling
diode
A Winding

RB

Series
resistance

Fig. 5.5. Excitation circuit with series resistance

You might also like