[HELP] Write time from DS1307 and write in its NVRAM

//Arduino 1.0+ Only
//Arduino 1.0+ Only

#include "Wire.h"
#include <LiquidCrystal.h>
#include <EEPROM.h>
#define DS1307_ADDRESS 0x68
byte zero = 0x00; //workaround for issue #527

//-- Initial pin LCD 16x2-----
LiquidCrystal lcd(13,12,11,10,9,8);
//---------------------------------

void setup(){
  Wire.begin();
  lcd.begin(16,2);
  
  setTime(); //MUST CONFIGURE IN FUNCTION
}

void loop(){
  printTime();
}

void setTime(){

  byte second =      3; // << i want to modify this
  byte minute =      2; // << and this
  byte hour =        1; // << and this

  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero); //stop Oscillator

  Wire.write(decToBcd(second));
  Wire.write(decToBcd(minute));
  Wire.write(decToBcd(hour));

  Wire.write(zero); //start 

  Wire.endTransmission();

}

byte decToBcd(byte val){
// Convert normal decimal numbers to binary coded decimal
  return ( (val/10*16) + (val%10) );
}

byte bcdToDec(byte val)  {
// Convert binary coded decimal to normal decimal numbers
  return ( (val/16*10) + (val%16) );
}

void printTime(){

  // Reset the register pointer
  Wire.beginTransmission(DS1307_ADDRESS);
  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

  //print time
  lcd.setCursor(0,0);
  lcd.print("The time is : ");
  
  //-----------hr-----------------//
  lcd.setCursor(0,1);
  if (hour<10){
  lcd.print(0);
  }
  lcd.print(hour);
  lcd.setCursor(2,1);lcd.print(":");
  //---------------------------------
    
  //------------mnt--------------//
  lcd.setCursor(3,1);
  if (minute<10){
  lcd.print(0);
  }
  lcd.print(minute);
  lcd.setCursor(5,1);lcd.print(":");
  //--------------------------------
  
  //------------sec--------------//
  lcd.setCursor(6,1); 
  if (second<10){
  lcd.print(0);
  }
  lcd.print(second);    
  //-------------------------------
}

My Question is : How to Read time from DS1307 and Write in its NVRAM “ONCE per SECOND” and in Setup Function, How to Read nvram (second, minute, hour) and set to second, minute, hour in function setTime() above. Please help me to solve this, i spent more time cause i’m newbie about this.

See if this might help:

econjack:
See if this might help:

http://bildr.org/2011/03/ds1307-arduino/

@econjack Thanks, but the problem is when power down, i just want to continue the last time after power is on. I want to modify the setTime (second, minute, hour) value with last time writed in NVRAM. is there any way?

the problem is when power down, i just want to continue the last time after power is on. I want to modify the setTime (second, minute, hour) value with last time writed in NVRAM. is there any way?

Please clarify. When the program restarts, do you want the current time, or do you want the time when the power went down?

If you want the program to restart with the current time, just comment the setTime() function out of the code and run it a second time.

cattledog:
Please clarify. When the program restarts, do you want the current time, or do you want the time when the power went down?

If you want the program to restart with the current time, just comment the setTime() function out of the code and run it a second time.

i just want the time when the power went down, plz help

i just want the time when the power went down, plz help

Since you can not predict when the power goes off, you will need to determine how close to that event you need to record a last value. Once you know the time period, you can read the RTC at that frequency and write the last value into the free NVRAM space in the device registers 0x08 - 0x3F. You should look at the ds1307 data sheet.
http://datasheets.maximintegrated.com/en/ds/DS1307.pdf When you power up you can read those registers.

There are probably methods which use large capacitors to power the Arduino after power goes off. Then you have to detect when power goes away, and get the reading and writing done within the capacity of the stored power. Mr. Google will be able to get you farther down that path than I can.

@ryhar25:
What are you really trying to do? Why do you want the clock to stop when there is no power?

Go to a store where they sell old, used things. Buy an old-fashioned electric clock. This is a link to a picture of such a clock. That way, when the power goes out, the clock stops, and when the power comes back on, the clock resumes from the time at which it had stopped.

cattledog:
Since you can not predict when the power goes off, you will need to determine how close to that event you need to record a last value. Once you know the time period, you can read the RTC at that frequency and write the last value into the free NVRAM space in the device registers 0x08 - 0x3F. You should look at the ds1307 data sheet.
http://datasheets.maximintegrated.com/en/ds/DS1307.pdf When you power up you can read those registers.

There are probably methods which use large capacitors to power the Arduino after power goes off. Then you have to detect when power goes away, and get the reading and writing done within the capacity of the stored power. Mr. Google will be able to get you farther down that path than I can.

Thanks for fast response and informative post cattledog :slight_smile:

We went thru this very same discussion like a week ago.
Do a forum search for DS1307 and saving time to NVRAM.

odometer:
@ryhar25:
What are you really trying to do? Why do you want the clock to stop when there is no power?

Go to a store where they sell old, used things. Buy an old-fashioned electric clock. This is a link to a picture of such a clock. That way, when the power goes out, the clock stops, and when the power comes back on, the clock resumes from the time at which it had stopped.

thats a good idea :D, but not solve this. thanks for ur reply :).

Following on cattledog's comment, Jameco.com has a 1.5F 5.5V supercapacitor for under $4 that might last long enough to do your write. Fairly cheap to find out.

Use it with a small boost regulator such as LTC3525 to hold the 5V line up for as long as the cap has juice.
Use with diodes to not back charge the power supply from the cap.

LTC3525fb voltage_boost.pdf (541 KB)

For example, here is the concept for a board I did a few years ago where some data needed to be saved to SD card in a 5V system. The ndividual boost regulator components are not shown.

@ryhar25:

You still didn't answer my question.
Why do you want the clock to stop running when there is no power (or, at least, pretend to do so)?
What kind of project are you trying to build?
What else depends on the timekeeping of this clock?

If you just want to measure power outages, go to the store and buy an old electric clock like I told you.
If you are trying to do something more complicated, then tell us what it is that you are really trying to do so that we can help you better.

Thx all for fast reply, this forum is the best. after many time, i solved this problem using "RTC.writenvram" and "RTC.readnvram" after read from "examples > rtclib-master > ds1307nvram" and old discuss from this forum a week ago. this project about egg incubator and my lecturer challenge me to solve this :D, especially for @CrossRoads and all member who helped me, i'm sorry for my bad language but thanks all :smiley: