-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock.c
357 lines (301 loc) · 8.65 KB
/
clock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*****************************************************************************
*
* AVR Alarm Clock
*
* This file is part of the AVR Alarm Clock project. The project is
* distributed at:
* https://github.com/maximecb/AVR-Alarm-Clock
*
* Copyright (c) 2012, Maxime Chevalier-Boisvert. All rights reserved.
*
* This software is licensed under the following license (Modified BSD
* License):
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
* NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*****************************************************************************/
// Header files
//#include "config.h"
#include "i2cmaster.h"
//#include "lcd.h"
#include "clock.h"
//=============================================================================
// Real-Time Clock (RTC) handling
//=============================================================================
#define DEV_DS1307 0xD0
// Day of the week names
const char* dayNames[] = {
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
};
// Month names
const char* monthNames[] = {
"Jan",
"Fev",
"Mar",
"Avr",
"Mai",
"Jun",
"Jui",
"Aou",
"Sep",
"Oct",
"Nov",
"Dec",
};
uint8_t fromBCD(uint8_t byte)
{
return ((byte & 0xF0) >> 4) * 10 + (byte & 0xF);
}
uint8_t toBCD(uint8_t byte)
{
return (((byte / 10) % 10) << 4) + (byte % 10);
}
bool isLeapYear(uint16_t year)
{
return (
(year % 400 == 0) ||
((year % 4 == 0) && (year % 100 != 0))
);
}
uint8_t daysInMonth(uint8_t month, uint16_t year)
{
switch (month)
{
// 30 days hath September, April, June and November
case 9: // Sep
case 4: // Apr
case 6: // Jun
case 11: // Nov
return 30;
// All the rest have 31
default:
return 31;
case 2: // Feb
return isLeapYear(year)? 29:28;
}
}
uint8_t getWeekday(TimeVal* time)
{
//return day of the week
//Sunday = 0
int year = time->year;
int month = time->month;
int date = time->date;
int century = year / 100;
int cyear = year - 100 * century;
int c = 2 * (3 - (century % 4));
int y = cyear + cyear / 4;
int m = 0;
switch (month)
{
case 1 : isLeapYear(year)? 6:0; break;
case 2 : isLeapYear(year)? 2:3; break;
case 3 : m = 3; break;
case 4 : m = 6; break;
case 5 : m = 1; break;
case 6 : m = 4; break;
case 7 : m = 6; break;
case 8 : m = 2; break;
case 9 : m = 5; break;
case 10: m = 0; break;
case 11: m = 3; break;
case 12: m = 5; break;
}
// Compute the weekday
return (c + y + m + date) % 7;
}
const char* getDayStr(TimeVal* time)
{
return dayNames[getWeekday(time)];
}
const char* getMonthStr(TimeVal* time)
{
return monthNames[time->month - 1];
}
int cmpTime(TimeVal* tA, TimeVal* tB)
{
if (tA->year < tB->year)
return -1;
if (tA->year > tB->year)
return 1;
if (tA->month < tB->month)
return -1;
if (tA->month > tB->month)
return 1;
if (tA->date < tB->date)
return -1;
if (tA->date > tB->date)
return 1;
if (tA->hour < tB->hour)
return -1;
if (tA->hour > tB->hour)
return 1;
if (tA->min < tB->min)
return -1;
if (tA->min > tB->min)
return 1;
if (tA->sec < tB->sec)
return -1;
if (tA->sec > tB->sec)
return 1;
return 0;
}
int cmpTimeHM(TimeVal* tA, TimeVal* tB)
{
if (tA->hour < tB->hour)
return -1;
if (tA->hour > tB->hour)
return 1;
if (tA->min < tB->min)
return -1;
if (tA->min > tB->min)
return 1;
return 0;
}
void nextDate(TimeVal* time)
{
// If this is the last day of the current month
if (time->date >= daysInMonth(time->month, time->year))
{
// It is now the first of the following month
time->date = 1;
// If this is the last month of the year
if (time->month == 12)
{
// It is now the first month of the next year
time->month = 1;
// Move to the next year
time->year++;
}
else
{
// Simply move to the next month
time->month++;
}
}
else
{
// Simply move to the next day of the month
time->date++;
}
}
void setTime(TimeVal* timeVal)
{
/*
WRITE: 0xD0 ACK<< DS1307 write address
WRITE: 0x00 ACK<< location to write
WRITE: 0x00 ACK<< 0 seconds, reset CH bit to 0
WRITE: 0x30 ACK<< 30 minutes
WRITE: 0x09 ACK<< 9 hours, 24 hour clock mode
WRITE: 0x02 ACK<< Monday (day 2)
WRITE: 0x07 ACK<< 7th (date)
WRITE: 0x09 ACK<< September (month)
WRITE: 0x09 ACK<< '09 (year)
WRITE: 0x00 ACK<< Set control register to 0'
I2C STOP BIT
*/
// Compute the BCD data
uint8_t yearByte = toBCD(timeVal->year - 2000);
uint8_t monthByte = toBCD(timeVal->month);
uint8_t dateByte = toBCD(timeVal->date);
uint8_t dayByte = toBCD(getWeekday(timeVal) + 1);
uint8_t hourByte = toBCD(timeVal->hour);
uint8_t minByte = toBCD(timeVal->min);
uint8_t secByte = toBCD(timeVal->sec);
// Set device address and write mode
i2c_start_wait(DEV_DS1307 + I2C_WRITE);
// Write at address 0
i2c_write(0x00);
i2c_write(secByte); // Seconds, CH bit
i2c_write(minByte); // Minutes
i2c_write(hourByte); // Hours, 12/24h clock mode
i2c_write(dayByte); // Weekday
i2c_write(dateByte); // Date
i2c_write(monthByte); // Month
i2c_write(yearByte); // Year
// Set control register to 0
i2c_write(0x00);
// Stop, release bus
i2c_stop();
}
void getTime(TimeVal* timeVal)
{
/*
I2C START BIT
WRITE: 0xD0 ACK<< DS1307 write address
WRITE: 0x00 ACK<< set write pointer
I2C START BIT
WRITE: 0xD1 ACK<< DS1307 read address
READ: 0x30 ACK<< 30 seconds
READ: 0x30 ACK<< 30 minutes
READ: 0x09 ACK<< 9 hours, 24 hour clock mode
READ: 0x02 ACK<< Monday (day 2)
READ: 0x07 ACK<< 7th (date)
READ: 0x09 ACK<< September (month)
READ: 0x09 ACK<< '09 (year)
READ: 0x00 NACK<< Control register
I2C STOP BIT
*/
unsigned char ret;
// Set device address and write mode
ret = i2c_start(DEV_DS1307 + I2C_WRITE);
if (ret != 0)
return;
// Write read address
ret = i2c_write(0x00);
if (ret != 0)
return;
// Set device address and read mode
ret = i2c_rep_start(DEV_DS1307 + I2C_READ);
if (ret != 0)
return;
// Read the time data
unsigned char secByte = i2c_readAck();
unsigned char minByte = i2c_readAck();
unsigned char hourByte = i2c_readAck();
/*unsigned char dayByte=*/i2c_readAck();
unsigned char dateByte = i2c_readAck();
unsigned char monthByte = i2c_readAck();
unsigned char yearByte = i2c_readAck();
// Read the control register
i2c_readNak();
// Release the bus
i2c_stop();
// Compute the year
timeVal->year = 2000 + fromBCD(yearByte);
// Compute the month index
timeVal->month = fromBCD(monthByte);
// Compute the date
timeVal->date = fromBCD(dateByte);
// Compute the hours, minutes and seconds
timeVal->hour = fromBCD(hourByte);
timeVal->min = fromBCD(minByte);
timeVal->sec = fromBCD(secByte);
}