DS1307 won't set if already set

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:

I have no complaints about the emergence of new libraries that improve upon and/or replace older ones.

The problem is when you improve a library called, for example, the XyzzyLibrary and you name the improved version 'NewXyzzyLibrary'. What do you call the next iteration?

Don

floresta:
I have no complaints about the emergence of new libraries that improve upon and/or replace older ones.

The problem is when you improve a library called, for example, the XyzzyLibrary and you name the improved version 'NewXyzzyLibrary'. What do you call the next iteration?

Don
[/quote]

I hear what what you are saying. My point is to just point out the any and all 3rd party libraries go through no kind of 'official' vetting, approval, official naming, or even review. They are at best just allowed to be parked in the playground section along with many other related libraries. The user contributed library part of the Arduino world is both it's greatest strength and greatest weakness. On the one hand there is a tremendous amount of 3rd party code avalible, but on the other hand it may not even work due to changes in the IDE over time.

Only when the Arduino developers group allow a specific library to become part of the official IDE distribution does a library have a chance to be cared and feed properly to keep up with IDE and new boards as they are released.

So a more basic question for the purpose of this posted thread might be, why is there not an 'official' DS1307 library distributed with the IDE? It's not like it's not the most common RTC used in the arduino world?

Lefty

hardcore:

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'

Thanks, hardcore. Not for your rude and incorrect assertion but for your being so kind as to clarify your remark.

... but for your being so kind as to clarify your remark.

The only reason he had to clarify his remark is because he is the one who introduced the red herring. In my opinion he is the one who did not fully understand "the thread of the question" we were discussing. It was obvious, before he started talking about Start and Stop encapsulation etc., that we were discussing the internal workings of the clock chip itself and not the I2C implementation.

He quotes "the data sheet" but I have no idea what data sheet he is referring to. I can't find the phrases he quotes in my Maxim DS1307 data sheet. In my data sheet I found one place where the term 'I2C START' is used in conjunction with reference to the buffers, but that was referring to reading, not writing.

Now, for the benefit of future Googlers, let me summarize the first two entries in this thread.

The original post questioned why, when using the sketch provided by ladyada, one cannot set a clock that is already running.

The second post gave the answer. It has nothing to do with the clock chip, it is because that particular sketch was written to perform in that manner.

Don

floresta:

... but for your being so kind as to clarify your remark.

The only reason he had to clarify his remark is because he is the one who introduced the red herring. In my opinion he is the one who did not fully understand "the thread of the question" we were discussing. It was obvious, before he started talking about Start and Stop encapsulation etc., that we were discussing the internal workings of the clock chip itself and not the I2C implementation.

He quotes "the data sheet" but I have no idea what data sheet he is referring to. I can't find the phrases he quotes in my Maxim DS1307 data sheet. In my data sheet I found one place where the term 'I2C START' is used in conjunction with reference to the buffers, but that was referring to reading, not writing.

Now, for the benefit of future Googlers, let me summarize the first two entries in this thread.

The original post questioned why, when using the sketch provided by ladyada, one cannot set a clock that is already running.

The second post gave the answer. It has nothing to do with the clock chip, it is because that particular sketch was written to perform in that manner.

Don

Personally, 'Don' I that you need to read the thread, before spouting off and 'warning' others about posts.

DANE:
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?

retrolefty:
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 first post clearly includes the sentence
"Has anyone else experienced that the DS1307 won't set if it already has the time?" then goes on to state the library being use is lady ada's

The second post clearly states the need to call RTC.stop() this post is not a 'red-herring', it is in the forum for all to see, nor does it mention anything about Lady ada, in fact if you knew anything about the lady ada library you would know that there in not even an RTS.stop() function, which would indicate we are obviously NOT talking about the lady ada library and the thread has moved on, albeit without your permission, but moved on it has.

Finally take a look at the title of the post.
"DS1307 won't set if already set", nope I don't see anything about a reference specifically to the lady ada library(granted the initial poster may have overlooked it, but by default the thread is now open to discuss ALL aspects of "DS1307 won't set if already set").

In my following post, hopefully the issue will be settled once and for all, irrelevant of the library used.

Enclosed is a fully tested sketch for the arduino uno, though it should work on other platforms with suitable connection adjustments.

The sketch allows the DS1307 registers to be set with an initial time.

At any time you may use the serial console to test the chip:

h Halt the internal DS1307 oscillator, keeping the current time intact
s Start the internal DS1307 oscillator,
r reset the DS1307 with the initial time

The above can be used in any sequence, you can for example re-set the time on the DS1307 chip whilst it is running,
or you can stop the oscillator and set the time.
*Note:

  1. Rightly or wrongly some script/libraries prevent the DS1307 chip from being reset once it has been initially set, this is NOTHING to do with chip functionality, but supposedly there to prevent the RTC from being corrupted on sketch startup.
  2. Some sketches just DO NOT work, mainly because of issues in the wire.h library that have since been corrected for >= DE 1.0

Hardcore_Ds1307_Debunk_001.zip (3.82 KB)

hardcore:
The sketch allows the DS1307 registers to be set with [a] time.

At any time you may use the serial console to test the chip:

h Halt the internal DS1307 oscillator, keeping the current time intact
s Start the internal DS1307 oscillator,
r reset the DS1307 with the initial time

If I'm not very much mistaken, Mr. Hardcore, what you've posted is effectively a working version of the below (amongst other things, i.e. your proof of debunk)?

retrolefty:
[It] needs some tweeking to work on IDE 1.0. 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 [...].

//Sets and reads the time and date values from a Real Time Clock (RTC) DS1307 and displays it in the serial monitor

Lefty

DANE:
If I'm not very much mistaken, Mr. Hardcore, what you've posted is effectively a working version of the below (amongst other things, i.e. your proof of debunk)?

retrolefty:
[It] needs some tweeking to work on IDE 1.0. 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 [...].

//Sets and reads the time and date values from a Real Time Clock (RTC) DS1307 and displays it in the serial monitor

Lefty

Jesus....
Sometimes I just despair.......

The program I attached allows multiple TESTING functions:

  1. Set a time-> then randomly RESET the time-> WITHOUT STOPPING the DS1307. (r)
  2. Set a time-> then randomly STOP the DS1307 -> THEN randomly RESET the time. ->THEN randomly RESTART the DS1307 (h r s)
  3. Set a time-> STOP the DS1307 -> START the DS1307 (h s)

Analysis

  1. Shows that you can reset the time randomly without stopping the DS1307 oscillator.
  2. Shows that the timer can be loaded and will not use the values until the DS1307 oscillator is started.
  3. Shows that the DS1307 oscillator can be stopped at the current time and holds it indefinitely.

Conclusions

  1. DS1307 does not need to be stopped to load time
    2 Consider the situation where people keep asking how to absolutely set an accurate time:
    Solution: set the time of the DS1307 in the future with the oscillator stopped, sync to external time source, at exactly the right time, START the oscillator, it is a single I2c byte write into an empty buffer, thereby reducing the wire.h library delays to a minimum.

caveats
1.The sample you refer to ONLY STOPS the DS1307 THEN sets the time, telling us absolutely nothing about the any possible requirements of stopping the oscillator to load the values.
2 The code is partially incorrect, since it stops the oscillator writes the values then restarts the oscillator, theoretically it will always be 'slow' , since the internal counter has been stopped for the duration of the writes to the other registers, whereas NOT stopping the clock ensures that the sub-second counting continues WHILST the other registers are being loaded leading to a theoretical higher accuracy.

  RTC.stop();

RTC.set(DS1307_SEC,0);        //set the seconds
........
  RTC.start();

hardcore:
Jesus....
Sometimes I just despair.......
If you cannot see and understand the difference then I cannot help you.

Hardcore, you're being very silly. Suggest you take some time off somewhere warm.

I'm not interested in your "debunk". I'm interested in how I could use your code to set my RTC with the correct time by sending it a serial message.

If you read the thread properly, you'll see that.

Also, you messed up your quotes in your last delightful missive.

P.S. I would just like to add that Hardcore amended his post after I posted this.

P.S. I would just like to add that Hardcore amended his post after I posted this.

You have to be very careful about this especially when replying to a post that is obviously incorrect. The original poster frequently goes back and fixed or removes the error and then you look like the one who is the idiot. It also frequently negates the thread as a source of useful information in the future.

One way to deal with this is with a quote in your reply and the other is to keep a copy of the incorrect stuff so you can repost it if necessary to clear things up.

Don

Yes, it's an old thread, but I don't want to start a new one if it's not necessary.
Using the latest RTClib I was/maybe still am having the same problem that Dane was having.
Now, retrolefty posted code

 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

I know things change (and that may be the problem. That code does not work with the ladyada lib link GitHub - adafruit/RTClib: A fork of Jeelab's fantastic RTC Arduino library
Finally my question: Was that code for a different library? If so which one?