1 /*-------------------------------------------------------------------------
2 rtc390.c - time demo using the DS1315 (tested on TINI)
4 Written By - Johan Knol, johan.knol@iduna.nl
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 In other words, you are welcome to use, share and improve this program.
21 You are forbidden to forbid anyone else to use, share and improve
22 what you give them. Help stamp out software-hoarding!
23 -------------------------------------------------------------------------*/
28 // until we have a decent scanf:
29 int ScanInt(int current) {
33 if (isdigit(*(r=reply))) {
38 } while (isdigit(*r));
43 char GetTime(struct tm *rtcTime) {
44 printf ("Enter year [%d]: ", rtcTime->tm_year+1900);
45 rtcTime->tm_year=ScanInt(rtcTime->tm_year+1900);
46 printf ("Enter month [%d]: ", rtcTime->tm_mon+1);
47 rtcTime->tm_mon=ScanInt(rtcTime->tm_mon)-1;
48 printf ("Enter day [%d]: ", rtcTime->tm_mday);
49 rtcTime->tm_mday=ScanInt(rtcTime->tm_mday);
50 printf ("Enter hour [%d]: ", rtcTime->tm_hour);
51 rtcTime->tm_hour=ScanInt(rtcTime->tm_hour);
52 printf ("Enter minute [%d]: ", rtcTime->tm_min);
53 rtcTime->tm_min=ScanInt(rtcTime->tm_min);
54 printf ("Enter second [%d]: ", rtcTime->tm_sec);
55 rtcTime->tm_sec=ScanInt(rtcTime->tm_sec);
59 void PrintTime(struct tm *rtcTime, char verbose) {
61 printf ("%s%04d-%02d-%02d %02d:%02d:%02d.%02d\n",
62 verbose ? "RTC time: " : "", // this one still needs a cast
63 rtcTime->tm_year+1900, rtcTime->tm_mon+1, rtcTime->tm_mday,
64 rtcTime->tm_hour, rtcTime->tm_min, rtcTime->tm_sec,
65 rtcTime->tm_hundredth);
68 time_t calendarTime=mktime(rtcTime);
69 printf ("Seconds since 00:00:00 Jan 01 1970: %ld\n", calendarTime);
74 struct tm rtcTime, *now;
78 printf ("\nStarting RTC demo.\n");
81 PrintTime(&rtcTime,1);
85 now=localtime(&calendarTime);
87 if (now->tm_sec!=seconds) {
88 printf(ctime(&calendarTime));
92 if (Serial0CharArrived()) {
102 if (GetTime(&rtcTime)) {
104 printf ("Time written.\n");