728x90

728x90

Saturday, January 31, 2026

ATMega644P TWI DS1307 and AT24C16 EEPROM Interfacing

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.

ATMega644P TWI DS1307 and AT24C16 EEPROM Interfacing
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.

ATMega644P TWI DS1307 and AT24C16 EEPROM Interfacing
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.

ATMega644P TWI DS1307 and AT24C16 EEPROM Interfacing
The DS1307 Chip and RTC Module

ATMega644P TWI DS1307 and AT24C16 EEPROM Interfacing
DS1307 Pin Diagrams
ATMega644P TWI DS1307 and AT24C16 EEPROM Interfacing
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.

ATMega644P TWI DS1307 and AT24C16 EEPROM Interfacing
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.

  1. /*
  2. * 10-i2c_ds1307_lcd.c
  3. *
  4. * Created: 1/31/2026 8:15:30 AM
  5. * Author : Admin
  6. */


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

  10. #define F_CPU 16000000UL

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

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

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

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

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

  39. void rtc_init(void){
  40. //i2cStart();
  41. /*D0 is DS1307 Write Address*/
  42. //i2cWrite(0xD0);
  43. /*Select Control Register*/
  44. //i2cWrite(0x07);
  45. /*Enable SQWE bit blinks at 1 Hz*/
  46. //i2cWrite(1<<4);
  47. //i2cStop();
  48. //_delay_ms(1);
  49. char rtc[8]={0x30,0x00,0x17,0x07,0x31,0x01,0x26,1<<4};
  50. for (char i=0;i<8;i++)
  51. {
  52. i2cStart();
  53. //D0 is DS1307 Write Address
  54. i2cWrite(0xD0);
  55. //Select Control Register
  56. i2cWrite(i);
  57. //Enable SQWE bit blinks at 1 Hz
  58. i2cWrite(rtc[i]);
  59. i2cStop();
  60. _delay_ms(10);
  61. }
  62. }

  63. struct rtc{
  64. char Seconds;
  65. char Minutes;
  66. char Hours;
  67. char Day;
  68. char Date;
  69. char Month;
  70. char Year;
  71. char Control;
  72. };

  73. char rtc[6], msg[16];

  74. int main(void)
  75. {
  76. _delay_ms(1000);
  77. DDRB=0xFF;
  78. struct rtc my_rtc={0x30,0x00,0x03,0x07,0x31,0x01,0x26,1<<4};
  79. i2cInit();
  80. lcd_init();

  81. lcd_text("ATMega644P I2C");
  82. lcd_xy(1,2);
  83. lcd_text("ds1307 RTC");
  84. _delay_ms(10000);
  85. lcd_clear();
  86. char count;
  87. lcd_command(0x0C);
  88. //rtc_init();
  89. char counter=0;
  90. while (1)
  91. {
  92. for(char i=0;i<7;i++){
  93. /*Second Register*/
  94. i2cStart();
  95. i2cWrite(0xD0);
  96. /*Select Second register*/
  97. i2cWrite(i);
  98. i2cStop();
  99. _delay_us(10);
  100. i2cStart();
  101. i2cWrite(0xD1);
  102. rtc[i]=i2cRead(1);
  103. i2cStop();
  104. _delay_us(10);
  105. //PORTB=rtc[i];
  106. }
  107. lcd_xy(1,1);
  108. sprintf(msg,"Time: %02X:%02X:%02X",rtc[2],rtc[1],rtc[0]);
  109. lcd_text(msg);
  110. lcd_xy(1,2);
  111. sprintf(msg,"Date: %02X/%02X/20%02X",rtc[4],rtc[5],rtc[6]);
  112. lcd_text(msg);
  113. counter++;
  114. if(counter>=2) { PORTB=1; counter=0;}
  115. else PORTB=0;
  116. _delay_ms(500);
  117. }
  118. }


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:

ATMega644P TWI DS1307 and AT24C16 EEPROM Interfacing
Schematic and Simulation

AVR Prototype Board: 

ATMega644P TWI DS1307 and AT24C16 EEPROM Interfacing
Start Up Screen

ATMega644P TWI DS1307 and AT24C16 EEPROM Interfacing
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.

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

 

 

 

 

No comments:

Post a Comment

320x50

Search This Blog

tyro-728x90