Initialize clock chip to compile time the first time the program runs after that it will read the time from the clock chip. I find this easy since a quick recompile and load of utility sets the clock chip then we reload with the normal firmware. OK for use in low volume testing.

Dependencies:   DS1302 compile_time_to_system_time mbed

Fork of DS1302_HelloWorld by Erik -

Initialize Clock To Compile Time

Initialize clock chip to compile time the first time the program runs after that it will read the time from the clock chip.

I find this an easy way to initialize my clock chip and then it stays set for as long as the battery lasts. I use a quick recompile then copy this utility to the mbed board where it sets the time and date. After than reload the mbed board with normal firmware and the clock remains set.

I use it for low volume testing.

Underlying library for parsing DATE and TIME is as follows:

The time supplied by compiler is UMT so you need to adjust for local time if needed.

Referenced

main.cpp

Committer:
joeata2wh
Date:
2016-03-30
Revision:
3:a51d2418c6fe
Parent:
2:be7af7513a1b

File content as of revision 3:a51d2418c6fe:

/* Adapted to auto set clock when the clock chip has not been 
previously initialized */

#define SCLK    PB_15
#define IO      PB_14
#define CE      PB_13
/* Initialize clock chip to compile time if we do 
 not detect that it has previsouly been set.  After
 that set the CPU time to reflect the clock chip time
 on startup.
 
  By Joseph Ellsworth CTO of A2WH
  Take a look at A2WH.com Producing Water from Air using Solar Energy
  March-2016 License: https://developer.mbed.org/handbook/MIT-Licence 
 
 NOTE:  To force reset of clock time change ClockSetSentinal to a different
 value.  Or you can change on the compare statement below if you want to keep
 the same sentinal. 
 
*/
#define ClockSetSentinal 19
#include "mbed.h"
#include "DS1302.h"
#include "compile_time_to_system_time.h"

DS1302 clk(SCLK, IO, CE);

int main() {
    
    printf("compile time=%s date=%s\r\n",__TIME__,__DATE__);
    time_t build_time = cvt_date(__DATE__,__TIME__);
    printf("compile time reformate=%s r\n", ctime(&build_time));
    
    time_t seconds = clk.time(NULL);
    
    if  (clk.recallByte(0) != ClockSetSentinal) {
      // our clock chip has not been initialized
      // to with this utility so we need to store
      // a starting time and our sentinal so we 
      // know it was already setup.
      printf("setting time in DS1302");
      clk.storeByte(0, ClockSetSentinal);    
      set_time(build_time);      
      clk.set_time(build_time);
    }
    else {            
      // On Startup we want to copy our clock value
      // to our CPU chip.  After that the internal clock
      // can keep it runing but we should re-sync every so often.
      printf("reading time from DS1302\r\n");
      char storedByte = clk.recallByte(0);      
      time_t seconds = clk.time(NULL);      
      set_time(seconds);        
    }
    
    while(1) {
        seconds = clk.time(NULL);        
        time_t intSeconds = time(NULL);
        printf("ds1302Time=%s intTime=%s\r\n", ctime(&seconds), ctime(&intSeconds));
        wait(1);
    }
}