728x90

728x90

Friday, February 13, 2026

ATMega644P Software TWI SH1106 OLED and DS1307 RTC

In previous example the ATMega644P emulate the TWI via software bit-banging that able to communicate to various slave TWI devices on a single bus. In this example the ATMega644P master MCU send display data to the SH1106 128x64 OLED display and read calendar data from the DS1307 RTC. For more information about using the SH1106 128x64 OLED display please visit this post.

ATMega644P Software TWI SH1106 OLED and DS1307 RTC
ATMega644P Software TWI SH1106 OLED 

 

ATMega644P Software TWI SH1106 OLED 

For an introductory example the master MCU send display data to the SH1106 128x64 OLED module. 

Source Code "main.c":

  1. /*
  2. * 11-soft_twi_sh1106.c
  3. *
  4. * Created: 2/10/2026 12:03:22 AM
  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 ATMega644P");
  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,"Software TWI");
  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. }

Schematic:

Schematic
Schematic

Proteus only has a model for the SSD1306. Simulating this part is very slow.

AVR Prototype Board:

ATMega644P Software TWI SH1106 OLED and DS1307 RTC
ATMega644P AVR Prototype Board

 

ATMega644P Software TWI SH1106 OLED and DS1307 RTC 

 

ATMega644P Software TWI SH1106 OLED and DS1307 RTC
ATMega644P AVR Prototype Board

 

Click here to download this example. 

ATMega644P Software TWI SH1106 OLED 

Here the ATMega644P read the calendar from DS1307 RTC that will display on the SH1106 128x64 OLED display.

Source Code "main.c":

  1. /*
  2. * 11-soft_twi_ds1307_sh1106.c
  3. *
  4. * Created: 2/9/2026 7:34:19 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. #include "twi_device.h"

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

  27. unsigned char rtc[50], msg[20];
  28. float voltage;
  29. unsigned int temp;

  30. void rtc_read(void){
  31. for(char i=0;i<50;i++){
  32. /*Second Register*/
  33. twi_start();
  34. twi_write(DS1307_W);
  35. /*Select Second register*/
  36. twi_write(i);
  37. twi_stop();
  38. _delay_us(100);
  39. twi_start();
  40. twi_write(DS1307_R);
  41. rtc[i]=twi_read();
  42. twi_stop();
  43. _delay_us(100);
  44. }
  45. }
  46. int main(void)
  47. {
  48. /* Replace with your application code */
  49. _delay_ms(1000);
  50. display_init();
  51. adc_init();
  52. //rtc_init();
  53. display_clear(0);
  54. display_text_8x16(0,0,"Software TWI");
  55. display_text_8x16(0,1,"ATMega644P OLED");
  56. display_text_8x16(0,2,"SH1106 DS1307");
  57. display_text_8x16(0,3,"RTC Example");
  58. _delay_ms(10000);
  59. display_clear(0);
  60. while (1)
  61. {
  62. rtc_read();
  63. sprintf(msg,"Time: %02X:%02X:%02X",rtc[2],rtc[1],rtc[0]);
  64. display_text_8x16(0,0,msg);
  65. sprintf(msg,"Date: %02X/%02X/20%02X",rtc[4],rtc[5],rtc[6]);
  66. display_text_8x16(0,1,msg);
  67. //Read POT
  68. temp=read_adc(0);
  69. //Convert To Voltage
  70. voltage=temp*5.0/1023;
  71. sprintf(msg,"ADC0: %4d %.2fV",temp,voltage);
  72. display_text_8x16(0,2,msg);
  73. //Read LM35
  74. temp=read_adc(1);
  75. voltage=temp*5.0/1023;
  76. voltage*=100;
  77. sprintf(msg,"Temp: %.2f C",voltage);
  78. display_text_8x16(0,3,msg);
  79. _delay_us(500);
  80. }
  81. }


The schematic is the one's above.

 

AVR Experiment Board: 

ATMega644P Software TWI SH1106 OLED and DS1307 RTC
ATMega644P Software TWI SH1106 OLED 
ATMega644P Software TWI SH1106 OLED and DS1307 RTC
ATMega644P Software TWI SH1106 OLED

 Click here to download this example.

 

 

No comments:

Post a Comment

320x50

Search This Blog

tyro-728x90