728x90

728x90

Wednesday, February 4, 2026

ATMega644P TWI MCP23017 LCD Keypad

The MCP23017 can be use as input or output depends on its register configurations. A 4x4 matrix keypad requires only one 8-bit I/O port of its master controller. So I use one general I/O port the GPA of the MCP23017 to scan a 4x4 matrix keypad. Founded key will show on a16x2 character LCD.

ATMega644P TWI MCP23017 LCD Keypad

The LCD is already place on board. The MCP23017 interfaces to the ATMega644P using only two TWI wires. But it need to place and wire on a breadboard module with the keypad.

ATMega644P TWI MCP23017 LCD Keypad 

I use a remembrance 4x4 keypad module that's ready to connect to GPA.

  • "main.c"
  1. /*
  2. * 10-i2c_mcp23017_keypad.c
  3. *
  4. * Created: 2/5/2026 9:05:21 AM
  5. * Author : Admin
  6. */

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

  10. const char MCP23017_W=0x40;
  11. const char MCP23017_R=0x41;

  12. //IOCON.BANK=0
  13. enum BANK0{
  14. IODIRA=0,IODIRB,IPOLA,IPOLB,GPINTENA,GPINTENB,DEFVALA,
  15. DEFVALB,INTCONA,INTCONB,IOCON1,IOCON2,GPPUA,GPPUB,
  16. INTFA,INTFB,INTCAPA,INTCAPB,GPIOA,GPIOB,OLATA,OLATB
  17. };


  18. void mcp23017_write(char address,char data){
  19. twi_start();
  20. /*Select the write address*/
  21. twi_write(MCP23017_W);
  22. /*Select a register address*/
  23. twi_write(address);
  24. /*Send configuration data*/
  25. twi_write(data);
  26. twi_stop();
  27. }

  28. unsigned char mcp23017_read(char address){
  29. /*Select a specific address*/
  30. twi_start();
  31. twi_write(MCP23017_W);
  32. twi_write(address);
  33. twi_stop();
  34. /*Read data from the given address*/
  35. twi_start();
  36. twi_write(MCP23017_R);
  37. unsigned char i2cData=twi_read(1);
  38. twi_stop();
  39. return i2cData;
  40. }

  41. const char key_16[4][4]={'1','2','3','A',
  42. '4','5','6','B',
  43. '7','8','9','C',
  44. '*','0','#','D'};

  45. char keyScan(void){
  46. char data=0,temp,key;
  47. for(char i=0;i<4;i++){
  48. data=0xFF;
  49. data&=~(1<<i);
  50. mcp23017_write(OLATA,data);
  51. _delay_ms(5);
  52. data=mcp23017_read(GPIOA);
  53. data&=0xF0;
  54. if((data&0x10)==0) {temp=key_16[i][0]; break;}
  55. else if((data&0x20)==0){temp=key_16[i][1]; break;}
  56. else if((data&0x40)==0){temp=key_16[i][2]; break;}
  57. else if((data&0x80)==0){temp=key_16[i][3]; break;}
  58. else temp=0;
  59. _delay_ms(10);
  60. }
  61. return temp;
  62. }

  63. int main(void)
  64. {
  65. /* Replace with your application code */
  66. lcd_init();
  67. twi_init();
  68. lcd_text("ATMEGA644P TWI");
  69. lcd_xy(1,2);
  70. lcd_text("MCP23017 KeyPad");
  71. _delay_ms(5000);
  72. lcd_clear();
  73. //Key Pad Init
  74. mcp23017_write(IODIRA,0xF0);
  75. mcp23017_write(GPPUA,0xF0);
  76. char temp,charCount=0,newLine=0,line=1;
  77. while (1)
  78. {
  79. temp=keyScan();
  80. if(temp!=0){
  81. lcd_data(temp);
  82. charCount++;
  83. _delay_ms(500);
  84. }
  85. if(charCount>16){
  86. newLine=1;
  87. charCount=0;
  88. line+=1;
  89. }
  90. if(newLine){
  91. newLine=0;
  92. if(line==2) lcd_xy(1,2);
  93. else{
  94. lcd_xy(1,1);
  95. lcd_command(0x01);
  96. _delay_ms(5);
  97. line=1;
  98. }
  99. }
  100. }
  101. }


We need to add the "twi.c" to project before the twi function can be called. 

  • "twi.c"
  1. /*
  2. * twi.c
  3. *
  4. * Created: 2/4/2026 10:22:02 AM
  5. * Author: Admin
  6. */

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


  10. void twi_init(void){
  11. TWSR|=0x00; //Prescaler Selection Bit
  12. TWBR=0x0F; //Baud Rate Generator
  13. TWCR=(1<<TWEN); //Enable The TWI Module
  14. PORTC|=(1<<0);
  15. PORTC|=(1<<1);
  16. }

  17. void twi_start(void){
  18. TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
  19. while((TWCR&(1<<TWINT))==0);
  20. }

  21. void twi_write(unsigned char data){
  22. TWDR=data;
  23. TWCR=(1<<TWINT)|(1<<TWEN);
  24. while((TWCR&(1<<TWINT))==0);
  25. }

  26. unsigned char twi_read(char ACK){
  27. if(ACK==0)
  28. TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWEA);
  29. else
  30. TWCR=(1<<TWINT)|(1<<TWEN);
  31. while((TWCR&(1<<TWINT))==0);
  32. return TWDR;
  33. }

  34. void twi_stop(){
  35. TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
  36. _delay_us(10);
  37. }

 

  •  Schematic

 

ATMega644P TWI MCP23017 LCD Keypad
Proteus Circuit and Simulation
ATMega644P TWI MCP23017 LCD Keypad
Keypad Scanning
  •  ATMega644P AVR Board Experiment
 
 
ATMega644P TWI MCP23017 LCD Keypad
AVR Prototype Board Experiment
 
ATMega644P TWI MCP23017 LCD Keypad
AVR Prototype Board Experiment
 

This PCB was offered by PCBWay (pcbway.com).

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
 

There's on port left GPB. So I added one more keypad port.

ATMega644P TWI MCP23017 LCD Keypad
Two Keypad Ports

 Source Code:

  1. /*
  2. * 10-i2c_mcp23017_keypad_2.c
  3. *
  4. * Created: 2/5/2026 3:14:18 PM
  5. * Author : Admin
  6. */

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

  10. const char MCP23017_W=0x40;
  11. const char MCP23017_R=0x41;

  12. //IOCON.BANK=0
  13. enum BANK0{
  14. IODIRA=0,IODIRB,IPOLA,IPOLB,GPINTENA,GPINTENB,DEFVALA,
  15. DEFVALB,INTCONA,INTCONB,IOCON1,IOCON2,GPPUA,GPPUB,
  16. INTFA,INTFB,INTCAPA,INTCAPB,GPIOA,GPIOB,OLATA,OLATB
  17. };


  18. void mcp23017_write(char address,char data){
  19. twi_start();
  20. /*Select the write address*/
  21. twi_write(MCP23017_W);
  22. /*Select a register address*/
  23. twi_write(address);
  24. /*Send configuration data*/
  25. twi_write(data);
  26. twi_stop();
  27. }

  28. unsigned char mcp23017_read(char address){
  29. /*Select a specific address*/
  30. twi_start();
  31. twi_write(MCP23017_W);
  32. twi_write(address);
  33. twi_stop();
  34. /*Read data from the given address*/
  35. twi_start();
  36. twi_write(MCP23017_R);
  37. unsigned char i2cData=twi_read(1);
  38. twi_stop();
  39. return i2cData;
  40. }

  41. //KeyPad at GPA
  42. const char key_1[4][4]={'1','2','3','A',
  43. '4','5','6','B',
  44. '7','8','9','C',
  45. '*','0','#','D'};

  46. //KeyPad at GPB
  47. const char key_2[4][4]={'E','F','G','H',
  48. 'I','J','K','L',
  49. 'M','N','O','P',
  50. 'Q','R','S','T'};
  51. //KeyPad at GPA
  52. char keyScan_1(void){
  53. char data=0,temp,key;
  54. for(char i=0;i<4;i++){
  55. data=0xFF;
  56. data&=~(1<<i);
  57. mcp23017_write(OLATA,data);
  58. _delay_ms(5);
  59. data=mcp23017_read(GPIOA);
  60. data&=0xF0;
  61. if((data&0x10)==0) {temp=key_1[i][0]; break;}
  62. else if((data&0x20)==0){temp=key_1[i][1]; break;}
  63. else if((data&0x40)==0){temp=key_1[i][2]; break;}
  64. else if((data&0x80)==0){temp=key_1[i][3]; break;}
  65. else temp=0;
  66. _delay_ms(10);
  67. }
  68. return temp;
  69. }
  70. //KeyPad at GPB
  71. char keyScan_2(void){
  72. char data=0,temp,key;
  73. for(char i=0;i<4;i++){
  74. data=0xFF;
  75. data&=~(1<<i);
  76. mcp23017_write(OLATB,data);
  77. _delay_ms(5);
  78. data=mcp23017_read(GPIOB);
  79. data&=0xF0;
  80. if((data&0x10)==0) {temp=key_2[i][0]; break;}
  81. else if((data&0x20)==0){temp=key_2[i][1]; break;}
  82. else if((data&0x40)==0){temp=key_2[i][2]; break;}
  83. else if((data&0x80)==0){temp=key_2[i][3]; break;}
  84. else temp=0;
  85. _delay_ms(10);
  86. }
  87. return temp;
  88. }

  89. int main(void)
  90. {
  91. /* Replace with your application code */
  92. lcd_init();
  93. twi_init();
  94. lcd_text("ATMEGA644P TWI");
  95. lcd_xy(1,2);
  96. lcd_text("MCP23017 KeyPad");
  97. _delay_ms(5000);
  98. lcd_clear();
  99. //Key Pad Init
  100. mcp23017_write(IODIRA,0xF0);
  101. mcp23017_write(GPPUA,0xF0);
  102. mcp23017_write(IODIRB,0xF0);
  103. mcp23017_write(GPPUB,0xF0);
  104. char keyPad_1,keyPad_2,charCount=0,newLine=0,line=1;
  105. while (1)
  106. {
  107. keyPad_1=keyScan_1();
  108. if(keyPad_1!=0){
  109. lcd_data(keyPad_1);
  110. charCount++;
  111. _delay_ms(500);
  112. }
  113. keyPad_2=keyScan_2();
  114. if(keyPad_2!=0){
  115. lcd_data(keyPad_2);
  116. charCount++;
  117. _delay_ms(500);
  118. }
  119. if(charCount>16){
  120. newLine=1;
  121. charCount=0;
  122. line+=1;
  123. }
  124. if(newLine){
  125. newLine=0;
  126. if(line==2) lcd_xy(1,2);
  127. else{
  128. lcd_xy(1,1);
  129. lcd_command(0x01);
  130. _delay_ms(5);
  131. line=1;
  132. }
  133. }
  134. }
  135. }


Schematic:

ATMega644P TWI MCP23017 LCD Keypad
Schematic

 AVR Prototype Board:

ATMega644P TWI MCP23017 LCD Keypad
AVR Prototype Board
ATMega644P TWI MCP23017 LCD Keypad
AVR Prototype Board Experiment

 


 

No comments:

Post a Comment

320x50

Search This Blog

tyro-728x90