> > Two keys from one keypad with standard routines can be a bit tricky > > but can be done.. If two keys on different rows AND columns are > > down you will read all four keys that make a square as down. > > It's been my experience that ambiguity only occurs when THREE keys > that form three corners of a rectangle are pressed, and that TWO keys > are never a problem to read. > > Of course, this applies only to keypads that are scanned by row or > column; were you talking about another keypad topology? There are four basic matrix-scanning techniques I've used or seen in signi- ficant use. [1] Energize all rows and read columns; then energize all columns and read rows. This technique is simple to implement in software, and can get results in only two steps. It will not work well for multiple keypresses unless all of the keys are in the same row or column, but it's quick and easy and it works. Especially on something like a PIC, this is probably the easiest way to go from a keypress to "A number". Note that the following code would probably run too fast on anything other than a 32KHz crystal and should be slowed down on faster machines. clrf PORTB movlw $0F tris PORTB movf PORTB,w bsf RP0 comf PORTB ; Really the TRIS register bcf RP0 iorwf PORTB,w Note that the first three instructions only need to be done once at system startup (though doing them more often may allow the system to recover if one of the port latches gets glitched). Interestingly, if the first three instructions are omitted from subsequent calls, the behavior of TRISB will be different on every other call but the routine will still work fine! [2] Fully-scanned matrix, no diodes This approach energizes the row wires one at a time, reading what signals come back on the column wires. It will read unambiguously any one-or-two key press, and will also return presses of more keys with the caveat that if three keys forming the corners of a "box" are pressed it will appear as though all four are pushed. If such cases are logically impossible in the application, this won't pose a problem. Note that most PC keyboards are handled this way. [3] Fully-scanned matrix, with diodes This approach uses a diode on every key to ensure that the "box" problem can't happen. Many musical keyboards use this approach since it is hard to predict what keys people might try to push simultaneously. [4] Minimalist no-extra-components matrix In cases where I/O pins are at something of a premium, it's possible to read (n*(n+1))/2 switches with (n) port pins (e.g. 4 port pins can read 10 switches). This is more than double the capacity of a conventional matrix technique, but does not have the ability to handle even double- switch closures well (all such closures are ambiguous; if the aplication dictates that some are permissible and others not, they may be supportable in such a context). To place the switches for use with (n) I/O pins, draw an NxN matrix, strike out the primary diagonal (top-left to bottom-right), and put switches below that. Then add an additional (n) switches between the I/O pins and ground. Scanning such a keyboard is more complicated than scanning a square key- board with the all-rows then all-columns method, but the savings in I/O pins can make it worthwhile.