Exp No.
11 Date: D D - M M - Y Y
MATRIX KEYPAD INTERFACING
AIM
To interface 4x4 matrix keypad with AVR microcontroller and display selected key on LCD.
THEORY
Matrix keypad offers more inputs with the lesser I/O pins. A matrix keypad consists of a set of push-
buttons or switches which are arranged in a matrix format of rows and columns.
The CPU accesses both rows and columns through ports. When a key is pressed, a row and a column make
a contact. otherwise, there is no connection between rows and columns.
All the column pins (Col1 – Col4) are connected to the inputs pins and all the row (Ro1 –Ro4), pins are
connected to the output pins of the microcontroller. In the normal case, all the column pins are pulled up
(HIGH state) by internal or external pull-up resistors. Now we can read the status of each switch through
scanning.
1. A logic LOW is given to Row1 and others (Row2 – Row-4) HIGH
2. Now each Column is scanned. If any switch belongs to the 1st row is pressed the corresponding
column will pull down (logic LOW) and we can detect the pressed key.
3. This process is repeated for all rows.
From the row column pattern it is possible to identify the pressed key with the help of a look up table.
Debouncing can be done either with software or hardware.
PROCEDURE
Row and column lines of keypad are connected to PORTA. LCD is used to display the pressed key. PORTD
is used as data lines, and PORTC 6 and 7 lines are used as RS and EN lines of LCD.
C Program
#define F_CPU 16000000UL /* Define CPU Frequency e.g. here 16MHz */
#include <avr/io.h> /* Include AVR std. library file */
#include <util/delay.h> /* Include inbuilt defined Delay header file */
#define LCD_DATA PORTD //PORT D as data line
#define LCD_RS 6 //of PORT C
#define LCD_EN 7 //of PORT C
//------function declaration---------
void lcd_command(char c);
void lcd_data(char d);
void lcd_puts(const char *s);
//------main function----------------
main()
{
char i,chr;
DDRC=0XFF; //PORT C as o/p line
DDRD=0XFF; //PORT D as o/p line
DDRA=0XF0; //PORT A0-3 as i/p line, 4-7 as o/p
PORTA=0xff; //PORT A pull up enabled
//-------passing commands to lcd for initialization------
lcd_command(0X38); //2 lines, 5x8 matrix, 8-bit mode
lcd_command(0X80); // cursor to the beginning of the 1st line
lcd_command(0X0C); // Display on, cursor off
lcd_command(0X06); // Shift the cursor right
lcd_command(0X01); // Clear the display screen
_delay_us(100);
lcd_puts("CHECK KEYS"); // first line
while(1)
{
_delay_us(5);
lcd_command(0X8B);
_delay_us(5);
PORTA=0x7f; // row1 =0
i=PINA & 0X0F;
//-------reading columns-------------
switch(i)
{
case 0x0E:
chr='0';
lcd_data(chr);
break;
case 0x0D:
chr='4';
lcd_data(chr);
break;
case 0x0B:
chr='8';
lcd_data(chr);
break;
case 0x07:
chr='C';
lcd_data(chr);
break;
}
PORTA=0XBF; //row2=0
i=PINA & 0X0F;
switch(i)
{
case 0x0E:
chr='1';
lcd_data(chr);
break;
case 0x0D:
chr='5';
lcd_data(chr);
break;
case 0x0B:
chr='9';
lcd_data(chr);
break;
case 0x07:
chr='D';
lcd_data(chr);
break;
}
PORTA=0XDF; //row3=0
i=PINA& 0X0F;
switch(i)
{
case 0x0E:
chr='2';
lcd_data(chr);
break;
case 0x0D:
chr='6';
lcd_data(chr);
break;
case 0x0B:
chr='A';
lcd_data(chr);
break;
case 0x07:
chr='E';
lcd_data(chr);
break;
}
PORTA=0XEF; //row4=0
i=PINA& 0X0F;
switch(i)
{
case 0x0E:
chr='3';
lcd_data(chr);
break;
case 0x0D:
chr='7';
lcd_data(chr);
break;
case 0x0B:
chr='B';
lcd_data(chr);
break;
case 0x07:
chr='F';
lcd_data(chr);
break;
}
}
return(0);
}
//-----------function lcd command---------
void lcd_command(char c)
{
LCD_DATA=c;
PORTC&=~(1<<LCD_RS);
PORTC|=(1<<LCD_EN);
_delay_us(5);
PORTC&=~(1<<LCD_EN);
_delay_ms(1);
}
//-----------function lcd data---------
void lcd_data(char d)
{
LCD_DATA=d;
PORTC|=(1<<LCD_RS);
PORTC|=(1<<LCD_EN);
_delay_us(5);
PORTC&=~(1<<LCD_EN);
_delay_ms(1);
}
//-----------function lcd puts---------
void lcd_puts(const char *s)
{
while(*s)
{
lcd_data(*s++);
_delay_us(5);
}
RESULT
Interfaced Matrix Keypad with ATMEGA32 and observed the key pressed on 16x2 LCD.
#define F_CPU 16000000UL /* Define CPU Frequency e.g. here 16MHz */
#include <avr/io.h> /* Include AVR std. library file */
#include <util/delay.h> /* Include inbuilt defined Delay header file */
#define DATA PORTB //PORT B as data line
//------main function----------------
main()
{
char i,chr;
DDRB=0XFF; //PORT B as o/p line
DDRA=0XF0; //PORT A0-3 as i/p line, 4-7 as o/p
PORTA=0xff; //PORT A pull up enabled
while(1)
{
PORTA=0x7f; // row1 =0
i=PINA & 0X0F;
//-------reading columns-------
switch(i)
{
case 0x0E:
DATA =0;
break;
case 0x0D:
DATA =4;
break;
case 0x0B:
DATA =8;
break;
case 0x07:
DATA =0x0C;
break;
}
PORTA=0XBF; //row2=0
i=PINA & 0X0F;
switch(i)
{
case 0x0E:
DATA =1;
break;
case 0x0D:
DATA =5;
break;
case 0x0B:
DATA =9;
break;
case 0x07:
DATA =0x0D;
break;
}
PORTA=0XDF; //row3=0
i=PINA& 0X0F;
switch(i)
{
case 0x0E:
DATA =2;
break;
case 0x0D:
DATA =6;
break;
case 0x0B:
DATA =0x0A;
break;
case 0x07:
DATA =0x0E;
break;
}
PORTA=0XEF; //row4=0
i=PINA& 0X0F;
switch(i)
{
case 0x0E:
DATA =3;
break;
case 0x0D:
DATA =7;
break;
case 0x0B:
DATA =0x0B;
break;
case 0x07:
DATA =0x0F;
break;
}
}
return(0);
}