728x90

728x90

Sunday, February 8, 2026

ATMega32 TWI and SH1106 128x64 OLED Example

The SH1106 is a 132 X 64 Dot Matrix OLED/PLED Segment/Common Driver with Controller manufactured by SINO WEALTH. This chip is applicable for many low cost display. A low cost monochrome OLED display for Arduino is very popular among student and electronic hobbyists. It cost around 2USD. This chip has many communication interface depend on the display manufacturers, 8-bit 6800-series parallel interface, 8-bit 8080-series parallel interface, 3-wire & 4-wire serial peripheral interface, 400KHz fast I2C bus interface. However an SPI or an I2C OLED display module is common due complexity of wiring. An SPI display yield a high speed data transmission and reception compares to the I2C communication interface.

ATMega32 TWI and SH1106 128x64 OLED Example 

 

 ATMega32 TWI and SH1106 128x64 OLED Example

This display is easy to control via an 8-bit micro-controller especially the Arduino. The SSD1306 is its equivalent display controller manufactured by solomon-systech. It contains command and data similar to the old KS0108 display controller.

ATMega32 TWI and SH1106 128x64 OLED Example 


I bought a cheap SH1106 1.3" OLED display from a local electronics component store. It works fine at lower I2C frequency around 100kHz. At higher data rate it become halted. I already tested it with Arduino and PIC16F887 micro-controller. For more detail about configuration and display data sending to this display module please visit this post.

In this example I use my ATMega32 to display text on the SH1106 display. At this time I have problem with the ATMega644P TWI. So I use this smaller memory micro-controller.

Source Code:

  1. /*
  2. * 10-twi_sh1106.c
  3. *
  4. * Created: 2/7/2026 5:21:04 PM
  5. * Author : Admin
  6. */

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

  10. void show_text(void){
  11. display_xy(0,0);
  12. display_text("ATMega32 SH1106");
  13. display_xy(0,1);
  14. display_text("TWI Programming");
  15. display_xy(0,2);
  16. display_text("Microchip Studio");
  17. display_xy(0,3);
  18. display_text("v.7.0.2542");
  19. display_xy(0,4);
  20. display_text("1.3 I2C OLED ");
  21. display_xy(0,5);
  22. display_text("Display Module");
  23. display_xy(0,6);
  24. display_text("AKI-Technical");
  25. display_xy(0,7);
  26. display_text("2026/02/08 Sun");
  27. }

  28. int main(void)
  29. {
  30. /* Replace with your application code */
  31. _delay_ms(1000);
  32. display_init();
  33. display_clear(0);
  34. show_text();
  35. _delay_ms(5000);
  36. display_clear(0);
  37. while (1)
  38. {
  39. display_clear(0);
  40. show_text();
  41. _delay_ms(2500);
  42. display_clear(0);
  43. _delay_ms(2500);
  44. display_clear(0xFF);
  45. _delay_ms(2500);
  46. display_clear(0);
  47. show_text();
  48. _delay_ms(2500);
  49. display_clear(0xAA);
  50. _delay_ms(2500);
  51. }
  52. }


I place its twi.c and oled.c libraries in different files. Click here to download its source file.

ATMega32 TWI and SH1106 128x64 OLED Example
Schematic and Simulation

 The ATMega32 run very well at 16MHz and high SCL frequency.

ATMega32 TWI and SH1106 128x64 OLED Example
ATMega32 AVR Prototype Board

 

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

 Now I create a bigger fonts function to make displaying text more visible.

  1. void display_char_8x16(unsigned char x, unsigned char y, char ch){
  2. y=y*2;
  3. display_xy(x,y);
  4. uint16_t temp;
  5. ch=ch-32;
  6. temp=ch*16;
  7. char i=0;
  8. for(i=0;i<8;i++) display_data(font_8x16[temp+i]);
  9. display_xy(x,y+1);
  10. for(i=8;i<16;i++) display_data(font_8x16[temp+i]);
  11. }

  12. void display_text_8x16(unsigned char x, unsigned char y, char *txt){
  13. while(*txt){ display_char_8x16(x,y,*txt++); x=x+8;}
  14. }

This example the SH1106 will display ASCII characters and numbers.

Source Code: 

 

  1. /*
  2. * 10-i2c_sh1106_2.c
  3. *
  4. * Created: 2/8/2026 7:59:35 PM
  5. * Author : Admin
  6. */

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

  10. int main(void)
  11. {
  12. /* Replace with your application code */
  13. _delay_ms(1000);
  14. display_init();
  15. display_clear(0);
  16. display_text_8x16(0,0,"SH1106 I2C OLED");
  17. display_text_8x16(0,1,"And ATMega32");
  18. display_text_8x16(0,2,"Programming With");
  19. display_text_8x16(0,3,"Microchip Studio");
  20. _delay_ms(10000);
  21. display_clear(0x00);
  22. while (1)
  23. {
  24. display_text_8x16(0,0,"ATMega32 SH1106");
  25. _delay_ms(2500);
  26. uint8_t h=15;
  27. for(uint8_t i=0;i<10;i++) {
  28. display_char_8x16(h,1,'0'+i);
  29. h+=10;
  30. }
  31. _delay_ms(2500);
  32. h=0;
  33. for(uint8_t i=0;i<14;i++) {
  34. display_char_8x16(h,2,'A'+i);
  35. h+=10;
  36. }
  37. _delay_ms(2500);
  38. h=0;
  39. for(uint8_t i=0;i<14;i++) {
  40. display_char_8x16(h,3,'N'+i);
  41. h+=10;
  42. }
  43. _delay_ms(10000);
  44. display_clear(255);
  45. _delay_ms(10000);
  46. display_clear(0);
  47. _delay_ms(1000);
  48. }
  49. }


Click here to download this example. 

ATMega32 TWI and SH1106 128x64 OLED Example
Running Program on ATMega32 Prototype Board

The schematic is the same to previous example. 

There two on-board TWI devices, a ds1307 and a AT24L16B. So the MCU will read the RTC data from ds1307 and display it on the SH1106 OLED. Two ADC inputs, ADC0(PA0) connects to a POT, and ADC1(PA1) connects to the LM35 temperature sensor. However the LM35 was a fake TO-92 chip. Time to time it burned out due to heat dissipation.

ATMega32 TWI and SH1106 128x64 OLED Example 

Proteus has only a model for the SSD1306 that almost equivalent to the SH1106. However its simulation speed is very low compare to the parallel port base graphical LCD. But running this program using a real MCU and OLED it operate very well due to the high speed MCU  and SCL clock frequency.

To add any floating point value to a string we need to configure some parameters in the Microchip Studio IDE before compiling.

ATMega32 TWI and SH1106 128x64 OLED Example

ATMega32 TWI and SH1106 128x64 OLED Example 

ATMega32 TWI and SH1106 128x64 OLED Example 

The linker flag is  -lprintf_flt . Click save and close it before building the project.

Source Code:

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

  7. #define F_CPU 16000000UL

  8. #include <avr/io.h>
  9. #include <util/delay.h>

  10. void twi_init(void){
  11. TWSR|=0x01; //Prescaler Selection Bit
  12. TWBR=0x05; //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. }

I tested this firmware using the AMTega32 on my AVR Prototype Board. However it works on the ATMega16 or the ATMega644P with a proper software configurations. 

ATMega32 TWI and SH1106 128x64 OLED Example 


ATMega32 TWI and SH1106 128x64 OLED Example

 

Click here to download its source file




 

 


Saturday, February 7, 2026

ATMega644P TWI DS1307 MCP23017 LCD

In previous posts the ATMega644P TWI interface with the DS13017 RTC and MCP23017 GPIO expansion chip. Here I will put them altogethers. This TWI bus will drives the DS1307 RTC and the MCP23017 that controls a character LCD.

ATMega644P TWI DS1307 MCP23017 LCD 

The master microcontroller read the RTC data from ds1307 and it will send to the MCP23017 based LCD. 

Source Code:

  1. /*
  2. * 10-i2c_ds1307_mcp23017_lcd.c
  3. *
  4. * Created: 2/7/2026 7:42:39 PM
  5. * Author : Admin
  6. */

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

  10. void rtc_init(void){
  11. char rtc[8]={0x30,0x00,0x17,0x07,0x31,0x01,0x26,1<<4};
  12. for (char i=0;i<8;i++)
  13. {
  14. twi_start();
  15. //D0 is DS1307 Write Address
  16. twi_write(0xD0);
  17. //Select Control Register
  18. twi_write(i);
  19. //Enable SQWE bit blinks at 1 Hz
  20. twi_write(rtc[i]);
  21. twi_stop();
  22. _delay_ms(10);
  23. }
  24. }

  25. char rtc[6], msg[16];
  26. int main(void)
  27. {
  28. /* Replace with your application code */
  29. _delay_ms(1000);
  30. lcd_init();
  31. lcd_text("ATMega644P TWI");
  32. lcd_xy(1,2);
  33. lcd_text("MCP23017 DS1307");
  34. _delay_ms(10000);
  35. lcd_clear();
  36. lcd_command(0x0C);
  37. while (1)
  38. {
  39. for(char i=0;i<7;i++){
  40. /*Second Register*/
  41. twi_start();
  42. twi_write(0xD0);
  43. /*Select Second register*/
  44. twi_write(i);
  45. twi_stop();
  46. _delay_us(10);
  47. twi_start();
  48. twi_write(0xD1);
  49. rtc[i]=twi_read(1);
  50. twi_stop();
  51. _delay_us(10);
  52. }
  53. lcd_xy(1,1);
  54. sprintf(msg,"Time: %02X:%02X:%02X",rtc[2],rtc[1],rtc[0]);
  55. lcd_text(msg);
  56. lcd_xy(1,2);
  57. sprintf(msg,"Date: %02X/%02X/20%02X",rtc[4],rtc[5],rtc[6]);
  58. lcd_text(msg);
  59. _delay_ms(500);
  60. }
  61. }


Circuit Simulation:

ATMega644P TWI DS1307 MCP23017 LCD
Circuit and Simulation

 

AVR Prototype Board

ATMega644P TWI DS1307 MCP23017 LCD

 

ATMega644P TWI DS1307 MCP23017 LCD 

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

 

Click here to download this example

For a full ATMega644P tutorials please visit this page.

Thursday, February 5, 2026

ATMega644P TWI MCP23017 Character LCD

The HD44780 based character LCD can be controlled by a micro-controller, digital circuit, an I/O expansion chip or event by hands. In this example I use an output port of the MCP23017 to control a 16x2 LCD in one direction (write only) in 4-bit data transfer mode.

ATMega644P TWI MCP23017 Character LCD

The LCD connect to GPIOB (output register OLATB). That's,

  • LCD RS (GPB0)
  • LCD R/W (GPB1)
  • LCD EN(GPB2)
  • D4:D7(GPB4:GPB7)

Source Code: "main.c"

  1. /*
  2. * 10-i2c_mcp23017_1602.c
  3. *
  4. * Created: 2/5/2026 8:03:45 PM
  5. * Author : Admin
  6. */

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

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

  17. const char RS=0,RW=1,EN=2;
  18. char BLK_ON=0;

  19. void lcd_command(uint8_t temp){
  20. uint8_t command,led;
  21. command=temp&0xF0;
  22. if(BLK_ON==1) led=0x08;
  23. else led=0;
  24. mcp23017_write(OLATB,command|led|(1<<EN));
  25. _delay_us(10);
  26. mcp23017_write(OLATB,command|led);
  27. _delay_us(25);
  28. command=temp<<4;
  29. mcp23017_write(OLATB,command|led|(1<<EN));
  30. _delay_us(10);
  31. mcp23017_write(OLATB,command|led);
  32. _delay_us(25);
  33. }

  34. void lcd_data(uint8_t temp){
  35. uint8_t data,led;
  36. data=temp&0xF0;
  37. if(BLK_ON==1) led=0x08;
  38. else led=0;
  39. mcp23017_write(OLATB,data|led|(1<<EN)|(1<<RS));
  40. _delay_us(10);
  41. mcp23017_write(OLATB,data|led|(1<<RS));
  42. _delay_us(25);
  43. data=temp<<4;
  44. mcp23017_write(OLATB,data|led|(1<<EN)|(1<<RS));
  45. _delay_us(10);
  46. mcp23017_write(OLATB,data|led|(1<<RS));
  47. _delay_us(25);
  48. }

  49. void lcd_xy(uint8_t x, uint8_t y){
  50. uint8_t cursor[]={0x80,0xC0};
  51. lcd_command(cursor[y-1]+x-1);
  52. }

  53. void lcd_text(uint8_t *text){
  54. while(*text) lcd_data(*text++);
  55. }

  56. void lcd_clear(void){
  57. lcd_command(0x01);
  58. _delay_ms(5);
  59. }

  60. void lcd_init(void){
  61. BLK_ON=1;
  62. mcp23017_write(IODIRB,0x00);
  63. lcd_command(0x33);
  64. _delay_us(10);
  65. lcd_command(0x32);
  66. _delay_us(10);
  67. lcd_command(0x28);
  68. _delay_us(10);
  69. lcd_command(0x0F);
  70. _delay_us(10);
  71. lcd_command(0x01);
  72. _delay_ms(5);
  73. lcd_command(0x06);
  74. _delay_us(10);
  75. }

  76. int main(void)
  77. {
  78. /* Replace with your application code */
  79. lcd_init();
  80. lcd_text("MCP23017 TWI");
  81. lcd_xy(1,2);
  82. lcd_text("MCP23017 LCD");

  83. _delay_ms(10000);
  84. lcd_clear();
  85. lcd_command(0x0C);
  86. lcd_text("Counter Value:");
  87. long counter=0;
  88. char msg[10];
  89. while (1)
  90. {
  91. sprintf(msg,"%ld",counter);
  92. lcd_xy(1,2); lcd_text(msg);
  93. counter++;
  94. _delay_ms(500);
  95. }
  96. }

Source Code: "mcp23017.c"

  1. /*
  2. * mcp23017.c
  3. *
  4. * Created: 2/5/2026 8:06:38 PM
  5. * Author: Admin
  6. */

  7. const char MCP23017_W=0x40;
  8. const char MCP23017_R=0x41;

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


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

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

Source Code: "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 Character LCD

 

AVR Prototype Board: 

ATMega644P TWI MCP23017 Character LCD
AVR Experiment Board

For PIC micro-controller using PIC16F887 is a good choice.

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 is one port left, GPIOA. So I added a 4x4 matrix keypad at GPIOA. The result of keypad scanning will show on the LCD.

Source Code:

 

  1. /*
  2. * 10-i2c_mcp23017_1602_kb.c
  3. *
  4. * Created: 2/5/2026 10:32:40 PM
  5. * Author : Admin
  6. */

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

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

  17. const char RS=0,RW=1,EN=2;
  18. char BLK_ON=0;

  19. const char key_16[4][4]={'1','2','3','A',
  20. '4','5','6','B',
  21. '7','8','9','C',
  22. '*','0','#','D'};

  23. char keyScan(void){
  24. char data=0,temp,key;
  25. for(char i=0;i<4;i++){
  26. data=0xFF;
  27. data&=~(1<<i);
  28. mcp23017_write(OLATA,data);
  29. _delay_ms(5);
  30. data=mcp23017_read(GPIOA);
  31. data&=0xF0;
  32. if((data&0x10)==0) {temp=key_16[i][0]; break;}
  33. else if((data&0x20)==0){temp=key_16[i][1]; break;}
  34. else if((data&0x40)==0){temp=key_16[i][2]; break;}
  35. else if((data&0x80)==0){temp=key_16[i][3]; break;}
  36. else temp=0;
  37. _delay_ms(10);
  38. }
  39. return temp;
  40. }
  41. void lcd_command(uint8_t temp){
  42. uint8_t command,led;
  43. command=temp&0xF0;
  44. if(BLK_ON==1) led=0x08;
  45. else led=0;
  46. mcp23017_write(OLATB,command|led|(1<<EN));
  47. _delay_us(10);
  48. mcp23017_write(OLATB,command|led);
  49. _delay_us(25);
  50. command=temp<<4;
  51. mcp23017_write(OLATB,command|led|(1<<EN));
  52. _delay_us(10);
  53. mcp23017_write(OLATB,command|led);
  54. _delay_us(25);
  55. }

  56. void lcd_data(uint8_t temp){
  57. uint8_t data,led;
  58. data=temp&0xF0;
  59. if(BLK_ON==1) led=0x08;
  60. else led=0;
  61. mcp23017_write(OLATB,data|led|(1<<EN)|(1<<RS));
  62. _delay_us(10);
  63. mcp23017_write(OLATB,data|led|(1<<RS));
  64. _delay_us(25);
  65. data=temp<<4;
  66. mcp23017_write(OLATB,data|led|(1<<EN)|(1<<RS));
  67. _delay_us(10);
  68. mcp23017_write(OLATB,data|led|(1<<RS));
  69. _delay_us(25);
  70. }

  71. void lcd_xy(uint8_t x, uint8_t y){
  72. uint8_t cursor[]={0x80,0xC0};
  73. lcd_command(cursor[y-1]+x-1);
  74. }

  75. void lcd_text(uint8_t *text){
  76. while(*text) lcd_data(*text++);
  77. }

  78. void lcd_clear(void){
  79. lcd_command(0x01);
  80. _delay_ms(5);
  81. }

  82. void lcd_init(void){
  83. BLK_ON=1;
  84. mcp23017_write(IODIRB,0x00);
  85. lcd_command(0x33);
  86. _delay_us(10);
  87. lcd_command(0x32);
  88. _delay_us(10);
  89. lcd_command(0x28);
  90. _delay_us(10);
  91. lcd_command(0x0F);
  92. _delay_us(10);
  93. lcd_command(0x01);
  94. _delay_ms(5);
  95. lcd_command(0x06);
  96. _delay_us(10);
  97. }

  98. int main(void)
  99. {
  100. /* Replace with your application code */
  101. lcd_init();
  102. lcd_text("MCP23017 TWI");
  103. lcd_xy(1,2);
  104. lcd_text("LCD and Key Pad");

  105. _delay_ms(10000);
  106. lcd_clear();
  107. //Key Pad Init
  108. mcp23017_write(IODIRA,0xF0);
  109. mcp23017_write(GPPUA,0xF0);
  110. char temp,charCount=0,newLine=0,line=1;
  111. while (1)
  112. {
  113. temp=keyScan();
  114. if(temp!=0){
  115. lcd_data(temp);
  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. }



Program Simulation:

ATMega644P TWI MCP23017 Character LCD
Program Simulation

AVR Prototype Board: 

ATMega644P TWI MCP23017 Character LCD 

ATMega644P TWI MCP23017 Character LCD

 


 

Click here to download this example

For a full ATMega644P tutorials please visit this page. 

320x50

Search This Blog

tyro-728x90