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

Files at this revision

API Documentation at this revision

Comitter:
joeata2wh
Date:
Mon Mar 28 05:58:07 2016 +0000
Parent:
1:e747c4ea86e0
Child:
3:a51d2418c6fe
Commit message:
works on STM-F401RE

Changed in this revision

DS1302.lib Show annotated file Show diff for this revision Revisions of this file
compile_time_to_system_time.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- a/DS1302.lib	Sat Feb 21 15:18:52 2015 +0000
+++ b/DS1302.lib	Mon Mar 28 05:58:07 2016 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/Sissors/code/DS1302/#c7cea2a415c3
+http://mbed.org/users/Sissors/code/DS1302/#9a746f303e59
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/compile_time_to_system_time.lib	Mon Mar 28 05:58:07 2016 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/users/joeata2wh/code/compile_time_to_system_time/#5f3730f44e19
--- a/main.cpp	Sat Feb 21 15:18:52 2015 +0000
+++ b/main.cpp	Mon Mar 28 05:58:07 2016 +0000
@@ -1,27 +1,59 @@
-#define SCLK    PTC5
-#define IO      PTC4
-#define CE      PTC3
+/* Adapted to auto set clock when the clock chip has not been 
+previously initialized */
 
-//Comment this line if the DS1302 is already running
-#define INITIAL_RUN
-
+#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 Jose Ellsworth CTO of A2WH  Free use for all, No promises, no warranty
+ 
+ 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 20
 #include "mbed.h"
 #include "DS1302.h"
+#include "compile_time_to_system_time.h"
 
 DS1302 clk(SCLK, IO, CE);
 
 int main() {
-    #ifdef INITIAL_RUN
-    clk.set_time(1256729737);
-    #endif
+    
+    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);
     
-    char storedByte = clk.recallByte(0);
-    printf("\r\nStored byte was %d, now increasing by one\r\n", storedByte);
-    clk.storeByte(0, storedByte + 1);
+    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) {
-        time_t seconds = clk.time(NULL);
-        printf("Time as a basic string = %s\r", ctime(&seconds));
+        seconds = clk.time(NULL);        
+        time_t intSeconds = time(NULL);
+        printf("ds1302Time=%s intTime=%s\r\n", ctime(&seconds), ctime(&intSeconds));
         wait(1);
     }
 }
--- a/mbed.bld	Sat Feb 21 15:18:52 2015 +0000
+++ b/mbed.bld	Mon Mar 28 05:58:07 2016 +0000
@@ -1,1 +1,1 @@
-http://world3.dev.mbed.org/users/mbed_official/code/mbed/builds/824293ae5e43
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/c0f6e94411f5
\ No newline at end of file