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

Committer:
joeata2wh
Date:
Wed Mar 30 14:50:02 2016 +0000
Revision:
3:a51d2418c6fe
Parent:
2:be7af7513a1b
update to use MIT license

Who changed what in which revision?

UserRevisionLine numberNew contents of line
joeata2wh 2:be7af7513a1b 1 /* Adapted to auto set clock when the clock chip has not been
joeata2wh 2:be7af7513a1b 2 previously initialized */
Sissors 0:5e6107966aba 3
joeata2wh 2:be7af7513a1b 4 #define SCLK PB_15
joeata2wh 2:be7af7513a1b 5 #define IO PB_14
joeata2wh 2:be7af7513a1b 6 #define CE PB_13
joeata2wh 2:be7af7513a1b 7 /* Initialize clock chip to compile time if we do
joeata2wh 2:be7af7513a1b 8 not detect that it has previsouly been set. After
joeata2wh 2:be7af7513a1b 9 that set the CPU time to reflect the clock chip time
joeata2wh 2:be7af7513a1b 10 on startup.
joeata2wh 2:be7af7513a1b 11
joeata2wh 3:a51d2418c6fe 12 By Joseph Ellsworth CTO of A2WH
joeata2wh 3:a51d2418c6fe 13 Take a look at A2WH.com Producing Water from Air using Solar Energy
joeata2wh 3:a51d2418c6fe 14 March-2016 License: https://developer.mbed.org/handbook/MIT-Licence
joeata2wh 2:be7af7513a1b 15
joeata2wh 2:be7af7513a1b 16 NOTE: To force reset of clock time change ClockSetSentinal to a different
joeata2wh 2:be7af7513a1b 17 value. Or you can change on the compare statement below if you want to keep
joeata2wh 2:be7af7513a1b 18 the same sentinal.
joeata2wh 3:a51d2418c6fe 19
joeata2wh 2:be7af7513a1b 20 */
joeata2wh 3:a51d2418c6fe 21 #define ClockSetSentinal 19
Sissors 0:5e6107966aba 22 #include "mbed.h"
Sissors 0:5e6107966aba 23 #include "DS1302.h"
joeata2wh 2:be7af7513a1b 24 #include "compile_time_to_system_time.h"
Sissors 0:5e6107966aba 25
Gijsvanoort 1:e747c4ea86e0 26 DS1302 clk(SCLK, IO, CE);
Sissors 0:5e6107966aba 27
Sissors 0:5e6107966aba 28 int main() {
joeata2wh 2:be7af7513a1b 29
joeata2wh 2:be7af7513a1b 30 printf("compile time=%s date=%s\r\n",__TIME__,__DATE__);
joeata2wh 2:be7af7513a1b 31 time_t build_time = cvt_date(__DATE__,__TIME__);
joeata2wh 2:be7af7513a1b 32 printf("compile time reformate=%s r\n", ctime(&build_time));
joeata2wh 2:be7af7513a1b 33
joeata2wh 2:be7af7513a1b 34 time_t seconds = clk.time(NULL);
Sissors 0:5e6107966aba 35
joeata2wh 2:be7af7513a1b 36 if (clk.recallByte(0) != ClockSetSentinal) {
joeata2wh 2:be7af7513a1b 37 // our clock chip has not been initialized
joeata2wh 2:be7af7513a1b 38 // to with this utility so we need to store
joeata2wh 2:be7af7513a1b 39 // a starting time and our sentinal so we
joeata2wh 2:be7af7513a1b 40 // know it was already setup.
joeata2wh 2:be7af7513a1b 41 printf("setting time in DS1302");
joeata2wh 2:be7af7513a1b 42 clk.storeByte(0, ClockSetSentinal);
joeata2wh 2:be7af7513a1b 43 set_time(build_time);
joeata2wh 2:be7af7513a1b 44 clk.set_time(build_time);
joeata2wh 2:be7af7513a1b 45 }
joeata2wh 2:be7af7513a1b 46 else {
joeata2wh 2:be7af7513a1b 47 // On Startup we want to copy our clock value
joeata2wh 2:be7af7513a1b 48 // to our CPU chip. After that the internal clock
joeata2wh 2:be7af7513a1b 49 // can keep it runing but we should re-sync every so often.
joeata2wh 2:be7af7513a1b 50 printf("reading time from DS1302\r\n");
joeata2wh 2:be7af7513a1b 51 char storedByte = clk.recallByte(0);
joeata2wh 2:be7af7513a1b 52 time_t seconds = clk.time(NULL);
joeata2wh 2:be7af7513a1b 53 set_time(seconds);
joeata2wh 2:be7af7513a1b 54 }
Sissors 0:5e6107966aba 55
Sissors 0:5e6107966aba 56 while(1) {
joeata2wh 2:be7af7513a1b 57 seconds = clk.time(NULL);
joeata2wh 2:be7af7513a1b 58 time_t intSeconds = time(NULL);
joeata2wh 2:be7af7513a1b 59 printf("ds1302Time=%s intTime=%s\r\n", ctime(&seconds), ctime(&intSeconds));
Sissors 0:5e6107966aba 60 wait(1);
Sissors 0:5e6107966aba 61 }
Sissors 0:5e6107966aba 62 }