Searching \ for '4x4 keypad routine' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/index.htm?key=4x4+keypad+routine
Search entire site for: '4x4 keypad routine'.

Truncated match.
PICList Thread
'4x4 keypad routine'
1998\11\11@161839 by aureo

flavicon
face
Thanks a lot to all those people who answer my questions.

I am at the planing stage of writing a program to read in a value from a
4x4 matrix scan keypad.

I have got the basics of it (I think, open to suggestions):

begin:
       write a 4 bit known value to the column -> 1000
       read the rows
       IF rows value > 1 THEN GOTO key_pressed ELSE
       shift 4 bit known value (shift right)-> 0100
loop

cal_key_pressed:
       calculate from [value written to columns] and [value read from rows] a
unique          value for that key pressed

My problem is with cal_key_pressed, the only way I can think of is to
use the known value as the 4 MSBs and the read value as the 4 LSBs.

i.e.    write to column         1000
       read from rows          0100
       result                  10000100

-Is this an efficient way to do it?
-Would the next step be to use a lookup table to obtain a more relevant
value for than key pressed, i.e. 00000001 = 1 etc.
-How would I deal with two keys pressed at the same time (seems
impossible)?

--
Seth Fischer
Auckland, New Zealand

1998\11\11@172956 by Peter L. Peres

picon face
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

1998\11\12@063856 by Caisson

flavicon
face
> Van: Seth Fischer <spam_OUTaureoTakeThisOuTspamxtra.co.nz>
> Aan: .....PICLISTKILLspamspam@spam@MITVMA.MIT.EDU
> Onderwerp: 4x4 keypad routine
> Datum: woensdag 11 november 1998 10:50

Hello Seth,

> Thanks a lot to all those people who answer my questions.
>
> I am at the planing stage of writing a program to read in a value from a
> 4x4 matrix scan keypad.
>
> I have got the basics of it (I think, open to suggestions):

<Snip>

> i.e.    write to column         1000
>         read from rows          0100
>         result                  10000100
>
> -Is this an efficient way to do it?

I think you will have to invert all bit's, because not connected inputs
will result in reading a One.  So, it will be
 Write to column 0111
 Read from Rows 1011
 Result    01111011

Without any external hardware ?  Yes, it is.

> -Would the next step be to use a lookup table to obtain a more relevant
> value for than key pressed, i.e. 00000001 = 1 etc.

Both nibbles should have only _one_ bit clear.  Convert the bit-position to
a number (0 to 3) and combine both nibbles to a single 4 bit number.  the
number than tell's you the key-index.  after this the look-up table is only
16 bytes (one entry for every Key-index).

> -How would I deal with two keys pressed at the same time (seems
> impossible)?

No, it's not (but hey, I still think that cooking a good meal is impossible
to me too :-).
I take it that you want to _ignore_ multiple key's down.
Write all Zero's.  The result should be a single bit clear as a result.
switch the Output to Input and vice-verse.  Do the above again (write all
Zero's).  The result should again be a single bit clear.  If not, there are
multiple/no keys pressed.

Oh, by the way : If you want to do the above the Outputs should be
Open-collector types (Real or Emulated), and any column/row should be
pulled-up by a resistor.

Greetz,
 Rudy Wieser

1998\11\13@223807 by Javier

flavicon
face
How can I emulate the open collector  in the keypad ??
I don4t fully understand, why it4s better to send 04s and not 14s
Bye
Javier
-----Mensaje original-----
De: Caisson <caissonspamKILLspamTELEBYTE.NL>
Para: .....PICLISTKILLspamspam.....MITVMA.MIT.EDU <EraseMEPICLISTspam_OUTspamTakeThisOuTMITVMA.MIT.EDU>
Fecha: Jueves 12 de Noviembre de 1998 08:37 a.m.
Asunto: Re: 4x4 keypad routine


{Quote hidden}

1998\11\13@234125 by Regulus Berdin

flavicon
face
Javier wrote:
>
> How can I emulate the open collector  in the keypad ??
> I don4t fully understand, why it4s better to send 04s and not 14s

This technique is used only if you will not put any external component
(except for switches) on your keypad.  To make the software simple,
diodes and some pullup/down resistors are needed.

To do without external components, the port (PORTB) must be programmed
to enable it's weak pull-ups.  That is why sending 0 is more economical
because no resistors are needed.

The port output registers must also be put to low and only work on the
TRISB registers when pulling low the rows on each pass.

setup:
       bcf     STATUS,RP0              ;bank0
       clrf    PORTB                   ;set output registers low
       bsf     STATUS,RP0              ;bank1
       movlw   0xFF                    ;tristate
       movwf   TRISB                   ;
       bcf     OPTION,RBPU             ;enable weak pull-ups
       ...
       ...

       (in keyread routine)
       ...
       movlw   B'11111110'             ;scan row
       ...
       movlw   B'11111101'
       ...
       movlw   B'11111011'
       ...
       movlw   B'11110111'
       ...
       bsf     STATUS,RP0              ;bank1
       movwf   TRISB                   ;enable particular row
       andlw   0x0F                    ;mask row
       movwf   key                     ;save in key
       bcf     STATUS,RP0              ;bank0
       movf    PORTB,w                 ;read port
       andlw   0xF0                    ;mask column
       iorwf   key                     ;combine with row


regards,
Reggie

1998\11\16@064224 by Caisson

flavicon
face
> Van: Javier <KILLspamgrijalbaKILLspamspamCVTCI.COM.AR>
> Aan: RemoveMEPICLISTTakeThisOuTspamMITVMA.MIT.EDU
> Onderwerp: Re: 4x4 keypad routine
> Datum: vrijdag 13 november 1998 5:26

Hello Javier,

> How can I emulate the open collector  in the keypad ??

An Open-collector is emulated by switching the Micro-controller's I/O-pin
to Output and pull it Low at the same time, but switch the pin to Input
otherwise.  This way any other output-pin can pull it Low, without
colliding with the current I/O-pin who would be trying to pull the pin
High.

> I don't fully understand, why it's better to send 0's and not 1's

Inputs that wich are not connected (to anything) read High. If you send a
One to a button that is pressed, the connected Input will read a One.  But
: so will all other _unconnected_ inputs !  The only way to get a
difference in a pressed button and a not-pressed button is to pull all
not-connected input-lines Low (not preferrable : read below) or to send a
Zero to the button (preferrable).

The Outputs of most IC's are better in pulling something Low than High.
The Inputs of most IC's are better in detecting a High than a Low.  The
resistor for pulling an input Low would be of a smaller value than a
resistor (that helps) to pull an input High.

Another reason could be that there are already (so-called) weak pull-ups
(programmable !) present on port B of an 16C84 (wich I'm using). That would
mean that you would not need any _external_ pull-up resistors ...

Greetz,
 Rudy Wieser

More... (looser matching)
- Last day of these posts
- In 1998 , 1999 only
- Today
- New search...