DS1307 I2C RTC and NVM

CAI_Support

Senior Member
There is a low price RTC I2C module on the market.  That is based on DS1307 and AT24C32 chip to provide real time clock, battery back up ram and 32K bytes eeprom.  It looks like very useful to store configuration data for other devices on the external EEPROM and dynamic data on the battery backup 56 bytes RAM.
 
Unix style time is a 32 bit number, good through year 2038.  DS1307 has different time keeping register that is good through year 2100. 
 
We will play with this RTC clock chip and its RAM and post result here.  To sync that clock with WC8, we may or may not need to change some of our code.  If anyone has any interest in this little module, you can visit this link:

http://www.ebay.com/itm/171308750204

 
 
 
 
 
DS1307 module shipped with byte zero reading 128 (or 0x80), that is CH bit set, means clock is set to halt to save battery.
To enable clock running, the CH bit needs to be cleared.  After that, PLC program can start read.  First, send an I2Cwrite to tell
DS1307 the address to read, then send another I2CWRITE to tell DS1307 to send. After that, PLC code will start to read back.
DS1307 expect to receive an acknowledge bit 0 in each read.  In the last read, PLC send acknowledge 1 to indicate read finished.
 

START
I2CWRITE 1 0 208 #208 is DS1307 address in write mode
I2CWRITE 0 0 0 #this is address to write
I2CWRITE 0 0 0 #write zero to above address to clear CH bit
RUNNING:
CALLSUB GET
DELAY 1000
GOTO RUNNING
END

GET:
I2CWRITE 1 0 208 #this is write command to tell DS1307 PLC is writing
I2CWRITE 0 0 0 #this is to tell DS1307 where to set pointer/address
I2CWRITE 1 0 209 #this is READ command to DS1307 with re-start bit set to 1
I2CREAD 0 0 VAR1 # read first byte to VAR1, 0 is acknowledge to slave continue read
I2CREAD 0 0 VAR2
I2CREAD 0 0 VAR3
I2CREAD 0 0 VAR4
I2CREAD 0 0 VAR5
I2CREAD 0 0 VAR6
I2CREAD 1 1 VAR7 # last read with acknowledge to 1 tell DS1307 finished reading, stop bit also 1.
RET

 
When not use the realtime clock, send those lines will stop the clock running and save battery:

I2CWRITE 1 0 208
I2CWRITE 0 0 0
I2CWRITE 0 1 0x80



 
 
 
 
 
 
On this tiny module, there is also another chip AT24C32, a 32K eeprom to store any user data.
It is hardwired its address A0, A1,A2 to ground, so that its address is 0xA0 for write, 0xA1 for read.
 
EEPROM data is addressed by another two bytes, then if write, the data bytes through I2C write command
for example, if write number 32 to address 0x0010 on this EEPROM chip,
 
I2CWRITE 1 0 0xA0
I2CWRITE 0 0 0x00
I2CWRITE 0 0 0x10
I2CWRITE 0 1 32
 
 
to read it back, please note after above write, its address pointer is already moved to 0x0011 inside EEPROM. PLC must send address pointer again
 
I2CWRITE 1 0 0xA0
I2CWRITE 0 0 0x00   #EEPROM memory address byte 0
I2CWRITE 0 0 0x10   #EEPROM memory address byte 1
I2CWRITE 1 0 0xA1  # send re-start bit, also device READ I2C address
I2CREAD   1 1 RAM11
 
Now RAM11 contains 32.  All above examples are using 3.02.18b2 firmware.  3.02.18d2 firmware will work same way.
 
CAI_Support said:
Ross,
 
If you have any recommendation for the usage of the realtime clock in PLC, please let us know.
 
Between work and fencing I haven't got near it yet, but I will!
 
No, I think that adding cheap modules will render the board unstable.
The basic functions should be run on trusted platforms.
 
Since we removed 1-wire RTC support, it may be helpful by adding DS1307 I2C RTC support, that would allow places where don't still have internet to keep the clock pretty accurate.  To ask user doing PLC code handle that seems to be too much work.  We may release a update firmware to include support from inside firmware.
 
We have a beta testing firmware for hw rev 2.2.2 board.
If you have need for this, please send us direct message, we will provide you with a update image for bootloader updating.
 
This testing firmware allows WC8 board rev 2.2.2 board with DS1307 I2C module with battery to maintain time, even it is powered off and without network connection.
System clock does lose ticks during running. Without Internet NTP server, its clock will slow down over the time.  RTC module will help system clock to stay accurate.
 
We will make update firmware for hw rev 2.3.x boards later on, as well as for WC32 boards to include this feature.
 
We do have the firmware. But through the testing, we feel there are couple things must be clear:
 
1) since firmware itself talks also on I2C bus, user I2C commands must be accurate.  For example, at the end of last read, if sensor requires NACT and STOP, PLC code must have them on, like:
I2CREAD  1   1   VAR1
so that this sensor will not keep sending things causing trouble for other sensors.
 
2) some I2C sensors do not allow multiple I2C masters. DS1307 RTC sensor is one of them.  With manufacture told us:
 

| In order to perform a Read command, a 'dummy' Write must first be
| issued to set the address pointer. If instead of seeing a
| 'reSTART' after the address another 8bits of data is sent, this data
| will be written to the targeted register. Since you have two Masters
| active on the I2C bus, it is likely that one Master is interruping the
| Read command of the other, resulting in a Write being issued to the
| time registers.
|
| We do not have a way to regulate multiple Masters in the DS1307, so
| the only solution I see is to have only one Master active at a time.
|


 
So that if you plan to have multiple I2C master talking to same I2C client, it might be a problem.
 
Anyone has need to set time in the past years?  With this RTC logic in, we want to know if anyone has need to set clock to say 5 years ago.  We want to add some logic that if the new RTC module added to the I2C bus, its clock might be set to 1970. We do not want that clock being used by board.
 
We want to add restriction that the RTC can not be set earlier than firmware build date. If RTC is earlier than firmware build date, it will be ignored.  Is that reasonable for everyone?
 
Here you can find download link for HW rev 2.2.2 board firmware, which supports DS1307 RTC.  When properly hooking up DS1307 RTC module on I2C bus, it will store the time into the module. When power lost or network lost, the RTC module will keep the clock accurate to less than 1s a day.  All the RTC handle logic is in PLC firmware, no user code nor PLC code needed for RTC handling.  HW rev 2.3.x and WC32 firmware to support this have not been released yet.
Code:
The latest firmware for hw rev 2.2.2  has a new version:
http://www.cainetworks.com/support/download/wc8plc030222-update.zip
 
Many DS1307 RTC modules shipped now are without battery.  Please note above mentioned RTC module design call for LIR2032 battery, which is rechargeable Li-ion battery.  That is different from CR2032 battery that can not be recharged.  Due to this rechargeable feature, if the module is not connected to power for a long time its battery could be drained.  The battery recharge cycle is 500 times at 0.2c charge rate.
 
This is update firmware for hw rev 2.3.x board for supporting DS1307 RTC module on I2C bus. The support is transparent and no user PLC coding required.

The latest firmware for hw rev 2.3.x has a new version:
http://www.cainetworks.com/support/download/wc8plc030320-update.zip

Please note this version will set ZBIT when I2CWRITE executed. ZBIT value reflects the I2C slave device ACK on the SDA line. That can help identify if the slave device exist on bus or not. Please note firmware version starting with 0303 for 2.3.x board only. If you have 2.2.2 hw board, please use firmware version starting with 0302.



 
Code:
The latest firmware for hw rev 2.2.2  has a new version, this version will set ZBIT during each I2CWRITE call:

http://www.cainetworks.com/support/download/wc8plc030223-update.zip
 
Back
Top