20X2 - 4x4 keypad scanner

hippy

Technical Support
Staff member
A fast and simple 4x4 matrix keypad scanner for the 20X2, zero additional components; no diodes, no resistors required.

Returns zero when no key pressed, 1 to 16 when a key pressed. It also returns 17 to 60 when multiple keys pressed which can either be ignored or used depending on application. That also allows additional dual-pole buttons to be used to take a 16 button keypad up to a 40 button keypad - Would probably need better debouncing / press stabilised detection in those cases.
 

Attachments

ben90

New Member
Hi,

I would like to use this if possible in a burgular alarm system to read the input of the keypad but I'm not sure how to go about it? I have experience of programming (PHP, HTML, CSS, JavaScript etc.) but not the language that PICAXE uses- can I just use the ReadKeyPress function on it's own and get a return value from it of what keys were pressed? I'm using a 40X2 PICAXE chip- I tried simulating it in the picaxe editor as a 40X2 chip but got an error- 'Unknown symbol M32'- I tried changing SetFreq to M16 instead and this got rid of the error but I don't know whether it will cause a problem or not?

Thanks for any help,

Ben
 

hippy

Technical Support
Staff member
Just calling "ReadKeyPress" will return a scan code of any key pressed. That can be used in itself but it is easier to use the TABLE definitions and subsequent "ReadTable keyPress, keyPress" after calling "ReadKeyPress" to convert that scan code into a more usable number.

The "ReadKeyPress" routine can be called at any time, with any frequency and the program can be run at any speed though faster will make it more responsive. Using M16 to with a 40X2 will work.

For use with a 28X2 or 40X2 chip the "PULLUP %11011000" command will need to be altered to "PULLUP %11000011".
 
Last edited:

kewakl

Senior Member
Watchout using PULLUP in the simulator.
PULLUP still successfully KILLS the simulator!
Using v5.2.9
 

GeoffB

New Member
Hippy. I appreciate that this thread is very old. To use this code with a 4 row by 3 column keyboard I assume that I would just omit the connection to B6, then just rem out any references to the 4th column i.e. DIR_4 & MSK_4 it this correct.

Thanks Geoff
 

hippy

Technical Support
Staff member
I believe it's the connection to B.2 that would be best to drop but it doesn't really matter as each key will still give a unique number for it being pushed. You can wire your 4 row x 3 column as 3 row x 4 column and the software won't care.
 

jay4708

New Member
Hello,

Thanks for the code I'm learning a lot from it, understanding line after line.

One thing that stumped me was the 'pullup', it took me a while to realise it wasn't correct for the 20X2, instead of it being PullUp %11011000 as in the download it should actually be PullUp %11000011, is this a curve ball you left in there for us noobs :)

Again thanks for the code.

John
 

hippy

Technical Support
Staff member
The PULLUP value in the code is correct; it's just that on the 20X2 the bits do not map directly and one-to-one to the B.x pins. From PICAXE Manual 2 - Basic Commands ...

20X2 bit0-bit7 = C.0, C.6, C.7, B.0, B.1 B.5, B.6, B.7

Code:
;       BBBBBCCC
;       76510760
;       11-11---   1 = Pull-up on

PullUp %11011000
I am not sure why I did not include the above as comment in the source code.

Added : I guess this didn't help understanding either ...

For use with a 20X2 or 40X2 chip the "PULLUP %11011000" command will need to be altered to "PULLUP %11000011".
That should have been 28X2 or 40X2. Sorry about that. I have updated the earlier post.

If you do have any questions please ask, and I will do my best to answer if I can.
 
Last edited:

jay4708

New Member
Hi Hippy,

Thanks for the update.

Can I ask was there a reason you used MSK_1, MSK_2 ... etc when they all just equal MSK_X, am I missing something?

Thanks
John
 

hippy

Technical Support
Staff member
I think that was so I could use _1, _2 etc consistently in the button detection code sections. Each block should use all _1's or all _2's as it makes it easier to see each block is written correctly.
 

giokal

New Member
Hi hippy,I've been testing the original code posted in the thread # 1 adapting it to 40x2 as recommended in the following threads, as I show below ..
Code:
#Picaxe 40X2
#Terminal 19200	
#No_Data

SetFreq m16 

PULLUP %11000011 ;pullup b7b6....b1b0   manual 2 p159 v7.7 

lastKeyPress = $FF

Do
Gosub ReadKeyPress
ReadTable keyPress, keyPress
If keyPress <> lastKeyPress Then
lastKeyPress = keyPress
LOOKUP keyPress,("0A321B654C987D#0*"),KEYPRESS ;
SerTxd ( "Key = ", keyPress, CR, LF ) 
Pause 40
End If
as you will see add LOOKUP to make recognizable code an ASCII mode but in the execution of the program me strange, 1 things happen) I send "0" to energize the circuit, 2) each time you press a key the ascii value is sent but below "0". As I remove these two situations?
this is what shows the terminal:
keypad 4x4.JPG
I tried pressing two keys hang time and just read me when I press horizontally ie: A + 3, A + 2, A + 1, B + 6, B + 5, etc. and does not read vertically or diagonally, ie: A + B or A + 8, etc.
 

hippy

Technical Support
Staff member
as you will see add LOOKUP to make recognizable code an ASCII mode but in the execution of the program me strange, 1 things happen) I send "0" to energize the circuit, 2) each time you press a key the ascii value is sent but below "0". As I remove these two situations?
The 'ReadKeyPress' routine returns a zero value in 'keyPress' to indicate no buttons pressed, so there will always be a zero after any other key indicator as that key is released.
 
Top