just another example
[fw/sdcc] / device / examples / ds390 / rtc390 / rtc390.c
1 /*-------------------------------------------------------------------------
2   rtc390.c - rtc demo for the DS1315 (tested on TINI)
3   
4    Written By - Johan Knol, johan.knol@iduna.nl
5     
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
9    later version.
10    
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.
15    
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.
19    
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 -------------------------------------------------------------------------*/
24
25 #include <stdio.h>
26 #include <ctype.h>
27
28 //#define USE_LCD
29
30 #ifdef USE_LCD
31 #include "lcd.h"
32 #endif
33
34 int ScanInt(int current) {
35   char reply[32], *r;
36
37   gets(reply);
38   if (isdigit(*(r=reply))) {
39     current=0;
40     do {
41       current*=10;
42       current+=(*r++)-'0';
43     } while (isdigit(*r));
44   }
45   return current;
46 }
47
48 char GetTime(struct RTCDate *rtcDate) {
49   printf ("Enter year [%d]: ", rtcDate->year);
50   rtcDate->year=ScanInt(rtcDate->year);
51   printf ("Enter month [%d]: ", rtcDate->month);
52   rtcDate->month=ScanInt(rtcDate->month);
53   printf ("Enter day [%d]: ", rtcDate->day);
54   rtcDate->day=ScanInt(rtcDate->day);
55   printf ("Enter hour [%d]: ", rtcDate->hour);
56   rtcDate->hour=ScanInt(rtcDate->hour);
57   printf ("Enter minute [%d]: ", rtcDate->minute);
58   rtcDate->minute=ScanInt(rtcDate->minute);
59   printf ("Enter second [%d]: ", rtcDate->second);
60   rtcDate->second=ScanInt(rtcDate->second);
61   rtcDate->hundredth=0;
62 }
63
64 void PrintTime(struct RTCDate *rtcDate) {
65   printf ("%04d-%02bd-%02bd %02bd:%02bd:%02bd.%02bd\n", 
66           rtcDate->year, rtcDate->month, rtcDate->day,
67           rtcDate->hour, rtcDate->minute, rtcDate->second,
68           rtcDate->hundredth);
69 }
70
71 void main (void) {
72   struct RTCDate rtcDate;
73   unsigned char seconds=0xff;
74
75   printf ("\nStarting RTC demo.\n");
76
77 #ifdef USE_LCD
78   LcdInit();
79   LcdLPrintf (0, "Starting RTC demo.");
80 #endif
81   
82   while(1) {
83     RtcRead(&rtcDate);
84
85 #ifdef USE_LCD
86     // if lcd is enabled it only syncs the second time
87     RtcRead(&rtcDate);
88 #endif    
89
90 #ifdef USE_LCD
91     LcdLPrintf (2,"%04d-%02bd-%02bd",
92                 rtcDate.year, rtcDate.month, rtcDate.day);
93     LcdLPrintf (3, "%02bd:%02bd:%02bd.%02bd", 
94                 rtcDate.hour, rtcDate.minute, rtcDate.second,
95                 rtcDate.hundredth);
96 #endif
97     if (rtcDate.second!=seconds) {
98       PrintTime(&rtcDate);
99       seconds=rtcDate.second;
100     }
101     
102     if (Serial0CharArrived()) {
103       switch (getchar()) 
104         {
105         case 0:
106           break;
107         case 'q': 
108           printf ("Ok.\n");
109           return;
110         default:
111           if (GetTime(&rtcDate)) {
112 #ifndef USE_LCD
113             PrintTime(&rtcDate);
114             RtcWrite(&rtcDate);
115             printf ("Time written.\n");
116 #endif
117           }
118         }
119     }
120   }
121
122 }