Professional Documents
Culture Documents
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.
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.
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.
CIRCUIT DIAGRAM
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)
{
CODE2
//Program to interface Matrix Keypad with LPC 2148
#include<lpc21xx.h>
#include "lcd4bit.h"
#define CLR 0x0003C000
Unsigned char check (int);
void delay(int);
int main(void)
{
init_lcd();
lcd_command(0x01);
lcd_command(0x80);
printlcd("4x4 KEYPAD");
lcd_command(0xC0);
printlcd("with LPC 2148");
delay(2000);
lcd_command(0x01);
lcd_command(0x80);
printlcd("Developed By:");
lcd_command(0xC0);
printlcd("Dishant Shah");
delay(2000);
lcd_command(0x01);
lcd_command(0x80);
printlcd("Enter Value");
lcd_command(0xC0);
IO0DIR = 0X0003C000;
while(1)
{
IO0CLR = CLR;
IO0SET = O1;
delay(10);
if(check(I1))
{
lcd_data('0');
}
if(check(I2))
{
lcd_data('1');
}
if(check(I3))
{
lcd_data('2');
}
if(check(I4))
{
lcd_data('3');
}
IO0CLR = CLR;
IO0SET = O2;
if(check(I1))
{
lcd_data('4');
}
if(check(I2))
{
lcd_data('5');
}
if(check(I3))
{
lcd_data('6');
}
if(check(I4))
{
lcd_data('7');
}
IO0CLR = CLR;
IO0SET = O3;
if(check(I1))
{
lcd_data('8');
}
if(check(I2))
{
lcd_data('9');
}
if(check(I3))
{
lcd_data('A');
}
if(check(I4))
{
lcd_data('B');
}
IO0CLR = CLR;
IO0SET = O4;
if(check(I1))
{
lcd_data('C');
}
if(check(I2))
{
lcd_data('D');
}
if(check(I3))
{
lcd_data('E');
}
if(check(I4))
{
lcd_data('F');
}
}
}
char check(int val) /* scanning a a key */
{
while((IO0PIN & 0X00003C00)==val)
{
delay(50);
if((IO0PIN & 0X00003C00)== 0X00003C00)return(1);
}
return(0) ;
}
void delay(int n) /* generates one
milisecond delay */
{
int i,j;
for (i=1; i<=n; i++)
for(j=0; j<=40000; j++);
}