removed obsolete #include "lcd.h"
[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 int ScanInt(int current) {
31   char reply[32], *r;
32
33   gets(reply);
34   if (isdigit(*(r=reply))) {
35     current=0;
36     do {
37       current*=10;
38       current+=(*r++)-'0';
39     } while (isdigit(*r));
40   }
41   return current;
42 }
43
44 char GetTime(struct RTCDate *rtcDate) {
45   printf ("Enter year [%d]: ", rtcDate->year);
46   rtcDate->year=ScanInt(rtcDate->year);
47   printf ("Enter month [%d]: ", rtcDate->month);
48   rtcDate->month=ScanInt(rtcDate->month);
49   printf ("Enter day [%d]: ", rtcDate->day);
50   rtcDate->day=ScanInt(rtcDate->day);
51   printf ("Enter hour [%d]: ", rtcDate->hour);
52   rtcDate->hour=ScanInt(rtcDate->hour);
53   printf ("Enter minute [%d]: ", rtcDate->minute);
54   rtcDate->minute=ScanInt(rtcDate->minute);
55   printf ("Enter second [%d]: ", rtcDate->second);
56   rtcDate->second=ScanInt(rtcDate->second);
57   rtcDate->hundredth=0;
58 }
59
60 void PrintTime(struct RTCDate *rtcDate) {
61   printf ("%04d-%02bd-%02bd %02bd:%02bd:%02bd.%02bd\n", 
62           rtcDate->year, rtcDate->month, rtcDate->day,
63           rtcDate->hour, rtcDate->minute, rtcDate->second,
64           rtcDate->hundredth);
65 }
66
67 void main (void) {
68   struct RTCDate rtcDate;
69   unsigned char seconds=0xff;
70
71   printf ("\nStarting RTC demo.\n");
72
73 #ifdef USE_LCD
74   LcdInit();
75   LcdLPrintf (0, "Starting RTC demo.");
76 #endif
77   
78   while(1) {
79     RtcRead(&rtcDate);
80
81 #ifdef USE_LCD
82     // if lcd is enabled it only syncs the second time
83     RtcRead(&rtcDate);
84 #endif    
85
86 #ifdef USE_LCD
87     LcdLPrintf (2,"%04d-%02bd-%02bd",
88                 rtcDate.year, rtcDate.month, rtcDate.day);
89     LcdLPrintf (3, "%02bd:%02bd:%02bd.%02bd", 
90                 rtcDate.hour, rtcDate.minute, rtcDate.second,
91                 rtcDate.hundredth);
92 #endif
93     if (rtcDate.second!=seconds) {
94       PrintTime(&rtcDate);
95       seconds=rtcDate.second;
96     }
97     
98     if (Serial0CharArrived()) {
99       switch (getchar()) 
100         {
101         case 0:
102           break;
103         case 'q': 
104           printf ("Ok.\n");
105           return;
106         default:
107           if (GetTime(&rtcDate)) {
108 #ifndef USE_LCD
109             PrintTime(&rtcDate);
110             RtcWrite(&rtcDate);
111             printf ("Time written.\n");
112 #endif
113           }
114         }
115     }
116   }
117
118 }