Keypad
Interfacing
Done by
Akhash Kumar
Sriram Prasad MI
Kamlesh Kumar R
Keypad Interfacing
4×4 matrix keypads are
commonly used keypads in
embedded applications. Such
keypads are seen in telephones
and other commonly seen
applications.
Advantages of Matrix Keypad
The advantage of a matrix
keypad is that the use of it
will allow the programmer
to reduce the number of
pins to be used. In a 4×4
matrix keypad
● THE CPUI ACCESSES BOTH
ROWS AND COLUMNS
THROUGH PARTS
● WHEN A KEY IS PRESSED A
ROW AND COLUMN MAKE A
CONTACT. OTHERWISE THERE
IS NO CONTACT BETWEEN
THEM.
Matrix keypad
Debouncing
These are spurious spikes generated during keypress events. Normally
several spikes get generated during a key press because of the
mechanical contacts in the switch and it causes multiple spikes
Key-press Detection
FOR KEYPAD INTERFACING TWO PROCSSES SHOULD HAPPEN:
• KEY-PRESS DETECTON
• KEY-PRESS IDENTIFICATION
THERE ARE TWO WAYS BY WHICH THE PIC18 CAN PERFORM THE KEY
DETECTION.
• INTERRUPT METHOD
• SCANNING METHOD
INTERRUPT METHOD OF KEY PRESS
DETECTION
• Keypad connected to Port B
• Rows are connected to Port B high
• Columns are connected to Port B low.
Scanning
Introduction
● In this method, To detect a
key , the microcontroller
grounds all the rows by
providing zero
Attachments
[ App wireframe, Screenshots, ]
Step 1
Assume we pressed a button
Step 2
If it detects a logic 0 in
any one of the columns,
then a key press was
made in that column.
Step 3
The logic is that if a
button in that row
was pressed, then the
value written that row
will be reflected in the
corresponding column
(C2) as they are short
circuited.
Step 4
The procedure is followed
till C2 goes high when logic
high is written to a row. In
this case, a logic high to
the second row will be
reflected in the second
column.
CODE for
Interrupt
Method
D15H EQU D'100’; delay high byte of value
D15L EQU D'255’; low byte of value
COL EQU 0x08; holds the column found
DR15mH EQU 0x09; registers for 15 ma delay
DR15mL EQU 0x0A;
ORG 0x000000
Initialization
High priority Interrupt
ORG 0x000008
HI_ISR BTFSC INTCON,RBIP; Was it a PORTB change?
BRA RBIF_ISR; yes then go to ISR
RETFIE; else return
Main Function
ORG 0X00
CLRF TRISD; make PORTD output port
BCF INTCON2, RBPU; enable PORTB pull-up resistors.
MOVLW 0XF0; (Make PORTB high input ports
MOVWF TRISB; Take PORTB low output ports )
MOVWF PORTB; ground all rows
CPFSEQ PORTB; are all keys open
GOTO KEYOPEN; wait until keypad ready
KEYOPEN
0XFE =
1111 0000
MOVLW upper(KCODEO)
MOVWF TBLPTRU ; load upper byte of TBLPTR
MOVLW high(KCODEO)
MOVWF TBLPTRH ; load high byte of TBLPTR
BSF INTCON, RBIE; enable PORTB change interrupt
BSF INTCON, GIE; enable all interrupts globally
LOOP GOTO LOOP; wait for key press.
--key identification ISR
RBIF_ISR CALL DELAY; wait for debounce
MOVFF PORTB,COL ; get the column of key press
MOVLW 0XFE
MOVWF PORTB; ground row 0
CPFSEQ PORTB; Did PORTB change?
BRA ROW0; yes then row 0
MOVLW 0XFD;
MOVWF PORTB; ground row 1
CPFSEQ PORTA; Did PORTB change?
BRA ROW1; yes then row 1
0XFE =
1111 1110
0XFD =
1111 1101
MOVLW 0XFB;
MOVWF PORTB; ground row 2
CPFSEQ PORTB; Did PORTB change?
BRA ROW2; yes then row 2
MOVLW 0XF7;
MOVWF PORTB; ground row 3
CPFSEQ PORTB; Did PORTB change?
BRA ROW3; yes then row 3
GOTO BAD RBIF; no then key press too short
0XFB =
1111 1011
0XF7 =
1111 0111
ROW0 MOVLW Low (KCODEO); set TBLPTR start of row 0
BRA FIND; find the column
ROW1 MOVLW low (KCODE1); set TBLPTR= start of row I
BRA FIND; find the column
ROW2 MOVLW low (KCODE2); set TBLPTR = start of row 2
BRA FIND; find the column
ROW3 MOVLW low (KCODE3); set TBLPTR start of row 3
BRA FIND; find the column
BAD RBIF MOVLW 0x00; return null
GOTO WAIT; wait for key release
FIND MOVWF TBLPTRL; load low byte of TBLPTR
MOVLW 0XF0
XORWF COL ; invert high nibble
SWAPF COL, F; bring to low nibble
AGAIN RRCF COL; rotate to find column
BC MATCH ; column found, get the ASCII code
INCF TBLPTRL; else point to next col. address
BRA AGAIN; keep searching
0XF0 =
1111 0000
MATCH TBLRD*+; get ASCII code from table.
MOVFF TABLAT, PORTD; display pressed key on PORTD
WAIT MOVLM 0XF0;
MOVWF PORTB; reset PORTB
CPPSEQ PORTB; Did PORTB change?
BRA WAIT; yes then wait for key release.
BCF INTCON, RBIF; clear PORTB, change flag
RETFIE; return and wait for key press.
-------------delay
DELAY MOVLW D15mH; high byte of delay.
MOVWF DR15mH; store in register
D2: MOVLW D15mL; low byte of delay
MOVWF DR15mL; store in register
D1: DECF DR15mL, F; Stay until DR15ml becomes 0
BNZ D1
DECP DR15mH, F; loop until all DR15m= 0x0000
BNZ D2
RETURN
----key scan code look-up table
ORG 300H
KCODE0: DB '0','1', '2', '3'; ROW 0
KCODE1: DB '4','5', '6', '7'; ROW 1
KCODE2: DB '8','9', 'A', 'B'; ROW 2
KCODE3: DB 'C' ,'D', 'E', 'F'; ROW 3
Doubts
Thank You

Keypad interfacing

  • 1.
  • 2.
    Keypad Interfacing 4×4 matrixkeypads are commonly used keypads in embedded applications. Such keypads are seen in telephones and other commonly seen applications.
  • 3.
    Advantages of MatrixKeypad The advantage of a matrix keypad is that the use of it will allow the programmer to reduce the number of pins to be used. In a 4×4 matrix keypad
  • 4.
    ● THE CPUIACCESSES BOTH ROWS AND COLUMNS THROUGH PARTS ● WHEN A KEY IS PRESSED A ROW AND COLUMN MAKE A CONTACT. OTHERWISE THERE IS NO CONTACT BETWEEN THEM.
  • 5.
  • 7.
    Debouncing These are spuriousspikes generated during keypress events. Normally several spikes get generated during a key press because of the mechanical contacts in the switch and it causes multiple spikes
  • 8.
    Key-press Detection FOR KEYPADINTERFACING TWO PROCSSES SHOULD HAPPEN: • KEY-PRESS DETECTON • KEY-PRESS IDENTIFICATION THERE ARE TWO WAYS BY WHICH THE PIC18 CAN PERFORM THE KEY DETECTION. • INTERRUPT METHOD • SCANNING METHOD
  • 9.
    INTERRUPT METHOD OFKEY PRESS DETECTION • Keypad connected to Port B • Rows are connected to Port B high • Columns are connected to Port B low.
  • 11.
  • 12.
    Introduction ● In thismethod, To detect a key , the microcontroller grounds all the rows by providing zero
  • 13.
  • 14.
    Step 1 Assume wepressed a button
  • 16.
    Step 2 If itdetects a logic 0 in any one of the columns, then a key press was made in that column.
  • 17.
    Step 3 The logicis that if a button in that row was pressed, then the value written that row will be reflected in the corresponding column (C2) as they are short circuited.
  • 18.
    Step 4 The procedureis followed till C2 goes high when logic high is written to a row. In this case, a logic high to the second row will be reflected in the second column.
  • 19.
  • 20.
    D15H EQU D'100’;delay high byte of value D15L EQU D'255’; low byte of value COL EQU 0x08; holds the column found DR15mH EQU 0x09; registers for 15 ma delay DR15mL EQU 0x0A; ORG 0x000000 Initialization
  • 21.
    High priority Interrupt ORG0x000008 HI_ISR BTFSC INTCON,RBIP; Was it a PORTB change? BRA RBIF_ISR; yes then go to ISR RETFIE; else return
  • 22.
    Main Function ORG 0X00 CLRFTRISD; make PORTD output port BCF INTCON2, RBPU; enable PORTB pull-up resistors. MOVLW 0XF0; (Make PORTB high input ports MOVWF TRISB; Take PORTB low output ports ) MOVWF PORTB; ground all rows CPFSEQ PORTB; are all keys open GOTO KEYOPEN; wait until keypad ready KEYOPEN 0XFE = 1111 0000
  • 23.
    MOVLW upper(KCODEO) MOVWF TBLPTRU; load upper byte of TBLPTR MOVLW high(KCODEO) MOVWF TBLPTRH ; load high byte of TBLPTR BSF INTCON, RBIE; enable PORTB change interrupt BSF INTCON, GIE; enable all interrupts globally LOOP GOTO LOOP; wait for key press.
  • 24.
    --key identification ISR RBIF_ISRCALL DELAY; wait for debounce MOVFF PORTB,COL ; get the column of key press MOVLW 0XFE MOVWF PORTB; ground row 0 CPFSEQ PORTB; Did PORTB change? BRA ROW0; yes then row 0 MOVLW 0XFD; MOVWF PORTB; ground row 1 CPFSEQ PORTA; Did PORTB change? BRA ROW1; yes then row 1 0XFE = 1111 1110 0XFD = 1111 1101
  • 25.
    MOVLW 0XFB; MOVWF PORTB;ground row 2 CPFSEQ PORTB; Did PORTB change? BRA ROW2; yes then row 2 MOVLW 0XF7; MOVWF PORTB; ground row 3 CPFSEQ PORTB; Did PORTB change? BRA ROW3; yes then row 3 GOTO BAD RBIF; no then key press too short 0XFB = 1111 1011 0XF7 = 1111 0111
  • 26.
    ROW0 MOVLW Low(KCODEO); set TBLPTR start of row 0 BRA FIND; find the column ROW1 MOVLW low (KCODE1); set TBLPTR= start of row I BRA FIND; find the column ROW2 MOVLW low (KCODE2); set TBLPTR = start of row 2 BRA FIND; find the column ROW3 MOVLW low (KCODE3); set TBLPTR start of row 3 BRA FIND; find the column BAD RBIF MOVLW 0x00; return null GOTO WAIT; wait for key release
  • 27.
    FIND MOVWF TBLPTRL;load low byte of TBLPTR MOVLW 0XF0 XORWF COL ; invert high nibble SWAPF COL, F; bring to low nibble AGAIN RRCF COL; rotate to find column BC MATCH ; column found, get the ASCII code INCF TBLPTRL; else point to next col. address BRA AGAIN; keep searching 0XF0 = 1111 0000
  • 28.
    MATCH TBLRD*+; getASCII code from table. MOVFF TABLAT, PORTD; display pressed key on PORTD WAIT MOVLM 0XF0; MOVWF PORTB; reset PORTB CPPSEQ PORTB; Did PORTB change? BRA WAIT; yes then wait for key release. BCF INTCON, RBIF; clear PORTB, change flag RETFIE; return and wait for key press.
  • 29.
    -------------delay DELAY MOVLW D15mH;high byte of delay. MOVWF DR15mH; store in register D2: MOVLW D15mL; low byte of delay MOVWF DR15mL; store in register D1: DECF DR15mL, F; Stay until DR15ml becomes 0 BNZ D1 DECP DR15mH, F; loop until all DR15m= 0x0000 BNZ D2 RETURN
  • 30.
    ----key scan codelook-up table ORG 300H KCODE0: DB '0','1', '2', '3'; ROW 0 KCODE1: DB '4','5', '6', '7'; ROW 1 KCODE2: DB '8','9', 'A', 'B'; ROW 2 KCODE3: DB 'C' ,'D', 'E', 'F'; ROW 3
  • 31.
  • 33.