JanRorive:
These comments I don't understand:
// Enable Register
#define DS1302_WP DS1302_D7 // 1 = Write Protect, 0 = enabled
// Start by clearing the Write Protect bit
// Otherwise the clock data cannot be written
// The whole register is written,
// but the WP-bit is the only bit in that register.
DS1302_write (DS1302_ENABLE, 0);
Neither do I, but hopefully I don't have to to use the code.
I would like to lift the "SET_DATE_TIME_JUST_ONCE" out from the setup and in to a separate function, so what remains of it in setup is just:
#ifdef SET_DATE_TIME_JUST_ONCE
// Fill these variables with the date and time.
// int seconds, minutes, hours, dayofweek, dayofmonth, month, year;
setTime(0, 14, 1, 4, 9, 5, 2013);
#endif
Then the new function would look something like this:
setTime(int seconds, int minutes, int hours, int dayofweek, int dayofmonth, int month, int year)
{
// Set a time and date
// This also clears the CH (Clock Halt) bit,
// to start the clock.
// Fill the structure with zeros to make
// any unused bits zero
memset ((char *) &rtc, 0, sizeof(rtc));
rtc.Seconds = bin2bcd_l( seconds);
rtc.Seconds10 = bin2bcd_h( seconds);
rtc.CH = 0; // 1 for Clock Halt, 0 to run;
rtc.Minutes = bin2bcd_l( minutes);
rtc.Minutes10 = bin2bcd_h( minutes);
// To use the 12 hour format,
// use it like these four lines:
// rtc.h12.Hour = bin2bcd_l( hours);
// rtc.h12.Hour10 = bin2bcd_h( hours);
// rtc.h12.AM_PM = 0; // AM = 0
// rtc.h12.hour_12_24 = 1; // 1 for 24 hour format
rtc.h24.Hour = bin2bcd_l( hours);
rtc.h24.Hour10 = bin2bcd_h( hours);
rtc.h24.hour_12_24 = 0; // 0 for 24 hour format
rtc.Date = bin2bcd_l( dayofmonth);
rtc.Date10 = bin2bcd_h( dayofmonth);
rtc.Month = bin2bcd_l( month);
rtc.Month10 = bin2bcd_h( month);
rtc.Day = dayofweek;
rtc.Year = bin2bcd_l( year - 2000);
rtc.Year10 = bin2bcd_h( year - 2000);
rtc.WP = 0;
// Write all clock data at once (burst mode).
DS1302_clock_burst_write( (uint8_t *) &rtc);
#endif
}
The reason I want to lift it out is to reuse the function in order to be able to adjust the time, not by reprogramming the sketch but by user input.
My questions are:
- Will this function work? (Do I dare to make this change?)
- Do I need something more to call it from other places of the sketch, i e loop()? Or do I need some more code before or after the function call, like the "DS1302_write (DS1302_ENABLE, 0);" that I do not quite understand the meaning of?