Hi all, As part of a low-power Arduino-based project I'm working on, I wrote a DS1337 realtime clock library based on an example I found floating around on the internet ... looks like it was probably an old version of mattt's or xSmurf's libraries here.
Available here - includes an example sketch which shows and tests all the most important functions.
Mainly, I wrote in some routines to interchange easily between calendar date/times and epoch seconds (# of seconds elapsed since 1/1/2000, correctly accounting for leap years). Working in epoch seconds makes it much easier to work with relative times, e.g. "wakeup 15 seconds from now", add/subtract times and set alarms without worrying about what happens if the seconds/minutes/etc. roll over in between.
Now, it turns out xSmurf's library has now had this feature for quite some time, under the name UTS (unix time stamp) - noticed when I went to post this one - so this turns out to be mostly just a learning project for me after all ;-) Still, it's an alternate approach to the calculation that's probably a bit more straightforward to understand, and includes a complete set of examples, so hopefully someone finds it useful :-)
Public functions: DS1337();
unsigned char time_is_set(); // set TRUE when writeTime() is called; set FALSE by power loss or other clock stoppage / oscillator failure unsigned char alarm_is_set();
unsigned char enable_interrupt(); unsigned char disable_interrupt(); unsigned char clear_interrupt();
void readTime(); void readAlarm(); void writeTime(); void writeTime(unsigned long); void writeAlarm(); void writeAlarm(unsigned long); void setAlarmRepeat(byte repeat);
unsigned char getSeconds(); unsigned char getMinutes(); unsigned char getHours(); unsigned char getDays(); unsigned char getDayOfWeek(); unsigned char getMonths(); unsigned int getYears(); unsigned long date_to_epoch_seconds(unsigned int year, byte month, byte day, byte hour, byte minute, byte second); unsigned long date_to_epoch_seconds(); void epoch_seconds_to_date(unsigned long);
void setSeconds(unsigned char); void setMinutes(unsigned char); void setHours(unsigned char); void setDays(unsigned char); void setDayOfWeek(unsigned char); void setMonths(unsigned char); void setYears(unsigned int);
void start(void); void stop(void); unsigned char getRegister(unsigned char registerNumber); void setRegister(unsigned char registerNumber, unsigned char registerValue);