1 /*-------------------------------------------------------------------------
2 rtc390.c - rtc routines for 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 -------------------------------------------------------------------------*/
36 /* this is the address of the ds1315 phantom time chip, although
37 it doesn't really matter as long as it's in the 300000-3ffff
38 range since the chip only uses CE3*
41 xdata at 0x310000 static volatile unsigned char rtc;
43 // this is the 64bit pattern that has to be recognized by the ds1315
44 code unsigned char rtcMagic[8]={0xc5,0x3a,0xa3,0x5c,0xc5,0x3a,0xa3,0x5c};
46 static struct RTCDate{
48 unsigned char month, day, weekDay, hour, minute, second, hundredth;
51 #define BCDtoINT(x) (((x)&0x0f)+((x)>>4)*10)
52 #define INTtoBCD(x) (((x)%10)+(((x)/10)<<4))
55 unsigned char dummy, byte,bitMask;
61 for (byte=0; byte<8; byte++) {
62 for (bitMask=0x01; bitMask; bitMask<<=1) {
63 rtc = (rtcMagic[byte]&bitMask) ? 0xff : 0x00;
68 unsigned char RtcRead(struct RTCDate *rtcDate) {
69 unsigned char rtcBytes[8];
70 unsigned char byte,bitMask;
74 for (byte=0; byte<8; byte++) {
76 for (bitMask=0x01; bitMask; bitMask<<=1) {
78 rtcBytes[byte]|=bitMask;
82 rtcDate->year=2000 + BCDtoINT(rtcBytes[7]);
83 rtcDate->month=BCDtoINT(rtcBytes[6]);
84 rtcDate->day=BCDtoINT(rtcBytes[5]);
85 rtcDate->weekDay=rtcBytes[4]&0x07;
86 rtcDate->hour=BCDtoINT(rtcBytes[3]);
87 rtcDate->minute=BCDtoINT(rtcBytes[2]);
88 rtcDate->second=BCDtoINT(rtcBytes[1]);
89 rtcDate->hundredth=BCDtoINT(rtcBytes[0]);
90 if ((rtcBytes[4]&0x30) || (rtcBytes[3]&0x80)) {
91 //oscillator not running, reset not active or in 12h mode
97 void RtcWrite(struct RTCDate *rtcDate) {
98 unsigned char rtcBytes[8];
99 unsigned char byte,bitMask;
101 rtcBytes[7]=INTtoBCD(rtcDate->year-2000);
102 rtcBytes[6]=INTtoBCD(rtcDate->month);
103 rtcBytes[5]=INTtoBCD(rtcDate->day);
104 rtcBytes[4]=INTtoBCD(rtcDate->weekDay)&0x07; //set 24h mode
105 rtcBytes[3]=INTtoBCD(rtcDate->hour)&0x3f; // oscilator on, reset on
106 rtcBytes[2]=INTtoBCD(rtcDate->minute);
107 rtcBytes[1]=INTtoBCD(rtcDate->second);
108 rtcBytes[0]=INTtoBCD(rtcDate->hundredth);
112 for (byte=0; byte<8; byte++) {
113 for (bitMask=0x01; bitMask; bitMask<<=1) {
114 rtc = (rtcBytes[byte]&bitMask) ? 0xff : 0x00;
119 int ScanInt(int current) {
123 if (isdigit(*(r=reply))) {
128 } while (isdigit(*r));
133 char GetTime(struct RTCDate *rtcDate) {
134 printf ("Enter year [%d]: ", rtcDate->year);
135 rtcDate->year=ScanInt(rtcDate->year);
136 printf ("Enter month [%d]: ", rtcDate->month);
137 rtcDate->month=ScanInt(rtcDate->month);
138 printf ("Enter day [%d]: ", rtcDate->day);
139 rtcDate->day=ScanInt(rtcDate->day);
140 printf ("Enter hour [%d]: ", rtcDate->hour);
141 rtcDate->hour=ScanInt(rtcDate->hour);
142 printf ("Enter minute [%d]: ", rtcDate->minute);
143 rtcDate->minute=ScanInt(rtcDate->minute);
144 printf ("Enter second [%d]: ", rtcDate->second);
145 rtcDate->second=ScanInt(rtcDate->second);
146 rtcDate->hundredth=0;
149 void PrintTime(struct RTCDate *rtcDate) {
150 printf ("%04d-%02bd-%02bd %02bd:%02bd:%02bd.%02bd\n",
151 rtcDate->year, rtcDate->month, rtcDate->day,
152 rtcDate->hour, rtcDate->minute, rtcDate->second,
156 unsigned int intmul(unsigned int a, unsigned int b) {
160 long longmul(long a, long b) {
165 struct RTCDate rtcDate;
166 unsigned char seconds=0xff;
168 printf ("\nStarting RTC demo.\n");
172 LcdLPrintf (0, "Starting RTC demo.");
179 // if lcd is enabled it only syncs the second time
184 LcdLPrintf (2,"%04d-%02bd-%02bd",
185 rtcDate.year, rtcDate.month, rtcDate.day);
186 LcdLPrintf (3, "%02bd:%02bd:%02bd.%02bd",
187 rtcDate.hour, rtcDate.minute, rtcDate.second,
190 if (rtcDate.second!=seconds) {
192 seconds=rtcDate.second;
195 if (Serial0CharArrived()) {
204 if (GetTime(&rtcDate)) {
208 printf ("Time written.\n");