DS1302 Time set

Dear all,

I started a few weeks ago with Arduino Mega2560 and several sensors and shields. So far so good. One thing I can not manage; I bought a DS1302 RTC module and tried the sketch from the playground
// DS1302 RTC
// ----------
//
// Open Source / Public Domain
//
// Version 1
// By arduino.cc user "Krodal".
// June 2012
// Using Arduino 1.0.1
// Version 2
// By arduino.cc user "Krodal"
// March 2013
// Using Arduino 1.0.3, 1.5.2
// The code is no longer compatible with older versions.
// Added bcd2bin, bin2bcd_h, bin2bcd_l
// A few minor changes.
//
I really don't know how to SET the time.

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);

Any suggestions?

This is the page, Arduino Playground - DS1302

This part sets the time:

  // Example for april 15, 2013, 10:08, monday is 2nd day of Week.
  // Set your own time and date in these variables.
  seconds    = 0;
  minutes    = 8;
  hours      = 10;
  dayofweek  = 2;  // Day of week, any day can be first, counts 1...7
  dayofmonth = 15; // Day of month, 1...31
  month      = 4;  // month 1...12
  year       = 2013;

My sketch contains the real time:

// Example for april 15, 2013, 10:08, monday is 2nd day of Week.
// Set your own time and date in these variables.
seconds = 0;
minutes = 43;
hours = 21;
dayofweek = 2; // Day of week, any day can be first, counts 1...7
dayofmonth = 8; // Day of month, 1...31
month = 4; // month 1...12
year = 2013;

The only time I have seen so far is the time from the example 15 April etc. It was written once but how???

Follow the code down and this line sets the time:

DS1302_clock_burst_write( (uint8_t *) &rtc);

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:

  1. Will this function work? (Do I dare to make this change?)
  2. 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?

A quick and dirty solution would be make rtc structure global, comment out the declarations of rtc in loop and setup. :slight_smile:
Then the SetTime function will work.