Keypad Interfacing with 8051 Microcontroller

9,502 views 16 slides Oct 08, 2020
Slide 1
Slide 1 of 16
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16

About This Presentation

Keypad is a common interface with any microcontroller. This presentation gives details of keypad can be interfaced with 8051. The key pressed may be dispalyed on LCD/7 segment/LED displays.


Slide Content

Keypad Interfacing
with 8051
Sudhanshu Janwadkar
S. V. National Institute of Technology
Lecture Notes: 9-10
th
April 2018

Points to discuss
•Whatisamatrixkeypad?
•Schematicof4X4MatrixKeypad
•PrincipleofOperation–i.eHowdoweinterface?

Introduction
•A4X4matrixkeypadiscalledsobecause,
itisorganizedinmatrixstructure
Ithas4X4=16switches(i.e.pushbuttons)
•Itcanbeusedtogivemultipleinputs
•Eachinputcanhavesomesignificance.
Ifweusetwoportsofmicrocontroller,wecan
connect8X8keypad.Thiscanbeusedtogive64
inputs,whichwouldhavebeenonly16,ifwe
connectswitchesdirectly.
•Amatrixkeypadis
Easytointerface
Easytoprocure

Introduction
•Keyboards are organized in a matrix of rows and columns
•The CPU accesses both rows and columns through ports
•When a key is pressed, a row and a column make a contact
•Otherwise, there is no connection between rows and
columns

Schematic of a 4 X 4 Keypad
•Thereareatotalof16switchesarrangedin4rowsand4
columns
•Eachrowandcolumnhasaswitch,inbetween,strategically
placedsuchthat
Eachswitchhasthecapability,ifpressed,toshortthe
particularrowandcolumn(andformapath)
•Theotherpathswouldremainopen

Schematic of a 4 X 4 Keypad
•Eachswitchcanactuallygenerateauniquesituationofrowsand
columns.
•Forexample,saySW1ispressed.Thiswouldformapathbetween
Row1andColumn1.Nootherrowandcolumnwouldhaveapath.

Principle of operation
•ConnectallrowstoV
DDthroughpull-upresistors.Thismeansanytimeyou
readthelogiclevelacrossrows,itwouldbe‘1111’.
•ConnectColumn1isatLogic0(Ground),column2toV
DD,column3toV
DD
andcolumn4toV
DD
•IftheuserpressesswitchSW1,onlyrow1willbeconnectedtoColumn1and
itwouldbegrounded.Theotherthreerowswouldbeat5V.Therow
informationwouldberead0111.
•Insteadiftheuserpresses,SW9,onlyRow3wouldbegroundedand
remainingrowsbeatV
DD;rowinformationwouldbereadas1101.
•Ifyouhaveinformationaboutstatusofalltherowsandcolumn,youhave
enoughinformationtodeduceorcalculate,whichswitchhasbeenpressed.

Principle of operation
•Notethat,Therowsandcolumnsarejustaconventionandareabsolutely
interchangeable.Thiswouldnecessarilymeanthatrowscanbehaveas
columnsandcolumnscanbehaveasrows.
•Groundingcolumn1,connectingtheotherthreecolumnstoV
DDandthen
readingrowinformationwouldhelpyoudetectifanyoftheswitchesin
Column1hasbeenpressed.
•Extendingthelogic,todetectaswitchpressinColumn2,youwouldhaveto
groundColumn2andconnecttheothercolumnstoVDD.Readingtherow
informationwouldhelpyoudetectifSW2,SW6,SW10orSW14waspressed
andsoon
•Letsbuildthegeneralalgorithmbasedonthisinformation.

•Connectthe4X4matrixkeypadtothe8pinsofthemicrocontrolleror
arduinoorFPGA
•Assaidearlier,sincetherowsandcolumnsareinterchangeable,you
mayvarythepatternyouapplyoncolumnsandreadtherow
information.Oryoumayvarytherowinformationandreadthe
columns.
Principle of operation

•Ifnoswitchispressed,therowswouldread1111.
•Applythepattern0111tothecolumnsandreadalltherows.
•Thepattern0111onrowsindicatesSW1ispressed.Pattern1011
indicatesSW4ispressed.Pattern1101indicatesSw7ispressed.Pattern
1110indicatesSw*ispressed
•Nextapplythepattern1011tothecolumnsandreadallrows,todetectifa
switchhasbeenpressedincolumn2.
•Similarly,applythepattern1101tothecolumnsandreadallrows,todetect
ifaswitchhasbeenpressedincolumn3
•Similarly,applythepattern1110tothecolumnsandreadallrows,todetect
ifaswitchhasbeenpressedincolumn4
•Repeattheabovestepsataveryfastrate,sothatnoneofthekeypressgoes
undetected.

Summarizing,
•Itisthefunctionofthemicrocontrollertoscanthe
keyboardcontinuouslytodetectandidentifythekey
pressed
•Todetectapressedkey,themicrocontrollergroundsall
columns,successively,byproviding0andthenitreads
therows
•Ifthedatareadfromrowsis1111,nokeyhasbeen
pressedandtheprocesscontinuestillkeypressis
detected
•Ifoneoftherowbitshasazero,thismeansthatakey
presshasoccurred
•Afterdetectingakeypress,microcontrollerwillgo
throughtheprocessofidentifyingthekey
Principle of operation

Summarizing,
•Starting with the Column 1, the microcontroller grounds
it by providing a low to Column C0 only
It reads all the rows.
If the data read is all 1s, no key in that row is
activated and the process is moved to the next
column
•It grounds the next column, reads all the rows and
checks for any zero
•This process continues until the column in which key is
pressed is identified
After identification of the column in which the key
has been pressed, Find out which row the pressed
key belongs to
Principle of operation

ORG 00H
MOV DPTR, #LUT // The 7-segment codes of switch press detected are stored in Code memory
MOV P0, #00000000B // initializes P0 as output port; 7-segment is connected to Port 0
;------------CONNECTIONS------------------
;P1.0 = Col 0, P1.1 = Col 1, P1.2 = Col 2, P1.3 = Col 3,
;P1.4 = Row 0, P1.5 =Row 1 P1.6 = Row 2, P1.7 = Row 3,
BACK:
CLR P1.0 // makes Column 0 low,; col1, col2 and col3 = 1
JB P1.4,NEXT1 // checks whether Row 0 is low and jumps to NEXT1 if not low
MOV A,#1D // Row0 =0 when Col 0 =0,indicates that SW1 has been pressed. Display 1
ACALL DISPLAY // calls DISPLAY subroutine
NEXT1:JB P1.5,NEXT2 // checks whether Row 1 is low. Row 1 =0 indicates SW2 has been pressed.
MOV A,#2D // Display 2
ACALL DISPLAY
NEXT2:JB P1.6,NEXT3// Check whether Row2 is low
MOV A,#3D //Display 3
ACALL DISPLAY
NEXT3:JB P1.7,NEXT4//Check if Row 3 is low
MOV A,#10 D //Display A
ACALL DISPLAY
; This completed one set of Row checking
With Col0 =0. Next make Col0 =1 and col1=0,
Col2 =1 and col3=1, as earlier
1 4 7 D
2 5 8 E
3 6 9 F
A B C 0
P1.0 P1.1 P1.2 P1.3
P1.4
P1.5
P1.6
P1.7
Each keypad would have same internal connections, But the numbers displayed on top
might vary. This code will work only for the keypad display shown

; column information = 1011, read rows
NEXT4:SETB P1.0
CLR P1.1
JB P1.4,NEXT5
MOV A,#4D
ACALL DISPLAY
NEXT5:JB P1.5,NEXT6
MOV A,#5D
ACALL DISPLAY
NEXT6:JB P1.6,NEXT7
MOV A,#6D
ACALL DISPLAY
NEXT7:JB P1.7,NEXT8
MOV A,#11D
ACALL DISPLAY
; column information = 1101, read rows
NEXT8:SETB P1.1
CLR P1.2
JB P1.4,NEXT9
MOV A,#7D
ACALL DISPLAY
NEXT9:JB P1.5,NEXT10
MOV A,#8D
ACALL DISPLAY
NEXT10:JB P1.6,NEXT11
MOV A,#9D
ACALL DISPLAY
NEXT11:JB P1.7,NEXT12
MOV A,#12D
ACALL DISPLAY
1 4 7 D
2 5 8 E
3 6 9 F
A B C 0
P1.0 P1.1 P1.2 P1.3
P1.4
P1.5
P1.6
P1.7

; column information = 1110, read rows
NEXT12:SETB P1.2
CLR P1.3
JB P1.4,NEXT13
MOV A,#13D
ACALL DISPLAY
NEXT13:JB P1.5,NEXT14
MOV A,#14D
ACALL DISPLAY
NEXT14:JB P1.6,NEXT15
MOV A,#15D
ACALL DISPLAY
NEXT15:JB P1.7,BACK
MOV A,#0D
ACALL DISPLAY
LJMP BACK
;DPTR points to first location of LUT. Accumulator contains the offset value to be added. Fetch the
byte containing the Seven segment code from LUT (in Code memory) and display
DISPLAY:
MOVC A,@A+DPTR //
MOV P0,A // puts corresponding digit drive pattern into P0
RET

LUT:
// Look up table starts here
DB 11111100B //0… abcdefgh
DB 01100000B //1
DB 11011010B //2
DB 11110010B //3
DB 01100110B //4
DB 10110110B
DB 10111110B
DB 11100000B
DB 11111110B
DB 11110110B
DB 11101110B
DB 00111110B
DB 10011110B
DB 01111010B
DB 10011110B
DB 10011110B //F
END