And here's the in-line text version. Use fixed font for best readability. Implementing Switches with Low Numbers of I/O Lines. >Here's a link to a 4 I/O pin method of reading 12 buttons. Easily >expandable to 16 once you go thru it and understand how it works. http://www.mikerj.clara.net/keypad.html "Jonathan Kirwan" : The web site incorrectly says that 7 I/O lines are needed when no external components are added other than the switches. In the NxM topolgy, that's true. But in the simplex topology, only 6 i/o lines are needed to cover up to 15 switches without diodes. So the situation isn't quite as bad as postulated. However, it's interesting for a number of reasons -- when I/O is tight and lots of diodes are okay -- but also because it's modeled with binomials: I/O Binomial Matrix Usable Required Pins Pattern Columns Switches Diodes 3 1 2 1 3 9 6 4 1 3 3 1 7 28 16 5 1 4 6 4 1 15 75 40 and so on. The number of matrix columns is taken from the sum of values on each rank, minus 1 (the first value.) The numbers on each rank of the binomial means something like this: "How many unique ways can you read zeros back on the available input lines," where is 0 for the first value, 1 for the second, and so on. For example, on the rank for four I/O pins, that reads as, "1 way to uniquely read no zeros back on 3 input lines, 3 ways to uniquely read one zero back on 3 inputs, 3 ways to uniquely read two zeroes back on 3 inputs, and 1 way to uniquely read back three zeroes on 3 inputs." Only the first one, the one asking about how many ways to read back nothing at all (always 1 way) isn't useful for making a switch column, because it's the same as "no key press." If you arrange the diodes so that you can achieve each of these patterns, for each possible active line, the number of usable switches will be the number of I/O lines times the number of columns. If N is the number of I/O pins, then: Columns = [2^(N-1) - 1] Switches = N*[2^(N-1) - 1] Diodes = N*[2^(N-2)] To illustrate an example not shown on that web page, but which shows how these patterns extend, here's the example for 5 I/O lines: >-----------x-----x---x-x-x-x-x-x-------------, >---------x-----x---x---x-x-x-x---x---------, | >-------x-----x-----x-x---x-x---x-x-------, | | >-----x-------x-x-x-------x---x-x-x-----, | | | >-----x-x-x-x---------------x-x-x-x---, | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | O-O-O-O-O-O-O-O-O-O-O-O-O-O-O---' | | | | | | | | | | | | | | | | | | | | | | | O-O-O-O-O-O-O-O-O-O-O-O-O-O-O-----' | | | | | | | | | | | | | | | | | | | | | O-O-O-O-O-O-O-O-O-O-O-O-O-O-O-------' | | | | | | | | | | | | | | | | | | | O-O-O-O-O-O-O-O-O-O-O-O-O-O-O---------' | | | | | | | | | | | | | | | | | O-O-O-O-O-O-O-O-O-O-O-O-O-O-O-----------' The 'x' values show the diode connections that the particular column makes. The ratio of diodes to switches rapidly approaches 1/2. Of course, only one key press at a time for this scheme and often a handful of diodes. Thanks for the site! Jon SIMPLEX TOPOLOGY A simplex is used in a precise way in algebraic topology. A line segment is 1-dim, a triangle is 2-dim, a tetrahedron is 3-dim, and so on. However, that's more than I was trying to say about it. Actually, I meant the 2-d "shadow" of one of these N-dim shapes. In 2-D, it's really easy. Just take any polygon and connect all the vertices together to all the other vertices. For a 4-sided polygon (a square will do), you have to cross-connect the opposing corners so you have six lines connecting four vertices. Make sense? (This simplex square is just a shadow image of the tetrahedron. To try your mind at it, just to be sure you "see" this, first draw a triangle and then place a "dot" in the center of it. Now draw a line from that center dot to each of the three vertices of the triangle. That's one kind of shadow of the tetrahedron. Now draw a square and cross connect the opposing corners, as I mentioned above. Now try and "see" that this simplex square is actually the same tetrahedron just rotated so that one of the diagonals is a "behind edge" and the other diagonal is a "front edge" oriented precisely. If you do this, I think you'll see that both of these are just two special shadows of the same kind of 3-d object.) Anyway, getting back to your question, take those same 5 I/O lines. Draw a pentagon and cross connect all the vertices so that you have a "star" in side it. This gives you a total of 10 lines. Now imagine that each of these lines is a connected switch, instead. That's 10 switches. Connect the I/O lines to the vertices. Now, you can scan the switches by activating one and only one I/O line as an output, one at a time, and "seeing" which of the other four inputs show a connection. With 6 I/O lines, it's a hexagon with the inscribed lines, which is six switches around the perimeter and another nine switches on the inscribed lines, for a total of up to 15 switches with 6 I/O lines. You can actually detect some combinations of two switches being depressed this way. But it takes some effort to maximize that ability. You get (N^2-N)/2 switches with N I/O lines, this way, with no extra parts. You can use the same topology, with two oppositely arranged LEDs along each line or edge and with small valued resistors going from each I/O line to a vertex in order to selectively control up to (N^2-N) LEDs, as well. Typically, this is good if you only need one of them ON at a time. But you can do multiplexing tricks along with enabling multiple edges at a time to get some interesting effects. Jon ----- Original Message ----- From: "Herbert Graf" To: Sent: Saturday, April 12, 2003 6:37 PM Subject: Re: [OT:] different approach to keyboard scanning? > Wow, thanks for the summary, I didn't notice this thread until now. That is > an excellent design! 10keys with only 4 I/O ports used!?? Gonna file this > one in my "info" file... TTYL > > > -----Original Message----- > > From: pic microcontroller discussion list > > [mailto:PICLIST@MITVMA.MIT.EDU]On Behalf Of James Newton, webhost > > Sent: April 12, 2003 18:03 > > To: PICLIST@MITVMA.MIT.EDU > > Subject: Re: [OT:] different approach to keyboard scanning? > > > > > > source= http://www.piclist.com/postbot.asp?id=piclist\2003\04\11\174744a > > > > Let me see if I have all this right. Combining Scott and Eric's > > comments you > > could do this: > > > > 0 1 2 3 GND > > | | | | | > > 0---+--KEY-KEY-KEY-KEY- > > | | | | | > > 1---+---+--KEY-KEY-KEY- > > | | | | | > > 2---+---+---+--KEY-KEY- > > | | | | | > > 3---+---+---+---+--KEY- > > | | | | | > > > > Weak pull-ups enabled, first read the port and check for any lows, this > > would be a key on the GND (far right) column. If all pins are high, ground > > pin 0 and any lows on 1 thru 3 would be a key in the top row. > > Then ground 1 > > and a low on 2 or 3 indicates the two keys on the second row > > (other than the > > last column), finally ground 2 and check the remaining key (on the third > > row) by reading back 3. > > > > That is a 10 key pad in 4 IO lines with no external components other than > > the keys. > > > > --- > > James Newton: PICList.com webmaster, former Admin #3 > > mailto:jamesnewton@piclist.com 1-619-652-0593 phone > > http://www.piclist.com/member/JMN-EFP-786 > > PIC/PICList FAQ: http://www.piclist.com > > > > -- > > http://www.piclist.com hint: PICList Posts must start with ONE topic: > > [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads > > > > -- > http://www.piclist.com hint: PICList Posts must start with ONE topic: > [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads > -- http://www.piclist.com hint: The PICList is archived three different ways. See http://www.piclist.com/#archives for details.