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:
Mon Mar 28 05:58:07 2016 +0000
Revision:
2:be7af7513a1b
Parent:
1:e747c4ea86e0
Child:
3:a51d2418c6fe
works on STM-F401RE

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 2:be7af7513a1b 12 By Jose Ellsworth CTO of A2WH Free use for all, No promises, no warranty
joeata2wh 2:be7af7513a1b 13
joeata2wh 2:be7af7513a1b 14 NOTE: To force reset of clock time change ClockSetSentinal to a different
joeata2wh 2:be7af7513a1b 15 value. Or you can change on the compare statement below if you want to keep
joeata2wh 2:be7af7513a1b 16 the same sentinal.
joeata2wh 2:be7af7513a1b 17 */
joeata2wh 2:be7af7513a1b 18 #define ClockSetSentinal 20
Sissors 0:5e6107966aba 19 #include "mbed.h"
Sissors 0:5e6107966aba 20 #include "DS1302.h"
joeata2wh 2:be7af7513a1b 21 #include "compile_time_to_system_time.h"
Sissors 0:5e6107966aba 22
Gijsvanoort 1:e747c4ea86e0 23 DS1302 clk(SCLK, IO, CE);
Sissors 0:5e6107966aba 24
Sissors 0:5e6107966aba 25 int main() {
joeata2wh 2:be7af7513a1b 26
joeata2wh 2:be7af7513a1b 27 printf("compile time=%s date=%s\r\n",__TIME__,__DATE__);
joeata2wh 2:be7af7513a1b 28 time_t build_time = cvt_date(__DATE__,__TIME__);
joeata2wh 2:be7af7513a1b 29 printf("compile time reformate=%s r\n", ctime(&build_time));
joeata2wh 2:be7af7513a1b 30
joeata2wh 2:be7af7513a1b 31 time_t seconds = clk.time(NULL);
Sissors 0:5e6107966aba 32
joeata2wh 2:be7af7513a1b 33 if (clk.recallByte(0) != ClockSetSentinal) {
joeata2wh 2:be7af7513a1b 34 // our clock chip has not been initialized
joeata2wh 2:be7af7513a1b 35 // to with this utility so we need to store
joeata2wh 2:be7af7513a1b 36 // a starting time and our sentinal so we
joeata2wh 2:be7af7513a1b 37 // know it was already setup.
joeata2wh 2:be7af7513a1b 38 printf("setting time in DS1302");
joeata2wh 2:be7af7513a1b 39 clk.storeByte(0, ClockSetSentinal);
joeata2wh 2:be7af7513a1b 40 set_time(build_time);
joeata2wh 2:be7af7513a1b 41 clk.set_time(build_time);
joeata2wh 2:be7af7513a1b 42 }
joeata2wh 2:be7af7513a1b 43 else {
joeata2wh 2:be7af7513a1b 44 // On Startup we want to copy our clock value
joeata2wh 2:be7af7513a1b 45 // to our CPU chip. After that the internal clock
joeata2wh 2:be7af7513a1b 46 // can keep it runing but we should re-sync every so often.
joeata2wh 2:be7af7513a1b 47 printf("reading time from DS1302\r\n");
joeata2wh 2:be7af7513a1b 48 char storedByte = clk.recallByte(0);
joeata2wh 2:be7af7513a1b 49 time_t seconds = clk.time(NULL);
joeata2wh 2:be7af7513a1b 50 set_time(seconds);
joeata2wh 2:be7af7513a1b 51 }
Sissors 0:5e6107966aba 52
Sissors 0:5e6107966aba 53 while(1) {
joeata2wh 2:be7af7513a1b 54 seconds = clk.time(NULL);
joeata2wh 2:be7af7513a1b 55 time_t intSeconds = time(NULL);
joeata2wh 2:be7af7513a1b 56 printf("ds1302Time=%s intTime=%s\r\n", ctime(&seconds), ctime(&intSeconds));
Sissors 0:5e6107966aba 57 wait(1);
Sissors 0:5e6107966aba 58 }
Sissors 0:5e6107966aba 59 }