Thursday, January 20, 2011

4.1 Interfacing 4x4 key pad

Hello Readers:

I have started this blog to show you Embedded C programming for 8051 family microcontroller using KEIL compiler. The KISS philosophy is used to write this tutorial. What is KISS? Keep It Simple, Stupid!


4. Key Pad Interface [4x4]

This section teaches you how to interface 4x4 matrix key pad to microcontroller chip.

4.1 Interfacing 4x4 key pad


The key pad interface is widely demanded hardware interface unit in embedded systems. This program is better understood with closer look of code lines and flow chart diagram.

Start KEIL V4.01 and close projects, if any are open. Now start new project with Device -> Generic->8051 [all variants] for general 8051 family target chip. Add a new text file, name it Pgm_41.C and add it to above created project as a source file. It is time to add code to Pgm_41.C, so type below shown code in it.
 


Code explanation: The program goes thorough the following four major stages:
1.    To make sure that the preceding key has been released, 0s are output to all rows at once, and the columns are read and checked repeatedly unitl all the columns are high. When all columns are found to be high, the program waits for a short amount of time before it goes to the next stage of waiting for a key to be pressed.
2.    To see if any key is pressed, the columns are scanned over and over in an infinite loop unitl one of them has a 0 on it. Remember that the output latches connected to rows still have their initial zeros (provided in stage 1), making thm grounded. After the key press detection, the microcontroller waits 20ms for the bounce and then scans the columns again. This serves two functions: (a) it ensures that the first key press detection was not an erroneous one due to a spike noise, and (b) the 20-ms delay prevents the same key press from being interpreted as a multiple key press. If after the 20-ms delay the key is still pressed, it goes to the next stage to detect which row it belongs to; otherwise, it goes back into the loop to detect a real key press.
3.    To detect which row the key press belongs to, the microcontroller grounds one row at a time, reading the columns each time. If it finds that all columns are high, this means that the key press cannot belong to that row; therefore, it grounds the next row and continues until it finds the row the key press belongs to. Upon finding the row that the key press belongs to, it sets up the starting address for the look-up table holding the scan codes (or the ASCII value) for that row and goes to the next stage to identify the key.
4.    To identify the key press, the microcontroller rotates the column bits, one bit at a time, into the carry flag and checks to see if it is low. Upon finding the zero, it pulls out the ASCII code for that key from the look-up table; otherwise, it increments the pointer to point to the next element of the look-up table.

While the key press detection is standard for all keyboards, the process for determining which key is pressed varies. The look-up table method can be modified to work with any matrix up to 8 x 8.

Below is the flow chart diagram of program PGM_41.C.


After typing the code, press F7 or click Project->Build Target for building the source code. If there are no typical errors file will build successively and shows zero errors and zero warnings. You can burn the hex file generated by this program for hardware simulation. Note that this program needs user input i.e., KEY press for software simulation. Note that Port 1 is used as output port or row port and Port 2 is used as input port or column port.

<End of 4.1 Interfacing 4x4 key pad>
 
...till next post bye-bye & take care.

No comments:

Post a Comment