Talking to DS1307 with i2c

I am trying to use the DS1307 real time clock with Imp. Communication is over i2c.
I can read the clock, but am unable to write settings.

I am trying to set register 0x00 to 0x80 with the fuction:
hardware.i2c89.write(0xD0, format("%c%c", 0x00, 0x80));
but I do not receive an acknowledge after the register byte, and then the function hangs up.

If anyone has any insight into this problem I would appreciate it.

i2c commands shouldn’t ever hang up, but they can timeout; what is the write returning? (will be an int of some sort, 0=success)

Looking at the datasheet, you need to write all the time registers at once (within a second); it’s not the done thing to just hit the seconds register:

“The divider chain is reset whenever the seconds register is written. Write transfers occur on the I2C acknowledge from the DS1307. Once the divider chain is reset, to avoid rollover issues, the remaining time and date registers must be written within one second”

Not sure that’s your issue, but have you tried writing to the scratchpad ram to see if that works? Also the datasheet notes that the chip runs at 100kHz max, have you tried the 100/50 rates?

btw, when writing constants this format is easier: hardware.i2c89.write(0xd0, “\x00\x80”);

Thank you Hugo!!!

It was timing out while waiting for the rest of the data.
Wrote all 7 bytes at 100kHz and it works now.

And thanks for the alternative format. Much cleaner than what I had before.

hello blossomcoffee, is it possible for you to share your code for reading and writing the ds1307?
thank you.

Not sure why you’d want to use the DS1307? There is no connection available to get the NTP time from the server? It is accurate to 10ms absolute time, and I have written the code.