piclist 1998\11\11\172956a >
Thread: 4x4 keypad routine
www.piclist.com/techref/index.htm?key=4x4+keypad+routine
picon face BY : Peter L. Peres email (remove spam text)



For keys shorting to bus bars through diodes, and pull-ups on inputs:

First, assume nothing was pressed:

; In startup sequence
out 0b0000
if in != 0b1111 then scream & die (keyboard/IO pins defunct or prog. error)
;; startup done

; Poll keyboard
out 0b0000
if in == 1111 then exit: OK, no key pressed
P = 0b0001
COUNT = 4
COUNTED1 = 0
do
       out P
       shl P
       keys[COUNT-1] = in
       COUNTED1 += count_ones( keys[COUNT-1] )
       if COUNTED1 > 1 then
               exit: error, more than 1 key pressed
       if COUNTED1 == 1 then
               HERE = COUNT-1
while --COUNT
;; now id the key in keys[HERE]
KEY = id_key( keys[HERE] )
exit: OK, == KEY
;; poll keyboard done

;; count_ones: count how many bits set in a nibble. imho, one half of an
;; algorythm known here as Payson's algorythm will do that ;)

There are many improvements to this scheme, one of them being the one that
stores nibbles in two bytes, instead of four, one which uses two passes
to avoid using too many variables (the 1st pass checks, the 2nd registers
the first set key) and there is a debouncing problem that must be
solved outside this loop. There may be more.

Peter

<Pine.LNX.3.95.981111234208.2146D-100000@plp4.plp.home.org>

In reply to: <36495DD1.29E9E2E0@xtra.co.nz>
See also: www.piclist.com/techref/index.htm?key=4x4+keypad+routine
Reply You must be a member of the piclist mailing list (not only a www.piclist.com member) to post to the piclist. This form requires JavaScript and a browser/email client that can handle form mailto: posts.
Subject (change) 4x4 keypad routine

month overview.

new search...