2

I am trying to make an Arduino project based on the Sparkfun DS1307 RTC (real-time clock).

There are a few issues with how Arduino libraries don't support checking if the Arduino board has been powered down and check for that.

Then using that as a conditional as to when the program should fall back to the compiled time if the RTC isn't set to a time yet because of something else.

When I power down the clock, it will automatic fall back to the compiled time every single time, regardless of what is currently on the RTC. Clearly this is not something you would want.

Edited - This is my current code. Keep in mind there are a few more changes I could do like take Byte zero declare out of the loop so it is still rough.

#include <Wire.h>
#include <SparkFunDS1307RTC.h>

#define DS1307_ADDRESS 0x68

void setup() {
  Wire.begin();
  Serial.begin(9600);
  rtc.begin();
  if(false){
    //Should check RTC somehow to determine if should fallback to compiled time here.
    Serial.println("The DS1307 clock has lost power and reset to compiler's time");
    rtc.autoTime();
  }
}

void loop() {
  if(digitalRead(7)==LOW)
    printDate();
  delay(1000);
}

byte bcdToDec(byte val){
  return ((val/16*10) + (val%16));
}

void printDate(){
  rtc.update();
  Wire.beginTransmission(DS1307_ADDRESS);
  byte zero = 0x00;
  Wire.write(zero);
  Wire.endTransmission();
  Wire.requestFrom(DS1307_ADDRESS, 7);
  int second = bcdToDec(Wire.read());
  int minute = bcdToDec(Wire.read());
  int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  int weekDay = bcdToDec(Wire.read()); //0-6 -> Sunday - Saturday
  int monthDay = bcdToDec(Wire.read());
  int month = bcdToDec(Wire.read());
  int year = bcdToDec(Wire.read());
  //Print the date like 3/1/1/11 23:59:59
  Serial.print(month);
  Serial.print("/");
  Serial.print(monthDay);
  Serial.print("/");
  Serial.print(year);
  Serial.print(" ");
  Serial.print(hour);
  Serial.print(":");
  Serial.print(minute);
  Serial.print(":");
  Serial.println(second);
}
11
  • You only need to set the time in the RTC once. There is no need to keep it in the code. But if you still want to, the DS1307 has a Clock Halt bit that is set when the RTC has lost it's power. So you can only set the time when this bit is set.
    – Gerben
    Jun 5 '17 at 20:43
  • How do I check if this bit ha been set?
    – Vyndicu
    Jun 5 '17 at 23:11
  • 1
    That would indeed make it easier for others. The sparkfun library has no way to read the CH bit. You could instead check if the current year is smaller that 2017. The default value for year is 2000 if the chip loses power.
    – Gerben
    Jun 6 '17 at 19:53
  • 1
    Yeah that would work well.
    – Vyndicu
    Jun 6 '17 at 20:02
1

DS1307 gives a square wave output which can be set to specific frequencies which is available in the datasheet. Initially the square wave output is disabled. You can enable it when you set the time. When you power up again, check for the square wave and if it is absent you can set the time again. This can be easily done by creating a timeout code and resetting the timeout timer in an interrupt. If square wave is present an interrupt will be generated accordingly and the timeout time period will be set to initial value. If the timeout time expires you set the clock. This can even track clock failures in real time. Also you can use the same interrupt to extract data from the RTC as square wave output can be configured to 1Hz and hence you can poll the RTC at every 1Hz and take the data from the RTC.

0

There are a few issues with how arduino's libraries doesn't support checking if the arduino board has been powered down and check for that.

How is it supposed to do that? The board's powered down.

When I powered down the clock. It will automatic fallback to compiled time every single time regardless of what is currently on RTC. Clearly this is not something you would want.

So don't have a compiled in time.

1
  • I saw there was a flag in one of the custom library that check if RTC clock lost power (due to battery dying or disconnect or whatever). But it was for the different brand of RTC DS1307 that I have.
    – Vyndicu
    Jun 5 '17 at 23:10
0

Easy. Here is one way to do it.

  1. Read the clock off the rtc.
  2. Wait a known period of time, based on the mcu frequency,
  3. Read the clock off the rtc again.
  4. Compare the two time reading and it should have advanced. If not, fall back to compiled time.

There are other ways to do it as well, like bor, or square wave output, interrupt on vbat line, ......

1
  • 2
    This RTC real time clock as far I am aware always tick forward. So checking for that time delta doesn't tell me if the clock has been reset to default time or not. Which is what the question is about.
    – Vyndicu
    Sep 6 '17 at 17:52
0

You could periodically store the date and time in the battery-backed SRAM in the DS1307. Every few seconds, once a minute, something like that. Upon sketch start, perhaps in setup(), read the current RTC time, compare to the stored time, if the current time is later than the stored time, you can do the math and see how long the Arduino has been down. Or use the Arduino's EEPROM to hold the last stored time if you are concerned about the RTC losing it's battery and resetting to it's default date and time, which would be earlier than the EEPROM time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.