Switch is the most widely used input device. When a switch is pressed either low or high signal is given to controller depending upon the circuit designing. This approach requires one pin for a single switch. With increasing requirement of input devices, more numbers of controller pins are required which may not be available all the time. For example, to enter value from 0 to 15, we need 16 pins of controller to be interfaced with switches. To reduce number of required pins, we can arrange keys in matrix form.
Matrix keypad is one of the widely used input devices. Some of the application includes Mobile keypad, Telephone dial pad, calculator, ATM etc. Keypad provides an easy way to allow user to provide input to any system. In this article, we will explain how to interface 4x4 matrix keypad with LPC2148. The pressed key will be displayed on LCD.
Here is a simple comparison of two different approaches.
[[wysiwyg_imageupload:11365:]]Fig. 1: Comparison between switches and Matrix Keypad
If we have to identify X=a*b characters, we need only a+b pins from controller as explained in table
In matrix keypad, keys are connected in rows and columns. When any switch is pressed, rows and columns come into contact which is detected by controller to identify which key has been pressed. Let’s see how keypad works.
Keypad in general looks like this.
Fig. 3: Diagram Of Matrix Keypad
For identify the pressed key, we have to follow given procedure.
Fig. 4: Flow Chart Of Detecting Pressed Key
Step-1: Set output port to logic 0 and input port to logic 1.
If all rows are set to logic 1 and no key is pressed, reading input port will result all input pins set at logic 1 as there is no path available for current to flow between Vcc and Ground. It is a function of micro-controller to read input port continuously to identify whether the key has been pressed or not.
Step-2: Whenever any key is pressed, one of the input pin will be grounded by pressed key. By reading input pins, we can identify the column in which key has been pressed by following table.
So now we have identify that in which column the key has been pressed by user. Now we have to identify the row in which key has been pressed.
Step-3: Ground first row and set rest of the rows. Read input port. If all are 1s, no switch in that row is pressed. Now ground second row and set all others rows at logic 1 and see whether any key has been pressed in that row or not. Continue same process until you find out in which row, key is pressed.
Once we know column and row in which key has been pressed, we can identify the pressed key easily.
Step-4: Ground all rows and read columns. If any of the columns has 0, keep doing this process unless all columns are 1.
Step-5: Once we have identified the pressed key, we can display it on LCD, Serial Port etc. as per the requirement.
Project Source Code
###
//Program to interface 16x2 LCD with LPC 2148 // Pressed key has been displayed on LCD using this functions #include <LPC21xx.h> #include "lcd4bit.h" void write_command(int cmd) { IO1CLR |= 0x00f00000; /* Clear D4-D7 */ IO1CLR |= 0x00040000; /* Read/Write = 0 */ IO1CLR |= 0X00020000; /* Register Select = 0,Command */ IO1SET |= 0x00f00000 & cmd; /* Set D4-D7 */ IO1SET |= 0X00080000; /* Enable = 1 */ Delay(30000); IO1CLR |= 0x00080000; /* set E to low */ } void write_data(int dat) { IO1CLR |= 0x00f00000; /* Clear D4-D7 */ IO1CLR |= 0x00040000; /* Read/Write = 0 */ IO1SET |= 0X00020000; /* Register Select = 1,Data */ IO1SET |= 0x00f00000 & dat; /* Set D4-D7 */ IO1SET |= 0X00080000; /* Enable = 1 */ Delay(30000); //delay ~2ms IO1CLR |= 0x00080000; /* Set E to low */ } void lcd_data(char dat) { write_data(dat << 16); write_data(dat << 20); } void lcd_command(char cmd) { write_command(cmd << 16); write_command(cmd << 20); } void printlcd(char *CPtr) { while(*CPtr != '