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 |
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":
- /*
- * 11-soft_twi_sh1106.c
- *
- * Created: 2/10/2026 12:03:22 AM
- * Author : Admin
- */
- #include <avr/io.h>
- #include <util/delay.h>
- #define F_CPU 16000000UL
- int main(void)
- {
- /* Replace with your application code */
- _delay_ms(1000);
- display_init();
- display_clear(0);
- display_text_8x16(0,0,"SH1106 I2C OLED");
- display_text_8x16(0,1,"And ATMega644P");
- display_text_8x16(0,2,"Programming With");
- display_text_8x16(0,3,"Microchip Studio");
- _delay_ms(10000);
- display_clear(0x00);
- while (1)
- {
- display_text_8x16(0,0,"Software TWI");
- _delay_ms(2500);
- uint8_t h=15;
- for(uint8_t i=0;i<10;i++) {
- display_char_8x16(h,1,'0'+i);
- h+=10;
- }
- _delay_ms(2500);
- h=0;
- for(uint8_t i=0;i<14;i++) {
- display_char_8x16(h,2,'A'+i);
- h+=10;
- }
- _delay_ms(2500);
- h=0;
- for(uint8_t i=0;i<14;i++) {
- display_char_8x16(h,3,'N'+i);
- h+=10;
- }
- _delay_ms(10000);
- display_clear(255);
- _delay_ms(10000);
- display_clear(0);
- _delay_ms(1000);
- }
- }
Schematic:
| Schematic |
Proteus only has a model for the SSD1306. Simulating this part is very slow.
AVR Prototype Board:
| ATMega644P AVR Prototype Board |
| 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":
- /*
- * 11-soft_twi_ds1307_sh1106.c
- *
- * Created: 2/9/2026 7:34:19 PM
- * Author : Admin
- */
- #include<stdio.h>
- #include <avr/io.h>
- #include <util/delay.h>
- #define F_CPU 16000000UL
- #include "twi_device.h"
- void rtc_init(void){
- char rtc[8]={0x30,0x35,0x13,0x07,0x31,0x01,0x26,1<<4};
- for (char i=0;i<8;i++)
- {
- twi_start();
- //D0 is DS1307 Write Address
- twi_write(DS1307_W);
- //Select Control Register
- twi_write(i);
- //Enable SQWE bit blinks at 1 Hz
- twi_write(rtc[i]);
- twi_stop();
- _delay_ms(10);
- }
- }
- unsigned char rtc[50], msg[20];
- float voltage;
- unsigned int temp;
- void rtc_read(void){
- for(char i=0;i<50;i++){
- /*Second Register*/
- twi_start();
- twi_write(DS1307_W);
- /*Select Second register*/
- twi_write(i);
- twi_stop();
- _delay_us(100);
- twi_start();
- twi_write(DS1307_R);
- rtc[i]=twi_read();
- twi_stop();
- _delay_us(100);
- }
- }
- int main(void)
- {
- /* Replace with your application code */
- _delay_ms(1000);
- display_init();
- adc_init();
- //rtc_init();
- display_clear(0);
- display_text_8x16(0,0,"Software TWI");
- display_text_8x16(0,1,"ATMega644P OLED");
- display_text_8x16(0,2,"SH1106 DS1307");
- display_text_8x16(0,3,"RTC Example");
- _delay_ms(10000);
- display_clear(0);
- while (1)
- {
- rtc_read();
- sprintf(msg,"Time: %02X:%02X:%02X",rtc[2],rtc[1],rtc[0]);
- display_text_8x16(0,0,msg);
- sprintf(msg,"Date: %02X/%02X/20%02X",rtc[4],rtc[5],rtc[6]);
- display_text_8x16(0,1,msg);
- //Read POT
- temp=read_adc(0);
- //Convert To Voltage
- voltage=temp*5.0/1023;
- sprintf(msg,"ADC0: %4d %.2fV",temp,voltage);
- display_text_8x16(0,2,msg);
- //Read LM35
- temp=read_adc(1);
- voltage=temp*5.0/1023;
- voltage*=100;
- sprintf(msg,"Temp: %.2f C",voltage);
- display_text_8x16(0,3,msg);
- _delay_us(500);
- }
- }
The schematic is the one's above.
AVR Experiment Board:
| ATMega644P Software TWI SH1106 OLED |
| ATMega644P Software TWI SH1106 OLED |
Click here to download this example.
No comments:
Post a Comment