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
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
- https://developer.mbed.org/users/petereiso/notebook/cc-time-functions/
- http://stackoverflow.com/questions/1765014/convert-string-from-date-into-a-time-t
main.cpp@1:e747c4ea86e0, 2015-02-21 (annotated)
- Committer:
- Gijsvanoort
- Date:
- Sat Feb 21 15:18:52 2015 +0000
- Revision:
- 1:e747c4ea86e0
- Parent:
- 0:5e6107966aba
- Child:
- 2:be7af7513a1b
Fixed bug which occurs when you define a different CE pin
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sissors | 0:5e6107966aba | 1 | #define SCLK PTC5 |
Sissors | 0:5e6107966aba | 2 | #define IO PTC4 |
Sissors | 0:5e6107966aba | 3 | #define CE PTC3 |
Sissors | 0:5e6107966aba | 4 | |
Sissors | 0:5e6107966aba | 5 | //Comment this line if the DS1302 is already running |
Sissors | 0:5e6107966aba | 6 | #define INITIAL_RUN |
Sissors | 0:5e6107966aba | 7 | |
Sissors | 0:5e6107966aba | 8 | #include "mbed.h" |
Sissors | 0:5e6107966aba | 9 | #include "DS1302.h" |
Sissors | 0:5e6107966aba | 10 | |
Gijsvanoort | 1:e747c4ea86e0 | 11 | DS1302 clk(SCLK, IO, CE); |
Sissors | 0:5e6107966aba | 12 | |
Sissors | 0:5e6107966aba | 13 | int main() { |
Sissors | 0:5e6107966aba | 14 | #ifdef INITIAL_RUN |
Sissors | 0:5e6107966aba | 15 | clk.set_time(1256729737); |
Sissors | 0:5e6107966aba | 16 | #endif |
Sissors | 0:5e6107966aba | 17 | |
Sissors | 0:5e6107966aba | 18 | char storedByte = clk.recallByte(0); |
Sissors | 0:5e6107966aba | 19 | printf("\r\nStored byte was %d, now increasing by one\r\n", storedByte); |
Sissors | 0:5e6107966aba | 20 | clk.storeByte(0, storedByte + 1); |
Sissors | 0:5e6107966aba | 21 | |
Sissors | 0:5e6107966aba | 22 | while(1) { |
Sissors | 0:5e6107966aba | 23 | time_t seconds = clk.time(NULL); |
Sissors | 0:5e6107966aba | 24 | printf("Time as a basic string = %s\r", ctime(&seconds)); |
Sissors | 0:5e6107966aba | 25 | wait(1); |
Sissors | 0:5e6107966aba | 26 | } |
Sissors | 0:5e6107966aba | 27 | } |