PDA

View Full Version : 4x4 Switch matrix keypad


John Mc
07-22-04, 01:46 AM
I am thinking about how to read a 4x4 keypad. I can think of a few possibilities, but I wonder what the best direction to take is. I am fairly new to FORTH and ISOMAX. I appreciate any suggestions. The keypad has 4 rows and 4 columns of switches the rows are driven from output pins and the columns are inputs, you step thru the rows activating each row in sequence and read each column during each row activation. Every switch (16 switches) is exactly one row-column connection. Has anyone done this ?

Thanks ! ...John
:)

RMDumse
07-22-04, 10:11 AM
This is an example I wrote in haste, and now looking back on it, it probably isn't a very good code to use as an example. But. It does work. Maybe it would save you some time, if you just want to copy it, and replace the final X test word with calls in your program. So I hope it's "better than nothing".

SCRUB



HEX
( ROWS COLS

( COLS PULLED HI ON PA0,1,2,3
( ROWS ON PA4,5,6,7

( get address of context-last variable
GPIO CONTEXT @ CONSTANT context-last EEWORD

NO-CONTEXT

: IS-CONTEXT ( nfa -- )
context-last ! context-last CONTEXT !
; EEWORD

: INDEXED-OBJLIT ( i -- )
R> SWAP OVER P@ + P@ OBJREF ! CELL+ >R
; EEWORD

: INDEXED-OBJLITERAL ( a -- compiling ) ( i a -- executing )
STATE @
IF
COMPILE INDEXED-OBJLIT P, ( compiling: OBJLIT,a
ELSE
+ P@ OBJREF ! ( executing: just store it
THEN
; IMMEDIATE EEWORD

: INDEXED-OBJECT
<BUILDS
IMMEDIATE ( make an IMMEDIATE word
context-last @ P, ( and save its private dictionary head
( additional ROM or RAM to be allocated by user )
DOES>
DUP P@ IS-CONTEXT ( set the private context
CELL+ [COMPILE] INDEXED-OBJLITERAL
( set object table pointer
; EEWORD

: }} ; EEWORD

: {{
BEGIN
' CFA DUP [ ' }} CFA ] LITERAL -
WHILE
3 + P,
REPEAT DROP
; EEWORD

( testing example


GPIO INDEXED-OBJECT COLx
{{ PA0 PA1 PA2 PA3 }} EEWORD

GPIO INDEXED-OBJECT ROWx
{{ PA4 PA5 PA6 PA7 }} EEWORD


CREATE KEYTBL
1 P, 2 P, 3 P, A P,
4 P, 5 P, 6 P, B P,
7 P, 8 P, 9 P, C P,
2A P, 0 P, 23 P, D P,
EEWORD

VARIABLE LSTKEY EEWORD
VARIABLE NEWKEY EEWORD
VARIABLE KEYLOC EEWORD
VARIABLE COL EEWORD
VARIABLE ROW EEWORD

PA0
: CHKKEY
DUP COL ! COLx OFF?
IF
ROW @ 4 * COL @ + DUP KEYLOC ! KEYTBL + P@ NEWKEY !
THEN
; EEWORD

: KEYSCAN

NEWKEY @ LSTKEY !
TRUE NEWKEY !

PA0 ON? DROP
PA1 ON? DROP
PA2 ON? DROP
PA3 ON? DROP

PA4 ON
PA5 ON
PA6 ON
PA7 ON

0 COL !
0 ROW !


PA4 OFF
0 ROW !

0 CHKKEY
1 CHKKEY
2 CHKKEY
3 CHKKEY

PA4 ON
PA5 OFF
1 ROW !

0 CHKKEY
1 CHKKEY
2 CHKKEY
3 CHKKEY

PA5 ON
PA6 OFF
2 ROW !

0 CHKKEY
1 CHKKEY
2 CHKKEY
3 CHKKEY


PA6 ON
PA7 OFF
3 ROW !

0 CHKKEY
1 CHKKEY
2 CHKKEY
3 CHKKEY


PA7 ON
; EEWORD

: X
BEGIN
KEYSCAN
NEWKEY @ LSTKEY @ = NOT
IF
CR KEYLOC ? NEWKEY ?
THEN
?TERMINAL
UNTIL
; EEWORD

John Mc
07-22-04, 06:56 PM
Thanks Randy for the great info ! I must admit I am not quite up to speed on the OO stuff here, the lower half makes good sense, but I am going to have to study the upper part for a while in order to really understand it.