Hello Readers, welcome to my Blog BloggerTipsTricks. com - A Blog For Bloggers By A Blogger. I'm Arun, an entrepreneur Blogger from India.

4X4 Keypad Interfacing with ATmega16 and LCD Display

14130
Mark as Favourite

Keypads are parts of HMI or Human Machine Interface and play really important role in a small embedded system where human interaction or human input is needed. Matrix keypads are well known for their simple architecture and ease of interfacing.

In this project, we will learn How to interface a 4X4 keypad with AVR ATmega16 microcontroller. Here, we will display the value of the pressed key in a 16X2 alphanumeric LCD. See the below table to know the value assigned to each key of the 4X4 keypad. For to display the value of the pressed key, the microcontroller should know if any key is pressed or not and if pressed then which key is pressed. To know all this, the microcontroller scans each row or column of the 4X4 keypad one by one continuously (scanning is done by the read_keypad function). When any key of 4X4 keypad is pressed, the read_keypad function returns the value of the pressed key and the microcontroller displays it in the 16X2 alphanumeric LCD. When none of the key is pressed the read_keypad function returns 0XFF and the LCD displays the value of the last pressed key. Now, press different keys of the 4×4 keypad and see the output in the 16×2 alphanumeric LCD.

Key Pressed

Key Value

1

1

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

0

0

*

*

#

#

A

A

B

B

C

C

D

D

 

Softwares Required

Hardwares Required

Name / Unit Price Quantity Total Price
AVR Trainer Board-100 with ATmega16
Rs. 700.00
1pc
Rs.700.00
AVR USB Programmer
Rs. 350.00
1pc
Rs.350.00
12V, 1A DC SMPS Adaptor
Rs. 130.00
1pc
Rs.130.00
RG1602A 16X2 Alphanumeric LCD Green
Rs. 150.00
1pc
Rs.150.00
4X4 Keypad
Rs. 120.00
1pc
Rs.120.00
1 to 1 Connector-Female to Female
Rs. 4.00
3pc
Rs.12.00
10 to 10 FRC Female to Female Connector
Rs. 15.00
3pc
Rs.45.00
TOTAL Rs.1507.00

Circuit Diagram

C Program

Fullscreen
 

 //**************************************************************//
//System Clock                          :1MHz
//Software                                 :AVR Studio 4
//LCD Data Interfacing             :8-Bit
//**************************************************************//

#include<avr/io.h>
/*Includes io.h header file where all the Input/Output Registers and its Bits are defined for all AVR microcontrollers*/

#define          F_CPU          1000000
/*Defines a macro for the delay.h header file. F_CPU is the microcontroller frequency value for the delay.h header file. Default value of F_CPU in delay.h header file is 1000000(1MHz)*/

#include<util/delay.h>
/*Includes delay.h header file which defines two functions, _delay_ms (millisecond delay) and _delay_us (microsecond delay)*/

#define          KEYPAD_PORT          PORTC
/*Defines a macro for the keypad.h header file. KEYPAD_PORT is the microcontroller PORT Register to which 4X4 keypad is connected. Default PORT Register in keypad.h header file is PORTB*/

#define          KEYPAD_PIN          PINC
/*Defines a macro for the keypad.h header file. KEYPAD_PIN is the microcontroller PIN Register to which 4X4 keypad is connected. Default PIN Register in keypad.h header file is PINB*/

#include<avr/keypad.h>
/*Includes keypad.h header file which defines one function: read_keypad () to read the 4X4 keypad. Keypad header file version is 1.1*/

#define          LCD_DATA_PORT          PORTB
/*Defines a macro for the lcd.h header File. LCD_DATA_PORT is the microcontroller PORT Register to which the data pins of the LCD are connected*/

#define          LCD_CONT_PORT          PORTD
/*Defines a macro for the lcd.h header File. LCD_CONT_PORT is the microcontroller PORT Register to which the control pins of the LCD are connected*/

#define          LCD_RS          PD0
/*Defines a macro for the lcd.h header file. LCD_RS is the microcontroller Port pin to which the RS pin of the LCD is connected*/

#define          LCD_RW          PD1
/*Defines a macro for the lcd.h header file. LCD_RW is the microcontroller Port pin to which the RW pin of the LCD is connected*/

#define          LCD_EN          PD2
/*Defines a macro for the lcd.h header file. LCD_EN is the microcontroller Port pin to which the EN pin of the LCD is connected*/

#include<avr/lcd.h>
/*Includes lcd.h header file which defines different functions for all Alphanumeric LCD(8-Bit Interfacing Method). LCD header file version is 1.1*/

void main(void)
{

DDRB=0xff;
/*All 8 pins of PortB are declared output (data pins of LCD are connected)*/

DDRD=0x07;
/*PD0, PD1 and PD2 pins of PortD are declared output (control pins of LCD are connected)*/

DDRC=0x0f;
/*PortC’s upper 4 bits are declared input and lower 4 bits are declared output(4×4 Keypad is connected)*/

PORTC=0xff;
/*PortC’s lower 4 bits are given high value and pull-up are enabled for upper 4 bits*/

unsigned char keypad_value;
/*Variable declarations*/

lcd_init();
/*LCD initialization*/

lcd_string_write(“ABLab Solutions”);
/*String display in 1st row of LCD*/

lcd_command_write(0xc0);
/*Cursor moves to 2nd row 1st column of LCD*/

lcd_string_write(“www.ablab.in”);
/*String display in 2nd row of LCD*/

_delay_ms(500);
_delay_ms(500);
_delay_ms(500);
_delay_ms(500);
/*Display stays for 2 second*/

lcd_command_write(0x01);
/*Clear Screen*/

/*Start of infinite loop*/
while(1)
{

lcd_command_write(0x80);
/*Cursor moves to 1st row 1st column of LCD*/

lcd_string_write(“Press any Key”);
/*String display in 1st row of LCD*/

lcd_command_write(0xc0);
/*Cursor moves to 2nd row 1st column of LCD*/

lcd_string_write(“Key_Value: “);
/*String display in 2nd row of LCD*/

keypad_value=read_keypad();
/*Scan’s 4X4 keypad and returns pressed key value or default value*/

/*Checking if any key is pressed or not*/
if(keypad_value!=0xff)
{

switch(keypad_value)
{

case 0:

lcd_data_write(‘0’);
/*Displays 0 in 2nd row of LCD*/

break;
/*Break statement*/

case 1:

lcd_data_write(‘1’);
/*Displays 1 in 2nd row of LCD*/

break;
/*Break statement*/

case 2:

lcd_data_write(‘2’);
/*Displays 2 in 2nd row of LCD*/

break;
/*Break statement*/

case 3:

lcd_data_write(‘3’);
/*Displays 3 in 2nd row of LCD*/

break;
/*Break statement*/

case 4:

lcd_data_write(‘4’);
/*Displays 4 in 2nd row of LCD*/

break;
/*Break statement*/

case 5:

lcd_data_write(‘5’);
/*Displays 5 in 2nd row of LCD*/

break;
/*Break statement*/

case 6:

lcd_data_write(‘6’);
/*Displays 6 in 2nd row of LCD*/

break;
/*Break statement*/

case 7:

lcd_data_write(‘7’);
/*Displays 7 in 2nd row of LCD*/

break;
/*Break statement*/

case 8:

lcd_data_write(‘8’);
/*Displays 8 in 2nd row of LCD*/

break;
/*Break statement*/

case 9:

lcd_data_write(‘9’);
/*Displays 9 in 2nd row of LCD*/

break;
/*Break statement*/

case 10:

lcd_data_write(‘*’);
/*Displays * in 2nd row of LCD*/

break;
/*Break statement*/

case 11:

lcd_data_write(‘#’);
/*Displays # in 2nd row of LCD*/

break;
/*Break statement*/

case 12:

lcd_data_write(‘A’);
/*Displays A in 2nd row of LCD*/

break;
/*Break statement*/

case 13:

lcd_data_write(‘B’);
/*Displays B in 2nd row of LCD*/

break;
/*Break statement*/

case 14:

lcd_data_write(‘C’);
/*Displays C in 2nd row of LCD*/

break;
/*Break statement*/;

case 15:

lcd_data_write(‘D’);
/*Displays D in 2nd row of LCD*/

break;
/*Break statement*/;

}

}
else
{

;
/*Null statement*/

}

_delay_ms(200);
/*200ms delay as guard time between two consecutive pressing of key*/

}

}
/*End of Program*/

  

 //**************************************************************//
//System Clock                          :1MHz
//Software                                 :AVR Studio 4
//LCD Data Interfacing             :8-Bit
//**************************************************************//

#include<avr/io.h>
/*Includes io.h header file where all the Input/Output Registers and its Bits are defined for all AVR microcontrollers*/

#define          F_CPU          1000000
/*Defines a macro for the delay.h header file. F_CPU is the microcontroller frequency value for the delay.h header file. Default value of F_CPU in delay.h header file is 1000000(1MHz)*/

#include<util/delay.h>
/*Includes delay.h header file which defines two functions, _delay_ms (millisecond delay) and _delay_us (microsecond delay)*/

#define          KEYPAD_PORT          PORTC
/*Defines a macro for the keypad.h header file. KEYPAD_PORT is the microcontroller PORT Register to which 4X4 keypad is connected. Default PORT Register in keypad.h header file is PORTB*/

#define          KEYPAD_PIN          PINC
/*Defines a macro for the keypad.h header file. KEYPAD_PIN is the microcontroller PIN Register to which 4X4 keypad is connected. Default PIN Register in keypad.h header file is PINB*/

#include<avr/keypad.h>
/*Includes keypad.h header file which defines one function: read_keypad () to read the 4X4 keypad. Keypad header file version is 1.1*/

#define          LCD_DATA_PORT          PORTB
/*Defines a macro for the lcd.h header File. LCD_DATA_PORT is the microcontroller PORT Register to which the data pins of the LCD are connected*/

#define          LCD_CONT_PORT          PORTD
/*Defines a macro for the lcd.h header File. LCD_CONT_PORT is the microcontroller PORT Register to which the control pins of the LCD are connected*/

#define          LCD_RS          PD0
/*Defines a macro for the lcd.h header file. LCD_RS is the microcontroller Port pin to which the RS pin of the LCD is connected*/

#define          LCD_RW          PD1
/*Defines a macro for the lcd.h header file. LCD_RW is the microcontroller Port pin to which the RW pin of the LCD is connected*/

#define          LCD_EN          PD2
/*Defines a macro for the lcd.h header file. LCD_EN is the microcontroller Port pin to which the EN pin of the LCD is connected*/

#include<avr/lcd.h>
/*Includes lcd.h header file which defines different functions for all Alphanumeric LCD(8-Bit Interfacing Method). LCD header file version is 1.1*/

void main(void)
{

DDRB=0xff;
/*All 8 pins of PortB are declared output (data pins of LCD are connected)*/

DDRD=0x07;
/*PD0, PD1 and PD2 pins of PortD are declared output (control pins of LCD are connected)*/

DDRC=0x0f;
/*PortC’s upper 4 bits are declared input and lower 4 bits are declared output(4×4 Keypad is connected)*/

PORTC=0xff;
/*PortC’s lower 4 bits are given high value and pull-up are enabled for upper 4 bits*/

unsigned char keypad_value;
/*Variable declarations*/

lcd_init();
/*LCD initialization*/

lcd_string_write(“ABLab Solutions”);
/*String display in 1st row of LCD*/

lcd_command_write(0xc0);
/*Cursor moves to 2nd row 1st column of LCD*/

lcd_string_write(“www.ablab.in”);
/*String display in 2nd row of LCD*/

_delay_ms(500);
_delay_ms(500);
_delay_ms(500);
_delay_ms(500);
/*Display stays for 2 second*/

lcd_command_write(0x01);
/*Clear Screen*/

/*Start of infinite loop*/
while(1)
{

lcd_command_write(0x80);
/*Cursor moves to 1st row 1st column of LCD*/

lcd_string_write(“Press any Key”);
/*String display in 1st row of LCD*/

lcd_command_write(0xc0);
/*Cursor moves to 2nd row 1st column of LCD*/

lcd_string_write(“Key_Value: “);
/*String display in 2nd row of LCD*/

keypad_value=read_keypad();
/*Scan’s 4X4 keypad and returns pressed key value or default value*/

/*Checking if any key is pressed or not*/
if(keypad_value!=0xff)
{

switch(keypad_value)
{

case 0:

lcd_data_write(‘0’);
/*Displays 0 in 2nd row of LCD*/

break;
/*Break statement*/

case 1:

lcd_data_write(‘1’);
/*Displays 1 in 2nd row of LCD*/

break;
/*Break statement*/

case 2:

lcd_data_write(‘2’);
/*Displays 2 in 2nd row of LCD*/

break;
/*Break statement*/

case 3:

lcd_data_write(‘3’);
/*Displays 3 in 2nd row of LCD*/

break;
/*Break statement*/

case 4:

lcd_data_write(‘4’);
/*Displays 4 in 2nd row of LCD*/

break;
/*Break statement*/

case 5:

lcd_data_write(‘5’);
/*Displays 5 in 2nd row of LCD*/

break;
/*Break statement*/

case 6:

lcd_data_write(‘6’);
/*Displays 6 in 2nd row of LCD*/

break;
/*Break statement*/

case 7:

lcd_data_write(‘7’);
/*Displays 7 in 2nd row of LCD*/

break;
/*Break statement*/

case 8:

lcd_data_write(‘8’);
/*Displays 8 in 2nd row of LCD*/

break;
/*Break statement*/

case 9:

lcd_data_write(‘9’);
/*Displays 9 in 2nd row of LCD*/

break;
/*Break statement*/

case 10:

lcd_data_write(‘*’);
/*Displays * in 2nd row of LCD*/

break;
/*Break statement*/

case 11:

lcd_data_write(‘#’);
/*Displays # in 2nd row of LCD*/

break;
/*Break statement*/

case 12:

lcd_data_write(‘A’);
/*Displays A in 2nd row of LCD*/

break;
/*Break statement*/

case 13:

lcd_data_write(‘B’);
/*Displays B in 2nd row of LCD*/

break;
/*Break statement*/

case 14:

lcd_data_write(‘C’);
/*Displays C in 2nd row of LCD*/

break;
/*Break statement*/;

case 15:

lcd_data_write(‘D’);
/*Displays D in 2nd row of LCD*/

break;
/*Break statement*/;

}

}
else
{

;
/*Null statement*/

}

_delay_ms(200);
/*200ms delay as guard time between two consecutive pressing of key*/

}

}
/*End of Program*/

Connection Guide

The step-by-step connection guide for 4X4 Keypad Interfacing with ATmega16 and LCD Display is as follows:

  • Insert the DC Pin of 12V, 1A DC Adapter to the DC Socket of AVR Trainer Board-100.

    Steps - 1 ( 0f 9 )

  • Connect PortB header with LCD data header in AVR Trainer Board-100 with a 10 to 10 FRC Female Connector.

    Steps - 2 ( 0f 9 )

  • Connect RS, RW & EN pins of LCD control header with PD0, PD1 & PD2 pins of PortD header respectively in AVR Trainer Board-100 with 1 to 1 Connectors.

    Steps - 3 ( 0f 9 )

  • Connect the 16X2 Alphanumeric LCD to the LCD header of AVR Trainer Board-100.

    Steps - 4 ( 0f 9 )

  • Connect the 4X4 Keypad header of 4X4 Keypad with PortC header of AVR Trainer Board-100 with a 10 to 10 FRC Female Connector.

    Steps - 5 ( 0f 9 )

  • Connect the ISP header of AVR Trainer Board-100 with AVR USB Programmer header of AVR USB Programmer with a 10 to 10 FRC Female Connector.

    Steps - 6 ( 0f 9 )

  • Connect the AVR USB Programmer to the PC/Laptop's USB Port directly or with the help of USB AM-FM Cable.

    Steps - 7 ( 0f 9 )

  • Switch on the power with the help of Power Switch of AVR Trainer Board-100.

    Steps - 8 ( 0f 9 )

  • Download the 4X4 Keypad Interfacing with ATmega16 and LCD Display Hex file to AVR Trainer Board-100 with the help of SinaProg Hex downloader and AVR USB Programmer.

    Steps - 9 ( 0f 9 )

  • Insert the DC Pin of 12V, 1A DC Adapter to the DC Socket of AVR Trainer Board-100.

    Steps - 1 ( 0f 9 )

  • Connect PortB header with LCD data header in AVR Trainer Board-100 with a 10 to 10 FRC Female Connector.

    Steps - 2 ( 0f 9 )

  • Connect RS, RW & EN pins of LCD control header with PD0, PD1 & PD2 pins of PortD header respectively in AVR Trainer Board-100 with 1 to 1 Connectors.

    Steps - 3 ( 0f 9 )

  • Connect the 16X2 Alphanumeric LCD to the LCD header of AVR Trainer Board-100.

    Steps - 4 ( 0f 9 )

  • Connect the 4X4 Keypad header of 4X4 Keypad with PortC header of AVR Trainer Board-100 with a 10 to 10 FRC Female Connector.

    Steps - 5 ( 0f 9 )

  • Connect the ISP header of AVR Trainer Board-100 with AVR USB Programmer header of AVR USB Programmer with a 10 to 10 FRC Female Connector.

    Steps - 6 ( 0f 9 )

  • Connect the AVR USB Programmer to the PC/Laptop's USB Port directly or with the help of USB AM-FM Cable.

    Steps - 7 ( 0f 9 )

  • Switch on the power with the help of Power Switch of AVR Trainer Board-100.

    Steps - 8 ( 0f 9 )

  • Download the 4X4 Keypad Interfacing with ATmega16 and LCD Display Hex file to AVR Trainer Board-100 with the help of SinaProg Hex downloader and AVR USB Programmer.

    Steps - 9 ( 0f 9 )

Downloads

4x4 Keypad Interfacing with ATmega16 and LCD Display C File
  • 96 downloads
  • 6 Kb
4x4 Keypad Interfacing with ATmega16 and LCD Display Circuit Diagram
  • 56 downloads
  • 197 Kb
4x4 Keypad Interfacing with ATmega16 and LCD Display Hex File
  • 68 downloads
  • 3 Kb
Alphanumeric LCD Header File v1.0
  • 826 downloads
  • 1 Kb
4X4 Keypad Header File v1.0
  • 241 downloads
  • 2 Kb

Comments