Shop OBEX P1 Docs P2 Docs Learn Events
Efficient Keypad Scan? — Parallax Forums

Efficient Keypad Scan?

DianeDiane Posts: 8
edited 2006-03-13 03:47 in BASIC Stamp
I have a Grayhill Series 96 keypad, which I'm using in conjunction with my BASIC stamp. All the keypad scans that I've seen tend to be quite lengthy. I keep thinking there has to be a more efficient way to code the scan, but haven't found one yet.

Does anyone have an efficient keypad scan they'd like to share?

Many thanks!

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-02-09 23:28
    Diane,

    ·· I assume you mean an efficient one using parallel I/O?· How is your keypad arranged?· How many rows/columns?· I have a few routines, although in the last few years I had switched to using parallel to serial chips.· Now with the SX coming down in price you can roll-your-own, so to speak.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • DianeDiane Posts: 8
    edited 2006-02-13 20:16
    Hi Chris,

    My keypad has four rows and four columns.· I've·attached a keypad scan I had for class, which was modelled after my professor's keypad scan.· He mentioned that there were lots of ways to wire/program keypad scans, and said that one could probably find lots on the internet.· Maybe my googling skills have declined, but I have yet to find a better on on the internet.

    Thanks again,

    Diane
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-02-13 20:28
    Diane,

    ·· That code could be further simplified by adding the ASCII variable assignment to the IF...THEN statements.· A more conventional way would be to use a LOOKUP table after the scan routine.· Here's a Nuts & Volts article that has a keyscan routine you could check out.· Otherwise I will see if I can find the one I used to use.

    http://www.parallax.com/dl/docs/prod/audiovis/Qvox306SoundOffAgain2.PDF

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-02-13 20:35
    Here's another one I came across in my search...

    http://www.engr.udayton.edu/faculty/jloomis/ece445/stamp/nv_mag/st_ap22.pdf

    And finally, Dr. Tracey Allen has some interesting information on the following page.· Lower down is the section about the keypad scan.· State machine stuff...keypad goodness!· =)

    http://www.emesystems.com/BS2fsm.htm

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-02-13 21:03
    Heres an advanced method of using a single pin on a microcontroller (a stamp would work)·to recieve a keypress. It uses a 555 timer chip to produce a pulse who's width indicates which key is pressed. The pulse widths are 110 to 1320µS in 100µS increments. While the example is 12 keys, it can be expanded for use with 16 keys by including an additional 10kΩ resistor for the extra column and replacing R3-R5 with 40kΩ resistors, the maximum pulse width using 16 key should be 1760µS. Be sure you use 0.5% tolerance resistors for keyboard array since they need to be as close as possible to the listed resistance for the keypad to operate as expected.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-02-13 21:41
    Paul,

    ·· Very interesting...Hadn't thought of that method.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • Kaos KiddKaos Kidd Posts: 614
    edited 2006-02-13 22:30
    Wow... I like that circuit Paul...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔

    Just tossing my two bits worth into the bit bucket
    KK
    ·
  • Beau SchwabeBeau Schwabe Posts: 6,559
    edited 2006-02-13 23:33
    Paul,

    I can name that circuit in one IC! smilewinkgrin.gif

    The schematic you posted is similar to one of my old tricks using binary weighted resistor values. (i.e. 1K 2.2K, 4.7K, 10K)

    webpages.charter.net/schwabelove/BasicStamp/3x5KeyPad.gif


    This circuit would use the PULSIN command, specifying a "0" or "1"for the "State" modifier would determine if you are
    looking at the ROW or COLUMN data.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • DianeDiane Posts: 8
    edited 2006-02-14 00:30
    Thanks for the ideas!

    Chris - I just started working with the Basic Stamp, and although I'm familiar with the "Lookup Table" concept, hadn't thought of applying it to the Basic Stamp!

    I haven't been issued a timer chip yet, so unfortuantely I can't try out the more advanced method.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-02-14 00:56
    Diane,

    ·· With enough examples and time to experiment you'll find the best choice for you.· Different people will often like different methods of doing the same thing.· Let us know if you need further help.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2006-02-14 01:22
    While not too well suited for a matrix scan, the following is another approach to "many buttons, one pin", using minimal components and RCTIME:
    emesystems.com/BS2rct.htm#switches
    It is an adaptation of Scott Edward's BS1 application note #18.

    Chris pointed out the following key scan routine:
    emesystems.com/BS2fsm.htm#keypad
    Note that there is one routine there that includes debouncing and another much simpler that simply scans the keypad. You have to evaluate your project to know if debouncing is going to be necessary. The debouncing routine in that example uses a kind of programming element called a "vertical counter" to debounce all of the keys in parallel, and a separate routine for decoding.

    In some applications, it can be useful to decode multiple key presses as "secret codes" or for other purposes. For example, "press "*" and "1" at the same time to enter the configuration menu". If you need that kind of capability, the key scan routine has to be designed to support it.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com

    Post Edited (Tracy Allen) : 2/14/2006 1:30:17 AM GMT
  • Beau SchwabeBeau Schwabe Posts: 6,559
    edited 2006-02-14 01:37
    Here is another spin on using the "many buttons, one pin" that uses fewer components than the previous post of mine.

    RCTIME is the stamp function you want to use with this example.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
    885 x 519 - 59K
  • Mohamed RefkyMohamed Refky Posts: 47
    edited 2006-02-15 18:42
    Look at Keypad Interface ICs from:

    http://www.elabinc.com/


    EDE 1144 - 4x4 Keypad Encoder IC

    EDE 1188 - 8x8 Keypad Encoder IC

    Mohamed Refky
  • DianeDiane Posts: 8
    edited 2006-03-08 04:54
    Here is a refinement of my previous code that I dreamed up after my professor showed me something similar.

    Although he said that my code is similar to his, he said that it's the smallest he's seen.

    Maybe I should hint the whole "many buttons, one pin" idea to him. Sometimes I wish I had only taken six credits this semester so I could focus more on this stuff, as I find it supremely fascinating.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-03-09 02:50
    Very nice Diane.· Does it seem to work well for you?· I don't see any facility for debounce, but perhaps you don't need it the way this routine works.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • DianeDiane Posts: 8
    edited 2006-03-09 03:35
    It works most excellent for me. I have resistors connecting the wires from the columns to ground, and that seems to debounce just fine.
  • ForrestForrest Posts: 1,341
    edited 2006-03-12 13:53
    Beau,
    Is there a formula you used to determine the 2.2K and 15K resistor values in your circuit? I don't have any 15K resistor in my parts box. Also, what's the value of the capacitor in your circuit?
  • Beau SchwabeBeau Schwabe Posts: 6,559
    edited 2006-03-12 15:52
    Forrest,

    The 15K resistors are not that critical, I just wanted a good separation between Columns.

    As far as the C value, this is determined the same way that you would normally choose a value
    using the RCTIME function with the BS2. (See RCTIME under Help in the BS2 program)


    I have re-posted the original keypad schematic which is simply an extension of the RCTIME
    circuit referred to in the manual and BS2 program help documentation. The revision includes
    values returned if you were to use 10K vs. 15K resistors.



    I guess, that the basic relation between the 2.2K and the 15K is as follows:

    Assume R1 = 15K's and R2 = 2.2K's


    R1 >= [noparse][[/noparse] R2 * (# of rows) ]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.

    Post Edited (Beau Schwabe (Parallax)) : 3/12/2006 4:08:21 PM GMT
    265 x 209 - 8K
  • ForrestForrest Posts: 1,341
    edited 2006-03-12 16:18
    Thanks for the update.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2006-03-12 23:53
    Hi Diane,

    Nice job on the code. I'm never one to leave these things alone, and the words "smallest he's seen" is a challenge.

    Using the same logic as your program, PBASIC has a couple of commands that can make the code even smaller. Here is a condensed inner scanning loop:

    scan: 'start of scan subroutine
    DIRA=%1111
    DO
      FOR i = 0 to 3
        OUTA = DCD i   ' set one column high, others low
        x = NCD INB     ' read and encode the inputs
        IF x THEN LOOKSCAN
      NEXT 
    LOOP
    
    Lookscan: 
      LOOKUP (x-1)*4+i, [noparse][[/noparse]1, 2, 3, "A", 4, 5, 6, "B", 7, 8, 9, "C", "*", 0 ,"#", "D"], x
    RETURN
    
    



    That gets rid of the IF-THEN logic and lets the program deal with all the port bits in parallel, rather than one at a time. The operator DCD (short for "DeCoDe") takes the value i=0,1,2,3 as input and gives back OUTA=1,2,4,8 as output. That is, the binary pattern that sets one column HIGH, %0001, %0010, %0100 or %1000.

    The operator NCD (short for "eNCoDe") lets the program deal with the whole port inB all at once. It takes an input in the range of 0 (no keys pressed) to 15 (all keys pressed at once), and gives back the number 0,1,2,3,4, which is the index of the highest bit set in the on port nibble inB. If no keys are pressed the value is zero (and the scan loop continues), but if one or more keys are pressed, (x=NCD inA) will give 1,2,3 or 4 and the program will jump to the LOOKUP. If two or more keys are pressed at once, the higher one wins. Your code gives the highest priority to the lowest numbered key, but that might not matter. If it does, then use (x = NCD (inB REV 4)), which reverses the order of priority.

    The LOOKUP (x-1)*4+i,... uses the value of both x-1 and i to compute a position in the lookup table.


    Diane said...
    Here is a refinement of my previous code that I dreamed up after my professor showed me something similar.

    Although he said that my code is similar to his, he said that it's the smallest he's seen.

    Maybe I should hint the whole "many buttons, one pin" idea to him. Sometimes I wish I had only taken six credits this semester so I could focus more on this stuff, as I find it supremely fascinating.
    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • GumbyGumby Posts: 24
    edited 2006-03-13 00:01
    Diane, Tracy et al

    Cab anyone help me translate the diagram of the RC Time by Beau into actual pin connections. I have tried all afternoon but no luck. I get the RC Time running on the BS2 with a 10K pot. I must not connect some of the resistors correctly. The 2 at the end of the row and Col.. Can anyone give me some direction...please and thank you.
    Gilles
  • DianeDiane Posts: 8
    edited 2006-03-13 00:05
    Tracy,

    Thanks for the help! I knew there was a way to further refine the code!!!
  • Beau SchwabeBeau Schwabe Posts: 6,559
    edited 2006-03-13 00:15
    Gumby,

    I'm not sure I understand what your question is. Have you been able to get the RCTIME command to work with a 10K pot?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • ForrestForrest Posts: 1,341
    edited 2006-03-13 00:24
    To troubleshoot your circuit, temporarily disconnect the Stamp pin and the capacitor in the circuit and use a multimeter where the capacitor was to verify you get the resistance reading shown in red or blue (depending if you used 15K or 10K resistors. If the resistance values don't look correct - make sure you have the columns/rows on the keypad wired correctly. You'll need the datasheet for the keypad to doublecheck the connections.
  • GumbyGumby Posts: 24
    edited 2006-03-13 00:40
    I know I'm kind of thick and dim here but bare with me,

    Yes the RC Time works fine with the pot.
    I am using a multimeter to check the R value at the 2 points of the 470 Ohm R
    On your diagram, at the top and to the right the first 10K resistor does it;
    a) connect right resistor wire to keypad pin1 and the other resistor wire (left) goes to pin 2 with the second resistor.
    or
    b)does the 1st resistor (10K) wire attach to pin 1 of the keypad but the other lead connect back to pin 8??

    If i connect the resistor 10K from pin 1 to 2 another resistor from 2 to 3 and so on to pin 4 which ties to the top of the 470 Ohm,
    and the row resistor also pin 5 to 6 6 to 7 7 to 8 with the 2.2 K and then to the 470 ohm
    that does not seem to work for me when I press a key the R value changes but not according to the values on the diagram.
    I'll probably kick myself when I finally get it but for now it's a puzzle?
    any idea?
    Thanks for your time
    Gilles
  • ForrestForrest Posts: 1,341
    edited 2006-03-13 01:30
    I believe b is correct

    Secondly, in your post your keep referring to a 470 ohm resistor - that should be a 470K ohm resistor.
  • GumbyGumby Posts: 24
    edited 2006-03-13 02:41
    Forrest,
    Thank you. Daah..., I was using a 470 instead of a 470K resistor. And a) description is the way to go. Works fine now.
    Gilles
  • Beau SchwabeBeau Schwabe Posts: 6,559
    edited 2006-03-13 03:19
    Thanks Forrest for helping Gumby out.

    Gumby,

    I'm glad you found a solution and everything is working ok now.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • ForrestForrest Posts: 1,341
    edited 2006-03-13 03:47
    Glad you got it working with connection a (which is what I meant to suggest).
Sign In or Register to comment.