16 key Keypad
Autor: lolpes | Publicado: 2011-02-20T06:11:00-08:00

Hello again, this time i'll show how to use a keypad.The most basic form of input in a system is digital input through a button. You press the button, the system detects it and respond accordingly.So, as one or two buttons is boring, let's go with 16 (this "method" should work for any number, but 16 is the most common number of keys in a keypad).I'll use the Futurlec's Keypad Mini Board, LINK, as reference design.This "method" for reading the keys is based on a matrix configuration. The buttons are linked in rows and colums.How does it work?The default state of the rows and colums is high (that's why the pull-up resistor's are there), and then you force a line to go low, and look which colum went low. This gives you the coordinate of the pressed button, as the colum will only go low if one of it's button is pressed, and the line you already know (you turned it low).Read the "16keypad.inc" to understand the routine, LINK. But basicaly it consists in turning one row at a time low and looking for which colums went low, and for each button detected as pressed, setting a corresponding bit on a memory register word.One thing you should be aware of is the bouncing effect while pressing and releasing a button.I'm borrowing two images from here, LINK:When you press or release a button, the voltage level doesn't goes high or low immediatly. Instead, it spends some time 'bouncing' between the two states. So, it may occur that your routine for button state detection is fast enough as to try to detect the button state while the boucing is still going on. One way to avoid it is to check the button state more than one time in a small time interval, and only use the result if the two readings are positive for button pressed.So, in my routine, i read the states of all buttons once, and 5ms after i do it again and compare the results. Some recomend 10ms or 20ms, this depends on the button and the level of noise in the system. You can also do more than one re-reading of the button states. This is called "debouncing".Another thing is the simultaneously pressed key detection.My program reads the keypad and shows on the LCD all the keys detected as pressed. But there is a... let's call difficulty. It may detect more keys than are pressed. Why?If you examine the logic of the wiring, you'll see that if you press any 3 keys that happens to be 2 on the same colum and 2 on the same row, a fourth key will be detected as pressed, the one that "closes" the rectangle:The '4', '8' and 'A' key's are detected correctly, but the '6' key is not. That's becouse this kind of configuration of pressed keys gives a route for the electric current to bypass the not-pressed button logic.A simple way to avoid this is using diodes between each button and the colum wire, allowing the current to flow one way but not the other. Unhappily, you don't find this kind of keypads to buy, so i think you can just ignore the reading result whenever they return positive for more than 3 buttons.Finaly, here are some pics of the working thing (again, sorry for the poor quality, it's my cell phone camera):One key pressed, two keys pressed, three keys that not generates a "difficulty", and three keys generating a difficulty. Nice.One note to conclude this post: these assemblies with jumper wires and breadboards are very noisy. This sucks.