DS1307 won't set if already set

Has anyone else experienced that the DS1307 won't set if it already has the time? I'm using the ladyada library which includes a sketch to set the time using system time from the point of compiling (which means the time is out by about 15s). I've found that the time won't adjust if I've already set it and I have to remove the battery to set the time again! Seems very odd…

The fact that it will set if I remove the battery seems to rule out any sketch or connections issues?

For the benefit of future Googlers, I think I have my answer:-

It isn't "setting" again because it's already running.

adafruit_support:
It will only set the time if the RTC is not running. If you pull the battery to stop the RTC, the next time you upload it will set the time. On subsequent runs, the RTC will already be running.

  if (! RTC.isrunning()) {

Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(DATE, TIME));
  }

This is of course just what you want - you don't want it to reset the time to the sketch-build-time whenever the reset button is pressed do you?

The DS1307 must be stopped to set new time and data values, and then restarted. Most (all?) DS1307 libraries will have those commands avalible for use. No need to pull the battery. Note this fragment section from my RTC sketch for setting the time and data, note it first issues a RTC.stop() command, then sets the data values and ends with a RTC.start() command. Note that if a DS1307 (if it has a functioning back-up battery) is powered off while it was running, it will automatically return to the running mode when powered back up.

  RTC.stop();                          // chip must be stopped before sending new values
  RTC.set(DS1307_SEC,0);        //set the seconds
  RTC.set(DS1307_MIN,28);       //set the minutes
  RTC.set(DS1307_HR,23);        //set the hours   note 24 hour clock
  RTC.set(DS1307_DOW,5);        //set the day of the week
  RTC.set(DS1307_DATE,13);      //set the date
  RTC.set(DS1307_MTH,1);        //set the month
  RTC.set(DS1307_YR,11);         //set the year   
  RTC.start();                          // chip must be started for RTC to run

Lefty

The DS1307 must be stopped to set new time and data values, and then restarted.

Are you sure of this. I couldn't find this mentioned in my (Maxim) datasheet and I don't recall paying any special attention to this when tinkering with the chip.

Don

floresta:

The DS1307 must be stopped to set new time and data values, and then restarted.

Are you sure of this. I couldn't find this mentioned in my (Maxim) datasheet and I don't recall paying any special attention to this when tinkering with the chip.

Don

To be honest I don't know if it's required or not, but it is the most logical way. I'm pretty sure I once did try to reset time and date data while the chip was running and it wouldn't take. But then it's been a couple of years seen I wrote and tested my sketch. A little experimentation could prove it out one way or another, try to change the time well ahead while it's running and see if it accepts it or not. The main problem is that if you set values while it is running you could have roll-over values (example mins to hours) happen before all the values got entered and end up with wrong data.

Lefty

The main problem is that if you set values while it is running you could have roll-over values (example mins to hours) happen before all the values got entered and end up with wrong data.

I think that's covered as well. I know there is a buffer to take care of that while reading the clock and I think there may be another one (or the same one) used for writing to the clock.

Don

Lefty, how do you cope with the compile delay when setting the RTC? I really need my RTC to be set as accurately as possible, i.e. Resolution to ~500ms. Is daily synching with an NTP server the only way to achieve this?

Thanks

There is no need to stop the clock prior to updating date/time as long as you start writing to address zero (seconds register).

When you write to the seconds register, the internal clock circuitry is automatically reset and you then have a full second to finish writing the remaining date/time registers before rollover occurs.

DANE:
Lefty, how do you cope with the compile delay when setting the RTC? I really need my RTC to be set as accurately as possible, i.e. Resolution to ~500ms. Is daily synching with an NTP server the only way to achieve this?

Thanks

I use a separate sketch to set and display the time. I set the time/data constant values in the sketch ahead a few mins and there is a portion in the setup the says something like "hi, hit any key on the serial monitor when ready and I will set the time/date". I leave the serial monitor waiting and I then go to one of the web time standard sites and wait for the real time to get to the time I had compiled and loaded waiting just for me to hit a key in the serial monitor. I can usually get it within 500ms or so i bet. Once the time is set, I can load whatever sketch I want that uses the RTC.

The thing is you will not get very long term accuracy with a standard non temperature compensated crystal. SparkFun sells a much more accurate one that has a built in temp compensated crystal and is software compatible with the DS-1307.

So does that answer your question?

Lefty

You are misunderstanding the 'meaning' of start & stop

There are two.

  1. bit 7 of the 'seconds' register this disables the oscillator and hence STOPS the clock.

The Protection mode "START" & "STOP", when you send a byte sequence to the RTC you send the "START" ,"STOP" encapsulation command sequence which tells the chip it is going to be read/written, it is a protection system to stop the clock form being corrupted when a micro-controller goes down. it DOES NOT stop the RTC, but rather protects the user read registers so that the time is consistently read during update.

When reading or writing the time and date registers, secondary (user) buffers are used to prevent errors when the internal registers update.2When reading the time and date registers, the user buffers are synchronized to the internal registers on any I C START. The time information is read from these secondary registers while the clock continues to run.

If the library is blocking your writes then that is something completely different.

hardcore:
You are misunderstanding the 'meaning' of start & stop

Who is? If it's me, I'd like to know.

retrolefty:
I use a separate sketch to set and display the time. I set the time/data constant values in the sketch ahead a few mins and there is a portion in the setup the says something like "hi, hit any key on the serial monitor when ready and I will set the time/date". I leave the serial monitor waiting and I then go to one of the web time standard sites and wait for the real time to get to the time I had compiled and loaded waiting just for me to hit a key in the serial monitor. I can usually get it within 500ms or so i bet. Once the time is set, I can load whatever sketch I want that uses the RTC.

Would you please post this sketch? It sounds very useful. Thanks! $)

DANE:

retrolefty:
I use a separate sketch to set and display the time. I set the time/data constant values in the sketch ahead a few mins and there is a portion in the setup the says something like "hi, hit any key on the serial monitor when ready and I will set the time/date". I leave the serial monitor waiting and I then go to one of the web time standard sites and wait for the real time to get to the time I had compiled and loaded waiting just for me to hit a key in the serial monitor. I can usually get it within 500ms or so i bet. Once the time is set, I can load whatever sketch I want that uses the RTC.

Would you please post this sketch? It sounds very useful. Thanks! $)

Ok, here it is. It compiles in arduino version 22, but needs some tweeking to work on IDE 1.0. Note also that is uses a library that was avalible in the arduino playground ( #include <DS1307.h> // written by mattt on the Arduino forum and modified by D. Sjunnesson )

You just change the time and data values in the setup statements and upload to the board. The board will wait until you send a character from the serial monitor and then sets the RTC and then just keeps sending the time/date to the serial monitor.

/*Sets and reads the time and date values from a Real Time Clock (RTC) DS1307 and displays it in the serial monitor
 *
 *Created by D. Sjunnesson 1scale1.com d.sjunnesson (at) 1scale1.com
 *
 *Created with combined information from 
 *http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1180908809
 *http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1191209057
 *
 *
 *Big credit to  mattt (please contact me for a more correct name...) from the Arduino forum 
 *which has written the main part of the library which I have modified
 *
 */

#include <WProgram.h>
#include <Wire.h>
#include <DS1307.h> // written by  mattt on the Arduino forum and modified by D. Sjunnesson

void setup()
{
  Serial.begin(57600);
  Serial.println ("Hit a key to start");     // signal initalization done
  while(Serial.available() == 0){}
                              // Set value ahead of 'real' time, then start RTC when time matches some reference
  RTC.stop();
  RTC.set(DS1307_SEC,0);        //set the seconds
  RTC.set(DS1307_MIN,28);       //set the minutes
  RTC.set(DS1307_HR,23);        //set the hours   note 24 hour clock
  RTC.set(DS1307_DOW,5);        //set the day of the week
  RTC.set(DS1307_DATE,13);      //set the date
  RTC.set(DS1307_MTH,1);        //set the month
  RTC.set(DS1307_YR,11);         //set the year   
  RTC.start();   
}

void loop()
{

  Serial.print(RTC.get(DS1307_HR,true)); //read the hour and also update all the values by pushing in true
  Serial.print(":");
  Serial.print(RTC.get(DS1307_MIN,false));//read minutes without update (false)
  Serial.print(":");
  Serial.print(RTC.get(DS1307_SEC,false));//read seconds
  Serial.print("      ");                 // some space for a more happy life
  Serial.print(RTC.get(DS1307_DATE,false));//read date
  Serial.print("/");
  Serial.print(RTC.get(DS1307_MTH,false));//read month
  Serial.print("/");
  Serial.print(RTC.get(DS1307_YR,false)); //read year 
  Serial.println();

  delay(1000);

}

Lefty

DANE:

hardcore:
You are misunderstanding the 'meaning' of start & stop

Who is? If it's me, I'd like to know.

This is NOT you.

The DS1307 must be stopped to set new time and data values, and then restarted.

Obviously 'DANE' (that is you) you do not fully read the thread of the questions you ask.

For the record (possibly not including DANE, unless he changed his mind) to anyone saying the DS1307 needs to be 'stopped'
According to the data-sheet:

The "START & STOP" command encapsulation is only to ensure that the registers are stable whilst reading and writing,(consider it 'START' communication session/'STOP' communication session rather than 'STOP' the DS1307 and 'START' the DS1307)
The DS1307 is NOT stopped, the DS1307 can only be physically stopped by setting bit 7 of the seconds register, which disconnects the oscillator.

To top it off the "START & STOP" command MUST be sent whenever you access the DS1307 chip to READ the time, so in theory the action of reading the registers is exactly the same as setting them except the values are loaded/unloaded and if the seconds register is WRITTEN, a counter chain reset occurs.

This is SAFE behavior, because by sending the 'START'/'STOP' you can dictate the EXACT time the registers become stable, even if you read them an hour after issuing the 'START', they will hold the snapshot value of the clock, once you issue the 'STOP' the registers will be resynchronized with the clock that has been running in the back ground for the hour passed.

Digging about on this Start / Stop issue
I have found one library (DS1307 Arduino Library) for the DS1307 & Arduino
http://code.google.com/p/ds1307new/downloads/list

That actually physically STOPS the clock!! killing the oscillator by setting bit 7 in register 0
The only reason to do this would be for power saving, if the clock is NOT needed, all other libraries read/write to the clock without having this functionality.

I'm going to dig about in my stock room for an DS1307 clock module and see if we can settle this once and for all, just incase the data sheet is wrong.

It looks like I opened a can of worms.

Don

floresta:
It looks like I opened a can of worms.

Don

Not at all. At this forum we like this kind of activity. The biggest problem in my opinion is when a technical question or topic like this is finally resolved to everyone's satisfaction (and it usually does get resolved), we don't seem to have a good way to archive the information away in a matter that's easily retrieved by newcomers at a later date.

Lefty

Reply #15 brought up a pet peeve of mine:

I have found one library (DS1307 Arduino Library) for the DS1307 & Arduino
http://code.google.com/p/ds1307new/downloads/list

Just what we need - another "new" library. What are they going to call the next one, Newest, Return of New, Very Newest?

The best ambiguous choice would be 'Latest'. I have two sets of paper maps from the same publisher with that title, purchased two or three years apart (in the 70s).

Grumpy Don

floresta:
Reply #15 brought up a pet peeve of mine:

I have found one library (DS1307 Arduino Library) for the DS1307 & Arduino
http://code.google.com/p/ds1307new/downloads/list

Just what we need - another "new" library. What are they going to call the next one, Newest, Return of New, Very Newest?

The best ambiguous choice would be 'Latest'. I have two sets of paper maps from the same publisher with that title, purchased two or three years apart (in the 70s).

Grumpy Don

Unfortunately the major API changes that the Arduino development team created with the release of the Arduino V1.0 has cause the need for a whole lot of user contributed libraries to be in need of tweaking to be able to compile in 1.0. As there was never an 'official' arduino RTC library it's not surprising there are so many different ones out in the wild. Such is life in a wide open arduino world. :wink: