728x90

728x90

Monday, February 2, 2026

ATMega644P TWI PCF8574AP Matrix Keypad LCD

A matrix keypad could save the number of micro-controller I/O usage. For instant a 4x4 matrix keypad uses only 8 pins of a micro-controller but it yield 16 different key values. Using an 8-bit micro-controller digital I/O port is common for most of programmers. 

ATMega644P TWI PCF8574AP Matrix Keypad LCD

However when the there is a I/O constraint we need an I/O expansion, for example adding serial interface I/O expansion chip to a micro-controller.These chips need only a few wires of a micro-controller.

ATMega644P TWI PCF8574AP Matrix Keypad LCD
16 Key Membrane Switch Keypad 4X4 3X4 Matrix Keyboard For Arduino Diy Kit

 

In this post I will show an example of using the PCF8574AP to make a 4x4 matrix keypad with a character LCD. For more information about using the PCF8574 series please visit these posts,

  1. ATMega644P TWI PCF8574 I/O Expansion Interfacing 
  2. PIC16F887 PCF8574AP I2C LCD Example using XC8 
  3. PIC16F887 PCF8574AP I2C 4x4 KeyPad using XC8 
  4. PIC16F887 PCF8574 I2C Example using XC8 
  5. A DIY TWI LCD Using PCF8574AP For Arduino 
  6. ATMega32 TWI interfaces to PCF8574A parallel port expansion 
  7. Reading The Digital Input Output From PCF8574 Using PIC12F629 

The TWI master micro-controller of the ATMega644P periodically scan for any key press. The result of will display on an on-board 16x2 character LCD.

Source Code:

  1. /*
  2. * 10-i2c_pcf8574ap_keypad.c
  3. *
  4. * Created: 2/2/2026 9:32:41 PM
  5. * Author : Admin
  6. */

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

  10. //DIY PCF8574AP LCD Module (A2:A0=0)
  11. const char pcf8574_w=0x40;
  12. const char pcf8574_r=0x41;
  13. //PCF8574 (A2:A0=0)
  14. const char pcf8574a_w=0x70;
  15. const char pcf8574a_r=0x71;
  16. //DIY Arduino PCF8574T LCD Module
  17. const char pcf8574T_LCD_W=0x4E;
  18. const char pcf8574T_LCD_R=0x4F;

  19. void i2cInit(void){
  20. TWSR|=0x00; //Prescaler Selection Bit
  21. TWBR=0xF0; //Baud Rate Generator
  22. TWCR=(1<<TWEN); //Enable The TWI Module
  23. PORTC|=(1<<0);
  24. PORTC|=(1<<1);
  25. }

  26. void i2cStart(void){
  27. TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
  28. while((TWCR&(1<<TWINT))==0);
  29. }

  30. void i2cWrite(unsigned char data){
  31. TWDR=data;
  32. TWCR=(1<<TWINT)|(1<<TWEN);
  33. while((TWCR&(1<<TWINT))==0);
  34. }

  35. unsigned char i2cRead(char ACK){
  36. if(ACK==0)
  37. TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWEA);
  38. else
  39. TWCR=(1<<TWINT)|(1<<TWEN);
  40. while((TWCR&(1<<TWINT))==0);
  41. return TWDR;
  42. }

  43. void i2cStop(){
  44. TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
  45. _delay_us(10);
  46. }

  47. void pcf8574Write(char data){
  48. i2cStart();
  49. i2cWrite(pcf8574a_w);
  50. i2cWrite(data);
  51. i2cStop();
  52. }

  53. char pcf8574Read(void){
  54. i2cStart();
  55. i2cWrite(pcf8574a_r);
  56. char temp=i2cRead(1);
  57. i2cStop();
  58. return temp;
  59. }

  60. uint8_t key_16[][4]={'1','2','3','A',
  61. '4','5','6','B',
  62. '7','8','9','C',
  63. '*','0','#','D'};

  64. uint8_t keyScan(void){
  65. char data=0,temp,key;
  66. for(uint8_t i=0;i<4;i++){
  67. data=0xFF;
  68. data&=~(1<<i);
  69. pcf8574Write(data);
  70. _delay_ms(5);
  71. data=pcf8574Read();
  72. data&=0xF0;
  73. if((data&0x10)==0) {temp=key_16[i][0]; break;}
  74. else if((data&0x20)==0){temp=key_16[i][1]; break;}
  75. else if((data&0x40)==0){temp=key_16[i][2]; break;}
  76. else if((data&0x80)==0){temp=key_16[i][3]; break;}
  77. else temp=0;
  78. _delay_ms(10);
  79. }
  80. return temp;
  81. }

  82. int main(void)
  83. {
  84. /* Replace with your application code */
  85. i2cInit();
  86. lcd_init();
  87. lcd_text("ATMega644P TWI");
  88. lcd_xy(1,2);
  89. lcd_text("PCF8574AP KeyPad");
  90. _delay_ms(10000);
  91. lcd_clear();
  92. char temp,charCount=0,newLine=0,line=1;
  93. while (1)
  94. {
  95. temp=keyScan();
  96. if(temp!=0){
  97. lcd_data(temp);
  98. charCount++;
  99. _delay_ms(500);
  100. }
  101. if(charCount>16){
  102. newLine=1;
  103. charCount=0;
  104. line+=1;
  105. }
  106. if(newLine){
  107. newLine=0;
  108. if(line==2) lcd_xy(1,2);
  109. else{
  110. lcd_xy(1,1);
  111. lcd_command(0x01);
  112. _delay_ms(5);
  113. line=1;
  114. }
  115. }
  116. }
  117. }



Schematic:

ATMega644P TWI PCF8574AP Matrix Keypad LCD
Schematic and Simulation
Simulation Program

 

ATMega644P Experiment Board:

ATMega644P TWI PCF8574AP Matrix Keypad LCD


ATMega644P TWI PCF8574AP Matrix Keypad LCD


This PCB was offered by 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