DS1302RTC library - Set the time

Hello forum, my first message here and I hope to have choise the right section.

I am using in my sketch the library "DS1302RTC" because provide me the ability to obtain separate information for hours/minutes/seconds.

I have found this library on http://playground.arduino.cc/Main/DS1302RTC everything works good but I don't understand how to write/update the RTC time using this library.

I have found the method .set(), .write(), .writeRTC() but I can't figure out what I have to pass to these methods

I try to investigate how opening the .h file and go trough the .cpp but this library involve the time.h library and led me to not understand anything more.

At the time I have a little code that use another DS1302 library to set the time but what I want is to integrate the "Write RTC" on my sketch using the DS1302RTC lib.

Someone can help me to solve this problem?

Thank you! Grazie!

I have not used this library, but it appears that a call to the now() function sets the RTC to the current system date and time. It would look something like:

time_t myTime;

myTime = now();

I haven't checked, but my guess is that time_t is a typedef for an unsigned long data data type. From there, you pass myTime to the functions you want to call.

now() gets the time from the RTC for you to use in your program. There is no system time to set the RTC to. Arduino doesn't have a system time. If it did you wouldn't ever need an RTC.

With most of the RTC libraries I have seen, you use set() to set the time. But for some reason the link to this library is hanging up on me and I can't see the library code ATM.

uint8_t DS1302RTC::set(time_t t)
{
  tmElements_t tm;

  breakTime(t, tm);
  return ( write(tm) );
}

There's the code from the .cpp for the set function. That looks like the one you want if you are using UNIX time to set it.

If you want to set hours, minutes, seconds, etc then you'll have to use the tmElements_t structure from the Time library and use this write method. But you'll have to setup a tmElements_t object first. With the set method youjust provide a UNIX time and that's all.

uint8_t DS1302RTC::write(tmElements_t &tm)
{
  uint8_t buff[8];

  writeEN(true);
  
  if(!writeEN()) return 255;                // Error! Write-protect not disabled

  buff[0] = dec2bcd(tm.Second);             // Disable Clock halt flag
  buff[1] = dec2bcd(tm.Minute);
  buff[2] = dec2bcd(tm.Hour);               // 24-hour mode
  buff[3] = dec2bcd(tm.Day);
  buff[4] = dec2bcd(tm.Month);
  buff[5] = tm.Wday;
  buff[6] = dec2bcd(tmYearToY2k(tm.Year));
  buff[7] = B10000000;                      // Write protect enable

  writeRTC(buff);

  return writeEN();
}

Thank you Delta_G you helped me understandanding the point.

Now, however, another problem appears to me, but I will open a separate post for this.

Thanks again