Shop OBEX P1 Docs P2 Docs Learn Events
Help with 4x4 16 peypad code — Parallax Forums

Help with 4x4 16 peypad code

towerhandtowerhand Posts: 6
edited 2011-04-04 20:27 in BASIC Stamp
Looking to input a 3 or 4 digit to turn on a LED instead of a single digit.


kout VAR OUTC ' 4-bit keypad columns, state, if output
kdir VAR DIRC ' 4-bit keypad columns, input or output
kin VAR IND ' inputs from keypad rows connected to port D
' bit low for key pressed
key0 VAR Word ' state variable, first detection
key1 VAR Word ' state variable, second detection
key2 VAR Word ' state variable, third detection
keyn VAR key0.NIB0 ' address the first detection as nibble array
keys VAR Word ' debounced output, bit low for key pressed
keyz VAR Word ' state variable, previous state of keys
keyx VAR Word ' transition output, bit high one iteration for each press
ix VAR Word ' index for array addressing
DIRS=%0000111111111111 ' port D is input, all others output
OUTS=%0000000000000000 ' start with port C columns output low
' pins P0 to P7 are not used in this program
DEBUG CLS ' Clear screen
scanloop:
key2=key1 ' move the debounced states
key1=key0
FOR ix=0 TO 3 ' scan 4 cols by bringing one col at a time low.
kdir = DCD ix ' 0001,0010,0100,1000 in succession
' this makes one column at a time output low
' while the other columns are inputs
keyn(ix)=kin ' read the row inputs into nibbles of key0
NEXT
kdir = %1111 ' make all columns output (avoid floating inputs)
keys=key0&key1&key2|keys ' key bit 0->1 when all 3 are high
keys=key0|key1|key2&keys ' key bit 1->0 when all 3 are low
keyx=keys^keyz & keyz ' detect change 1->0 (active low keypress)
keyz=keys
' optional debug to visualize operation of the state machine
' slows down the operation tremendously
' debug home,bin16 key0,cr,bin16 key1,cr,bin16 key2,cr
' debug bin16 keys,32,bin16 keyx,home
' optional branch to decode keypress, note keyx is transient
IF keyx>0 THEN decode
' pause 30 ' optional delay to slow the scanning

GOTO scanloop

decode:
code VAR Byte ' code variable
code = NCD keyx-1 ' binary encode the most significant key
LOOKUP code,[10,7,4,1,0,8,5,2,11,9,6,3,12,13,14,15],code ' translate to code
FREQOUT 5, 100, 4000 'send as touchtone
LOOKUP code,["2*0#78945613ABCD"],code ' --> printable character
DEBUG code ' print on screen
IF code = "*" THEN
HIGH 0
PAUSE 100
LOW 0
ELSEIF code = "#" THEN
HIGH 2
PAUSE 100
LOW 2
ELSEIF code = > 0 THEN
HIGH 1
PAUSE 100
LOW 1
ENDIF

GOTO scanloop
Sign In or Register to comment.