The Two Wire Interface (TWI) is a serial communication protocol that uses only two wires, Serial Data (SDA) and Serial Clock (SCL). In some micro-controllers especially the PIC family it called I2C (Inter Integrated Circuit). It's bi-directional between master and slave devices. The MCU is a master device whenever it interfaces to a TWI peripheral device for instance a Real Time Clock (RTC). However between two TWI micro-controllers the user can select one of them to be a master or a slave device. For more information about the I2C please visit this link.
| AVR ATMega644P Prototype Board |
The ATMega644P TWI Interface
The ATMega644P has a TWI module that use a 7-bit addressing that allows up to 127 connection devices. It supports up to 400kHz clock rate via its baud rate generator and its pre-scaler. For more information about the TWI of AVR micro-controller please see this link. I won't write it here anymore due to duplication.
| TWI Bus Interconnection |
Since the clock and data pins of the TWI are open-drain they need two weak pull up resistors between 4.7kΩ to 10kΩ. It depends on the wires length, data and clock rate.
The programmer must configure these registers to set a proper operation of the TWI:
For more information please the datasheet of this device.
The DS1307 Real Time Clock (RTC)
This is an old TWI RTC chip released a few decade now. Its function is not rich compare to newer TWI RTC chip for instance the DS3231 RTC. It communication interface and internal organization is very simple.
| The DS1307 Chip and RTC Module |
| DS1307 Pin Diagrams |
| TYPICAL OPERATING CIRCUIT |
Its date and time data are stored in device's SRAM. The SRAM contains date, time, configuration and user data. SRAM data are keep alive by an additional back up battery typically a CR2032 coin battery.
| Timekeeper Registers |
In this example the MCU read its time and date data and display them on a character LCD. This program requires the HD44780 LCD driver that I don't put them here. Please see this link. We just need to copy the hd44780.h and the hd44780.c to project source folder and add them to the project.
- /*
- * 10-i2c_ds1307_lcd.c
- *
- * Created: 1/31/2026 8:15:30 AM
- * Author : Admin
- */
- #include <avr/io.h>
- #include <util/delay.h>
- #include <stdio.h>
- #define F_CPU 16000000UL
- void i2cInit(void){
- TWSR|=0x00; //Prescaler Selection Bit
- TWBR=0xF0; //Baud Rate Generator
- TWCR=(1<<TWEN); //Enable The TWI Module
- PORTC|=(1<<0);
- PORTC|=(1<<1);
- }
- void i2cStart(void){
- TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
- while((TWCR&(1<<TWINT))==0);
- }
- void i2cWrite(unsigned char data){
- TWDR=data;
- TWCR=(1<<TWINT)|(1<<TWEN);
- while((TWCR&(1<<TWINT))==0);
- }
- unsigned char i2cRead(char ACK){
- if(ACK==0)
- TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWEA);
- else
- TWCR=(1<<TWINT)|(1<<TWEN);
- while((TWCR&(1<<TWINT))==0);
- return TWDR;
- }
- void i2cStop(){
- TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
- _delay_us(10);
- }
- void rtc_init(void){
- //i2cStart();
- /*D0 is DS1307 Write Address*/
- //i2cWrite(0xD0);
- /*Select Control Register*/
- //i2cWrite(0x07);
- /*Enable SQWE bit blinks at 1 Hz*/
- //i2cWrite(1<<4);
- //i2cStop();
- //_delay_ms(1);
- char rtc[8]={0x30,0x00,0x17,0x07,0x31,0x01,0x26,1<<4};
- for (char i=0;i<8;i++)
- {
- i2cStart();
- //D0 is DS1307 Write Address
- i2cWrite(0xD0);
- //Select Control Register
- i2cWrite(i);
- //Enable SQWE bit blinks at 1 Hz
- i2cWrite(rtc[i]);
- i2cStop();
- _delay_ms(10);
- }
- }
- struct rtc{
- char Seconds;
- char Minutes;
- char Hours;
- char Day;
- char Date;
- char Month;
- char Year;
- char Control;
- };
- char rtc[6], msg[16];
- int main(void)
- {
- _delay_ms(1000);
- DDRB=0xFF;
- struct rtc my_rtc={0x30,0x00,0x03,0x07,0x31,0x01,0x26,1<<4};
- i2cInit();
- lcd_init();
- lcd_text("ATMega644P I2C");
- lcd_xy(1,2);
- lcd_text("ds1307 RTC");
- _delay_ms(10000);
- lcd_clear();
- char count;
- lcd_command(0x0C);
- //rtc_init();
- char counter=0;
- while (1)
- {
- for(char i=0;i<7;i++){
- /*Second Register*/
- i2cStart();
- i2cWrite(0xD0);
- /*Select Second register*/
- i2cWrite(i);
- i2cStop();
- _delay_us(10);
- i2cStart();
- i2cWrite(0xD1);
- rtc[i]=i2cRead(1);
- i2cStop();
- _delay_us(10);
- //PORTB=rtc[i];
- }
- lcd_xy(1,1);
- sprintf(msg,"Time: %02X:%02X:%02X",rtc[2],rtc[1],rtc[0]);
- lcd_text(msg);
- lcd_xy(1,2);
- sprintf(msg,"Date: %02X/%02X/20%02X",rtc[4],rtc[5],rtc[6]);
- lcd_text(msg);
- counter++;
- if(counter>=2) { PORTB=1; counter=0;}
- else PORTB=0;
- _delay_ms(500);
- }
- }
The RTC chip is halted whenever it's newly inserted or error by the MCU program for instance a high speed TWI clock rate.
Shematic:
| Schematic and Simulation |
AVR Prototype Board:
| Start Up Screen |
| Date and Time Reading |
This PCB was offered from 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.
![]() |
| 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.
![]() |
| PCBWay Instant Quote |


No comments:
Post a Comment