You are on page 1of 5

Matrix Keyboard interface with LCD

Aim: To interface 4X4 matrix keypad and LCD in 4 bit mode with Atmega16
and write program to detect key pressed and display on the LCD.

Requirement: Atmega16 board,

4X4 matrix keypad,

16X2 LCD,

Connecting wires etc.

Software: AVR Studio 7.0,

Proteus 8.0 professional

Interface diagram:
Theory:

The keypad is used as an input device to read the key pressed by the user and to process
it.4x4 keypad consists of 4 rows and 4 columns. Switches are placed between the rows
and columns. A keypress establishes a connection between the corresponding row and
column between which the switch is placed.

In order to read the keypress, we need to configure the rows as outputs and columns as
inputs.

Columns are read after applying signals to the rows in order to determine whether or not
a key is pressed and if pressed, which key is pressed.

LCD:
Liquid Crystal Display is known as LCD is an electronic display which is
commonly used in many applications such as calculators, laptops, tablets, mobile
phones etc. there are many type of LCD
Such as 16×2 character LCD module is a very basic module which is commonly
used by electronic hobbyists and is used in many electronic devices and project.
It can display 2 lines of 16 character and each character is displayed using 5×7
or 5×10 pixel matrix

16×2 LCD can be interfaced with a microcontroller in 8 Bit or 4 Bit mode. These
differs in how data and commands are send to LCD. In 8 Bit mode character data
(as 8 bit ASCII) and LCD command are sent through the data lines D0 to D7.
That is 8 bit data is send at a time and data strobe is given through E of the LCD.
But 4 Bit mode uses only 4 data lines D4 to D7. In this 8 bit data is divided into
two parts and are sent sequentially through the data lines. The idea of 4 bit
communication is introduced to save pins of microcontroller. 4 bit communication
is bit slower than 8 bit but this speed difference has no significance as LCDs are
slow speed devices. Thus 4 bit mode data transfer is most commonly used.

Here, we are going to interface the 4x4 keypad with AVR ATmega16 and will display the pressed
a key on LCD16x2.

LCD16x2 used here in 4-bit mode.

Program
#include <avr/io.h>
#define F_CPU 16000000UL
#include <util/delay.h>
#include "Lcdlbr/lcd.h"

unsigned char keypad[4][4] = { {'7','8','9','/'},


{'4','5','6','*'},
{'1','2','3','-'},
{' ','0','=','+'}};
unsigned char colloc, rowloc;
char keyfind()
{
while(1)
{
DDRA = 0xF0; /* set port direction as input-output */
PORTA = 0xFF;
do
{
PORTA &= 0x0F; /* mask PORT for column read only */
asm("NOP");
colloc = (PINA & 0x0F); /* read status of column */
}
while(colloc != 0x0F);
do
{
do
{
_delay_ms(20); /* 20ms key debounce time */
colloc = (PINA & 0x0F); /* read status of column */
}
while(colloc == 0x0F); /* check for any key press */
_delay_ms (40); /* 20 ms key debounce time */
colloc = (PINA & 0x0F);
}
while(colloc == 0x0F);
/* now check for rows */
PORTA = 0xEF; /* check for pressed key in 1st row */
asm("NOP");
colloc = (PINA & 0x0F);
if(colloc != 0x0F)
{
rowloc = 0;
break;
}
PORTA = 0xDF; /* check for pressed key in 2nd row */
asm("NOP");
colloc = (PINA & 0x0F);
if(colloc != 0x0F)
{
rowloc = 1;
break;
}
PORTA = 0xBF; /* check for pressed key in 3rd row */
asm("NOP");
colloc = (PINA & 0x0F);
if(colloc != 0x0F)
{
rowloc = 2;
break;
}
PORTA = 0x7F; /* check for pressed key in 4th row */
asm("NOP");
colloc = (PINA & 0x0F);
if(colloc != 0x0F)
{
rowloc = 3;
break;
}
}
if(colloc == 0x0E)
return(keypad[rowloc][0]);
else if(colloc == 0x0D)
return(keypad[rowloc][1]);
else if(colloc == 0x0B)
return(keypad[rowloc][2]);
else
return(keypad[rowloc][3]);
}

int main(void)
{

lcd_init(LCD_DISP_ON); /*initialize lcd,display on, cursor on */

lcd_clrscr(); /* clear screen of lcd */


lcd_gotoxy(2,0); /* bring cursor to 4,0 */
lcd_puts("Press a key"); /* type something random */
while(1)
{
lcd_gotoxy(8,1); /* go to 2nd row 1st col */
lcd_putc( keyfind()); /* type something random */
}

You might also like