Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
05/25/11 19:26
Modified:
  05/25/11 20:08

Read: times


 
#182396 - What I Do With RTC
Responding to: ???'s previous message
Each RTC chip is somewhat different than others. The DS1307 is no exception to this and being a chip that has been around for quite a long time it lacks some nice things that other chips have. Other chips I've used from STMicro and Microchip may have status bits that set from events like battery fail, oscillator failed, clock counting stopped and others. These additional features can be incorporated into the basic algorithm I describe below to come to conclusion about clock state at initial power on.

My basic strategy at each power on is to follow this sequence:

1) Perform any hardware level initialization that permits access to I2C bus.

2) Attempt to read the clock bytes out of the RTC into local variables. Post a failure if the I2C handshaking and ACK logic fails to respond correctly.

3) Delay an amount known to be longer than one second.

4) Re-read the clock bytes again and check if the seconds value has changed from the previously read value. If it has not changed then post an error that the RTC may be broken.

4a) Note that with some older RTCs it is standard practice to read the clock bytes multiple times and only take as valid reading that from which the seconds value has NOT changed from the previous read. Most modern RTCs (including the DS1307) do not require this if you arrange to read out all the clock bytes within a single I2C bus block read transaction. None the less make sure to check the data sheet carefully for your specific chip.

5) Perform a sanity check on the clock byte values read. Does Year=00-99, Month=1-12, Date=1-31 (should include feb 29th check for leap years and legal range check for current month from a lookup table), Weekday=0-6 (some RTCs use 1-7), Hour=00-23 (some RTCs may be in an AM.PM mode so this may need adjusting), Minute=00-59 and Second=00-59. If any value is invalid display instead an indication on the display that the clock needs setting. You may choose to show "Set RTC", set a default date/time such as Jan 1, 2000 @ 00:00:00, or fetch a last known valid RTC time from some non-volatile storage.

6) Once to this point you can periodically read the RTC into local variables and convert to displayable format for the LCD or LEDs.

Good Luck.
Michael Karas



List of 12 messages in thread
TopicAuthorDate
DS1307 initialization            01/01/70 00:00      
   It's simple, really            01/01/70 00:00      
      Simple really            01/01/70 00:00      
   What I Do With RTC            01/01/70 00:00      
      Sanity check            01/01/70 00:00      
         ARGH>>> You must assign a subject to your message, please...            01/01/70 00:00      
            Indeed.            01/01/70 00:00      
            Broken Time Representations            01/01/70 00:00      
               Very many break years in circulation for 2-digit years            01/01/70 00:00      
            occasionally the government ...            01/01/70 00:00      
      Nice strategy!            01/01/70 00:00      
   Write Date Values Once!            01/01/70 00:00      

Back to Subject List