Pages: [1] 2   Go Down
Author Topic: DS1307 won't set if already set  (Read 2360 times)
0 Members and 1 Guest are viewing this topic.
Wales
Offline Offline
Full Member
***
Karma: 0
Posts: 244
Don't take things too seriously
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Wales
Offline Offline
Full Member
***
Karma: 0
Posts: 244
Don't take things too seriously
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Quote from: 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.

Code:
  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__));
  }
Logged

0
Offline Offline
Shannon Member
****
Karma: 200
Posts: 11718
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

[ I won't respond to messages, use the forum please ]

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17263
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

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

Lefty
« Last Edit: February 13, 2012, 01:09:42 pm by retrolefty » Logged

Western New York, USA
Offline Offline
Faraday Member
**
Karma: 32
Posts: 4259
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17263
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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

« Last Edit: February 13, 2012, 02:29:16 pm by retrolefty » Logged

Western New York, USA
Offline Offline
Faraday Member
**
Karma: 32
Posts: 4259
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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
Logged

Wales
Offline Offline
Full Member
***
Karma: 0
Posts: 244
Don't take things too seriously
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Edison Member
*
Karma: 3
Posts: 1001
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17263
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 98
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Quote
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.
Logged

Wales
Offline Offline
Full Member
***
Karma: 0
Posts: 244
Don't take things too seriously
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You are misunderstanding the 'meaning' of start & stop

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

Wales
Offline Offline
Full Member
***
Karma: 0
Posts: 244
Don't take things too seriously
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!  smiley-money
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17263
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!  smiley-money


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.

Code:
/*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
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 98
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You are misunderstanding the 'meaning' of start & stop

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

This is NOT you.
Quote
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.

« Last Edit: February 14, 2012, 07:05:32 pm by hardcore » Logged

Pages: [1] 2   Go Up
Jump to: