I have a problem with my keypad setup.
I made the following circuit so that the microcontroller can sniff out a unique value based on what key is pressed.
I ordered the keypad from futurlec and the datasheet for it is here: https://futurlec.com/Keypad3x4.shtml
I have tested the keypad by itself and verified it works. I also tested the PCB I assembled my circuit on and there are no short circuits and no cracks in the traces.
I made this circuit instead of using fancy chips because I wanted to use parts that are commonly available in Canada. Here I used the 74HC138 to select a column then the 74HC151 to help decode the signal. Then I tied the inputs of both chips to a 74HC393 binary counter so that every number can get scanned. The microcontroller itself uses a 22Mhz clock (not shown here). I also tied a 10K resistor to 74HC151 output so that I don't blow anything up if I accidentally set the wrong pin to logic low while the chip outputs a high.
This is the best I came up with so far given that I actually only have three I/O pins on the microcontroller left to use along with the reset signal.
Is there any way I can improve this circuit or even do a better circuit that can help a microcontroller determine what button is pressed on a matrix keypad?
I also added my source code for the 8051 microcontroller.
I made the following circuit so that the microcontroller can sniff out a unique value based on what key is pressed.
I ordered the keypad from futurlec and the datasheet for it is here: https://futurlec.com/Keypad3x4.shtml
I have tested the keypad by itself and verified it works. I also tested the PCB I assembled my circuit on and there are no short circuits and no cracks in the traces.
I made this circuit instead of using fancy chips because I wanted to use parts that are commonly available in Canada. Here I used the 74HC138 to select a column then the 74HC151 to help decode the signal. Then I tied the inputs of both chips to a 74HC393 binary counter so that every number can get scanned. The microcontroller itself uses a 22Mhz clock (not shown here). I also tied a 10K resistor to 74HC151 output so that I don't blow anything up if I accidentally set the wrong pin to logic low while the chip outputs a high.
This is the best I came up with so far given that I actually only have three I/O pins on the microcontroller left to use along with the reset signal.
Is there any way I can improve this circuit or even do a better circuit that can help a microcontroller determine what button is pressed on a matrix keypad?
I also added my source code for the 8051 microcontroller.
Code:
;individual keys on the keypad:
_K1 equ 0h
_K2 equ 1h
_K3 equ 2h
_K4 equ 4h
_K5 equ 5h
_K6 equ 6h
_K7 equ 8h
_K8 equ 9h
_K9 equ 0Ah
_Kstar equ 0Ch ;star
_K0 equ 0Dh
_Knum equ 0Eh ;pound
;other defines
KPADINC equ P1.5 ;keypad increment
KPADVAL equ P1.6 ;keypad get val. 1=set
KPADRES equ P1.7 ;reset keypad. 1=reset
KPADNUM equ 64h ;Possible number we want
KPAD bit 0h ;Bit indicating button was pressed
;Key press
kpress:
;See if a button is down
jnb KPADVAL,nmatrix
;button is down.
;see if it was pressed before
jb KPAD,mxnokeyyet
;it wasn't pressed before
;wait till button is released
jb KPADVAL,$
;Button released so get value
mov A,KPADNUM
anl A,#0Fh
;And set flag and exit
setb KPAD
ret
mxnokeyyet:
nmatrix:
;button isn't pressed
;so pulse the clock and increment number hardware is at
setb KPADINC
inc KPADNUM
clr KPADINC
;and exit
ret
mkeys:
acall kpress ;Try to get key
jnb KPAD,mkeys ;and keep trying till we get one
lcall ktoval ;then convert value to actual key we pressed
clr KPAD ;clear flag so new value can be received
ret
;keypad to raw value A=keypad value. Output: 0Ah=* 0Bh=#, 0-9=num
ktoval:
anl A,#0Fh ;input must be from 0-15
jnz knoz
inc A ;0 converts to 1
ret
knoz:
movc A,@A+PC ;input wasn't 0, so use table to get value
ret
db 02h,03h,0FFh,04h,05h,06h,0FFh,07h,08h,09h,0FFh,0Ah,00h,0Bh,0FFh,0FFh,0FFh