728x90

728x90

Wednesday, January 28, 2026

ATMega644P LCD Display and Matrix Keypad Interface

Using a matrix keypad could save the I/O port of a micro-controller. For instant a 4x4 matrix keypad yields different 16 key values. It requires only 8 pins of micro-controller or one I/O port only. We can program the keypad scanning from scratch using C language.The program just divides the 8-bit port into two nibbles, one for output scanning and one for input key press detecting. 

ATMega644P LCD Display and Matrix Keypad Interface

 

A matrix keypad can be made from scratch by using tactile switches and breadboard soldering without hazardous chemical etching. However there are many types of matrix keypad that made from tactile switches or even membrane switches. They are available at very low cost.

PIC16F887 PCF8574AP I2C 4x4 KeyPad using XC8
16 Key Membrane Switch Keypad 4X4 3X4 Matrix Keyboard For Arduino DIY Kit

In a simple introductory example, I just make a keypad scanning routine that uses PortD. Whenever any key press is founded the key value will display on a single seven-segment connects to PortA.


  1. /*
  2. * 7-keyapd_4x4_7.c
  3. *
  4. * Created: 1/28/2026 7:29:49 PM
  5. * Author : Admin
  6. */

  7. #include <avr/io.h>
  8. #include <util/delay.h>
  9. #define F_CPU 16000000UL

  10. /*
  11. const char key_16[][4]={'1','2','3','A',
  12. '4','5','6','B',
  13. '7','8','9','C',
  14. '*','0','#','D'};
  15. */
  16. const char key_16[][4]={7,8,9,15,
  17. 4,5,6,14,
  18. 1,2,3,13,
  19. 10,0,11,12};
  20. const uint8_t cc_7[16]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,
  21. 0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
  22. volatile char keyScan(void){
  23. char data=0,temp,key;
  24. for(uint8_t i=0;i<4;i++){
  25. data=0xFF;
  26. data&=~(1<<i);
  27. PORTB=data;
  28. _delay_ms(5);
  29. data=PINB;
  30. data&=0xF0;
  31. if((data&0x10)==0) {temp=key_16[i][0]; break;}
  32. else if((data&0x20)==0){temp=key_16[i][1]; break;}
  33. else if((data&0x40)==0){temp=key_16[i][2]; break;}
  34. else if((data&0x80)==0){temp=key_16[i][3]; break;}
  35. else temp=0xFF;
  36. _delay_ms(10);
  37. }
  38. return temp;
  39. }
  40. int main(void)
  41. {
  42. /* Replace with your application code */
  43. DDRB=0x0F;
  44. PINB=0x0F;
  45. DDRD=0xFF;
  46. char temp;
  47. while (1)
  48. {
  49. temp=keyScan();
  50. if(temp!=0xFF){
  51. PORTD=cc_7[temp];
  52. _delay_ms(250);
  53. }
  54. }
  55. }



I just tested this example in software simulator since I don't want wire additional seven-segment display. 

ATMega644P LCD Display and Matrix Keypad Interface
Schematic and Simulation Program 

I will use the real hardware on next example. 

However we can use other micro-controller chips or even additional interface chips. 

 Here I added the LCD driver to this example program since it's already placed on-board.

  1. /*
  2. * 7-keypad_4x4_lcd.c
  3. *
  4. * Created: 1/28/2026 6:20:48 PM
  5. * Author : Admin
  6. */

  7. #include <avr/io.h>
  8. #include <util/delay.h>
  9. #define F_CPU 16000000UL


  10. #include "hd44780.c"

  11. const char key_16[4][4]={'1','2','3','A',
  12. '4','5','6','B',
  13. '7','8','9','C',
  14. '*','0','#','D'};

  15. /*
  16. const char key_16[4][4]={'7','8','9','/',
  17. '4','5','6','X',
  18. '1','2','3','-',
  19. '*','0','=','+'};
  20. */

  21. volatile char keyScan(void){
  22. char data=0,temp,key;
  23. for(uint8_t i=0;i<4;i++){
  24. data=0xFF;
  25. data&=~(1<<i);
  26. PORTB=data;
  27. _delay_ms(5);
  28. data=PINB;
  29. data&=0xF0;
  30. if((data&0x10)==0) {temp=key_16[i][0]; break;}
  31. else if((data&0x20)==0){temp=key_16[i][1]; break;}
  32. else if((data&0x40)==0){temp=key_16[i][2]; break;}
  33. else if((data&0x80)==0){temp=key_16[i][3]; break;}
  34. else temp=0;
  35. _delay_ms(10);
  36. }
  37. return temp;
  38. }
  39. int main(void)
  40. {
  41. /* Replace with your application code */
  42. DDRB=0x0F;
  43. PINB=0x0F;
  44. lcd_init();
  45. lcd_text("ATMega644P LCD");
  46. lcd_xy(1,2);
  47. lcd_text("4x4 Key Pad");
  48. _delay_ms(10000);
  49. lcd_clear();
  50. char temp,charCount=0,newLine=0,line=1;
  51. while (1)
  52. {
  53. temp=keyScan();
  54. if(temp!=0){
  55. lcd_data(temp);
  56. charCount++;
  57. _delay_ms(500);
  58. }
  59. if(charCount>=16){
  60. newLine=1;
  61. charCount=0;
  62. line+=1;
  63. }
  64. if(newLine){
  65. newLine=0;
  66. if(line==2) lcd_xy(1,2);
  67. else{
  68. lcd_command(0x01);
  69. _delay_ms(5);
  70. line=1;
  71. }
  72. }
  73. }
  74. }


It requires the "hd44780.c" that created in previous post.

hd44780.c 

  1. /*
  2. * hd44780.c
  3. *
  4. * Created: 1/28/2026 9:55:18 PM
  5. * Author: Admin
  6. */


  7. #include "hd44780.h"


  8. void lcd_command(char command){
  9. char temp;
  10. temp=command&0xF0;
  11. PORTC=temp|(1<<EN);
  12. _delay_us(d_time);
  13. PORTC=temp;
  14. _delay_us(d_time);
  15. temp=command<<4;
  16. PORTC=temp|(1<<EN);
  17. _delay_us(d_time);
  18. PORTC=temp;
  19. _delay_us(d_time);
  20. }

  21. void lcd_data(char data){
  22. char temp;
  23. temp=data&0xF0;
  24. PORTC=temp|(1<<EN)|(1<<RS);
  25. _delay_us(d_time);
  26. PORTC=temp|(1<<RS);
  27. _delay_us(d_time);
  28. temp=data<<4;
  29. PORTC=temp|(1<<EN)|(1<<RS);
  30. _delay_us(d_time);
  31. PORTC=temp|(1<<RS);
  32. _delay_us(d_time);
  33. }
  34. void lcd_init(void){
  35. DDRC=0xFF;
  36. lcd_command(0x33);
  37. _delay_us(100);
  38. lcd_command(0x32);
  39. _delay_us(100);
  40. lcd_command(0x28);
  41. _delay_us(100);
  42. lcd_command(0x0F);
  43. _delay_us(100);
  44. lcd_command(0x01);
  45. _delay_ms(5);
  46. lcd_command(0x06);
  47. _delay_us(100);
  48. }
  49. void lcd_xy(char x, char y){
  50. char addr[]={0x80,0xC0};
  51. lcd_command(addr[y-1]+x-1);
  52. }
  53. void lcd_text(char *text){
  54. while(*text) lcd_data(*text++);
  55. }
  56. void lcd_clear(void){
  57. lcd_command(0x01);
  58. _delay_ms(5);
  59. }


hd44780.h

  1. /*
  2. * hd44780.h
  3. *
  4. * Created: 1/28/2026 9:56:39 PM
  5. * Author: Admin
  6. */


  7. #ifndef HD44780_H_
  8. #define HD44780_H_





  9. #endif /* HD44780_H_ */

  10. #include <avr/io.h>
  11. #include <stdio.h>
  12. #include <util/delay.h>

  13. #define F_CPU 16000000UL


  14. #define RS 2
  15. #define EN 3

  16. const char d_time=50;

  17. extern void lcd_command(char command);
  18. extern void lcd_data(char data);
  19. extern void lcd_init(void);
  20. extern void lcd_xy(char x, char y);
  21. extern void lcd_text(char *text);
  22. extern void lcd_clear(void);

 

The key press counts is auto-increment and it will start a new line whenever it reaches 16 character counts. 


ATMega644P LCD Display and Matrix Keypad Interface
Proteus Simulation

In Proteus it work slower than in real hardware since I add a 500ms delay time. 

ATMega644P LCD Display and Matrix Keypad Interface

ATMega644P LCD Display and Matrix Keypad Interface

ATMega644P LCD Display and Matrix Keypad Interface


However on my AVR Prototype Board (PCBWay Offer) it work fine. 

This PCB was offered from PCBWay. 

I have been using PCBWay for many years now. PCBWay fabricate PCBs at low cost, fast processing time for only 24 hours, and fast delivery time using any carrier options. This double side 10cmx10cm can be fabricate at only 5USD for 5 to 10pcs by PCBWay. It's a standard PCB with silk screen and solder mask.

A DIY dsPIC30F2010 and dsPIC30F1010 Prototype Board with Programmer
10 PCBs for only 5USD
 

For different size of PCB we can instantly quote on PCBWay website using a zip PCB Gerber file without account.

A DIY dsPIC30F2010 and dsPIC30F1010 Prototype Board with Programmer
PCBWay Instant Quote

 

No comments:

Post a Comment

320x50

Search This Blog

tyro-728x90