-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDS3231.cpp
574 lines (410 loc) · 11 KB
/
DS3231.cpp
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
// SimpleDS3231 was started by Jan Roslan <[email protected]>
// https://github.com/Jackjan4/SimpleDS3231
// Made with love & released to the public domain! Enjoy!
#include <Wire.h>
#include "DS3231.h"
#include <Arduino.h>
#include <stdio.h>
// ==== General functions ====
/**
*
*/
void DS3231::begin() {
// We are joining the bus as a master
Wire.begin();
}
/**
*
*/
void DS3231::begin(uint8_t i2c_address) {
// Change I2C address
DS3231_ADDRESS = i2c_address;
begin();
}
/**
*
*/
void DS3231::setTime24(uint8_t year, uint8_t month, uint8_t date, uint8_t day, uint8_t hours, uint8_t minutes, uint8_t seconds) {
/// Calculate values
//year
uint8_t tenYear = year / 10;
uint8_t singleYear = year % 10;
// month
uint8_t tenMonth = month / 10;
uint8_t singleMonth = month % 10;
// date
uint8_t tenDate = date / 10;
uint8_t singleDate = date % 10;
// hours
uint8_t singleHour = hours % 10;
// minutes
uint8_t tenMinute = minutes / 10;
uint8_t singleMinute = minutes % 10;
// seconds
uint8_t tenSecond = seconds / 10;
uint8_t singleSecond = seconds % 10;
goToRegisterOpen(0x00);
/// Write Registers
// Register 00 - Seconds
Wire.write((tenSecond << 4) + singleSecond);
// Register 01 - Minutes
Wire.write((tenMinute << 4) + singleMinute);
// Register 02 - Hours
uint8_t hourVal;
if (hours >= 20) {
hourVal = 1;
hourVal <<= 1;
} else if (hours >= 10) {
hourVal = 1;
}
Wire.write((hourVal << 4) + singleHour);
// Register 03 - Day
Wire.write(day);
// Register 04 - Date
Wire.write((tenDate << 4) + singleDate);
// Register 05 - Month
Wire.write((tenMonth << 4) + singleMonth);
// Register 06 - Year
Wire.write((tenYear << 4) + singleYear);
Wire.endTransmission();
}
/**
* Returns the year from the DS3231
*/
uint8_t DS3231::getYear() {
uint8_t val = getRegister(0x06);
uint8_t tenYear = val >> 4;
uint8_t singleYear = val - (tenYear << 4);
return tenYear * 10 + singleYear;
}
/**
* Returns the year from the DS3231
*/
uint8_t DS3231::getMonth() {
uint8_t val = getRegister(0x05);
uint8_t tenMonth = val >> 4;
uint8_t singleMonth = val - (tenMonth << 4);
return tenMonth * 10 + singleMonth;
}
/**
* Returns the year from the DS3231
*/
uint8_t DS3231::getDate() {
uint8_t val = getRegister(0x04);
uint8_t tenDate = val >> 4;
uint8_t singleDate = val - (tenDate << 4);
return tenDate * 10 + singleDate;
}
/**
* Returns the day of the week from the DS3231
*/
inline uint8_t DS3231::getDow() {
return getRegister(0x03);
}
/**
* Returns the year from the DS3231
*/
uint8_t DS3231::getHours() {
// TODO: Handle 12-hour mode
// check hour mode
// 24 hour mode
uint8_t val = getRegister(0x02);
uint8_t tenHour = val >> 4;
uint8_t singleHour = val - (tenHour << 4);
return 10 * tenHour + singleHour;
}
/**
* Returns the minutes from the DS3231
*/
uint8_t DS3231::getMinutes() {
uint8_t val = getRegister(0x01);
uint8_t tenMinute = val >> 4;
uint8_t singleMinute = val - (tenMinute << 4);
return tenMinute * 10 + singleMinute;
}
/**
* Returns the year from the DS3231
*/
uint8_t DS3231::getSeconds() {
uint8_t val = getRegister(0x00);
uint8_t tenSecond = val >> 4;
uint8_t singleSecond = val - (tenSecond << 4);
return tenSecond * 10 + singleSecond;
}
/**
* Returns a string which represents the current time.
* Can for example by printed into the console or used on other displays.
* Formatting: DD.MM.YYYY HH:MM:SS
* For a more textual representation, use getTimeStringText().
* Parameters:
* monthBeforeDate - Changes the formatting to MM.DD.YYYY HH:MM:SS which is common in the US for example.
* useAmPm - Displays the time in 12-hour-mode
*/
char* DS3231::getTimeString() {
static char buffer[21];
sprintf(buffer, "%02d.%02d.20%d %02d:%02d:%02d", getDate(), getMonth(), getYear(), getHours(),getMinutes(),getSeconds());
return buffer;
}
/**
*
*/
void DS3231::setAlarm1(uint8_t date, uint8_t day, uint8_t hours, uint8_t minutes, uint8_t seconds, uint8_t alarm_mask) {
/// Calculate values
// date
uint8_t tenDate = date / 10;
uint8_t singleDate = date % 10;
// hours
uint8_t singleHour = hours % 10;
// minutes
uint8_t tenMinute = minutes / 10;
uint8_t singleMinute = minutes % 10;
// seconds
uint8_t tenSecond = seconds / 10;
uint8_t singleSecond = seconds % 10;
// Navigate to Register 07
goToRegisterOpen(0x07);
/// Write Registers
// Register 07 - Seconds
uint8_t regSec = (tenSecond << 4) + singleSecond;
// Set Alarm Mask Bit
bitWrite(regSec, 7, alarm_mask & 0x01);
Wire.write(regSec);
// Register 08 - Minutes
uint8_t regMin = (tenMinute << 4) + singleMinute;
// Set Alarm Mask Bit
bitWrite(regMin, 7, ((alarm_mask) >> 1) & 0x01);
Wire.write(regMin);
// Register 09 - Hours
uint8_t hourVal;
if (hours >= 20) {
hourVal = 1;
hourVal <<= 1;
} else if (hours >= 10) {
hourVal = 1;
}
uint8_t regHour = (hourVal << 4) + singleHour;
// Set Alarm Mask Bit
bitWrite(regHour, 7, ((alarm_mask) >> 2) & 0x01);
Wire.write(regHour);
// Check if Date or Day - DY/DT
// Register 0A
if (((alarm_mask) >> 4) & 0x01) {
// Day
} else {
// Date
uint8_t regDate = (tenDate << 4) + singleDate;
// Set Alarm Mask Bit
bitWrite(regDate, 7, ((alarm_mask) >> 3) & 0x01);
// Set DT bit to zero to set Date of month check
regDate |= (1 << 6);
Wire.write(regDate);
}
Wire.endTransmission();
}
/**
*
*/
void DS3231::setAlarm2(uint8_t date, uint8_t day, uint8_t hours, uint8_t minutes, uint8_t alarm_mask) {
/// Calculate values
// date
uint8_t tenDate = date / 10;
uint8_t singleDate = date % 10;
// hours
uint8_t singleHour = hours % 10;
// minutes
uint8_t tenMinute = minutes / 10;
uint8_t singleMinute = minutes % 10;
// Navigate to Register 0B
goToRegisterOpen(0x0B);
/// Write Registers
// Register 0B - Minutes
uint8_t regMin = (tenMinute << 4) + singleMinute;
// Set Alarm Mask Bit
bitWrite(regMin, 7, alarm_mask & 0x01);
Wire.write(regMin);
// Register 0C - Hours
uint8_t hourVal;
if (hours >= 20) {
hourVal = 1;
hourVal <<= 1;
} else if (hours >= 10) {
hourVal = 1;
}
uint8_t regHour = (hourVal << 4) + singleHour;
// Set Alarm Mask Bit
bitWrite(regHour, 7, ((alarm_mask) >> 1) & 0x01);
Wire.write(regHour);
// Check if Date or Day - DY/DT
// Register 0A
if (((alarm_mask) >> 3) & 0x01) {
// Day
} else {
// Date
uint8_t regDate = (tenDate << 4) + singleDate;
// Set Alarm Mask Bit
bitWrite(regDate, 7, ((alarm_mask) >> 2) & 0x01);
// Set DT bit to zero to set Date of month check
regDate |= (1 << 6);
Wire.write(regDate);
}
Wire.endTransmission();
}
// === Temperature functions ===
/**
*
*/
double DS3231::getTemperature() {
double result;
// Temperature Register 1 (0x11) & 2 (0x12)
goToRegister(0x11);
Wire.requestFrom(DS3231_ADDRESS, (uint8_t) 2);
// Integer part
result = Wire.read();
uint8_t fractionPart = Wire.read();
fractionPart >>= 6;
return result + 0.25 * fractionPart;
}
/**
*
*/
inline double DS3231::getTemperatureFahrenheit() {
return (getTemperature() * 1.8) + 32;
}
/// ==== Square Wave functions ====
/**
* Activates the square-wave output on the INT/SQW pin
* This disables the possibility to use alarm interrupts
* The use alarm interrupts again, deactivateSquareWave() must be called first
*/
void DS3231::activateSquareWave() {
// Control Register
uint8_t regVal = getRegister(0x0E);
// Set Interrupt Control bit to 0 (INTCN)
regVal &= ~(1 << 2);
// Write new values to register
writeRegister(0x0E, regVal);
}
/**
* Deactivates the square-wave output on the INT/SQW pin.
* This enables the possibility to use alarm interrupts.
* To use the Square-Wave-output again, use activateSquareWave()
*/
void DS3231::deactivateSquareWave() {
// Control Register
uint8_t regVal = getRegister(0x0E);
// Set Interrupt Control bit to 1 (INTCN)
regVal |= (1 << 2);
// Write new values to register
writeRegister(0x0E, regVal);
}
/**
* Turns on Alarm 1
*/
inline void DS3231::turnOnAlarm1() {
turnOnAlarm(1);
}
/**
* Turns off Alarm 1
*/
inline void DS3231::turnOffAlarm1() {
turnOffAlarm(1);
}
/**
* Turns on Alarm 2
*/
inline void DS3231::turnOnAlarm2() {
turnOnAlarm(2);
}
/**
* Turns off Alarm 2
*/
inline void DS3231::turnOffAlarm2() {
turnOffAlarm(2);
}
/**
* Turns off the given alarm
*/
void DS3231::turnOffAlarm(uint8_t alarm) {
// no-op if user input is incorrect
if (alarm > 2) return;
// Control register
uint8_t regVal = getRegister(0x0E);
// Set Alarm Interrupt Enable bit to 0 (AXIE)
regVal &= ~(1 << (alarm - 1));
writeRegister(0x0E, regVal);
// Also clear Alarm Flag in Status register
clearAlarmFlag(alarm);
}
/**
* Turns on the given alarm
*/
void DS3231::turnOnAlarm(uint8_t alarm) {
// no-op if user input is incorrect
if (alarm > 2) return;
//
deactivateSquareWave();
// Control register
uint8_t regVal = getRegister(0x0E);
// Set Alarm Interrupt Enable bit to 1 (AXIE)
regVal |= (1 << (alarm - 1));
writeRegister(0x0E, regVal);
// Also clear Alarm Flag in Status register
clearAlarmFlag(alarm);
}
/**
* Returns the value of the given register.
* You do NOT need to navigate to the register beforehand!
*/
uint8_t DS3231::getRegister(uint8_t reg) {
// no-op if register is not in DS3231 range
if (reg > 0x12) return;
goToRegister(reg);
// Convert to uint8_t or we would get a compiler-warning
Wire.requestFrom(DS3231_ADDRESS, (uint8_t) 1);
return Wire.read();
}
/**
* Write the given byte to the given register
*/
void DS3231::writeRegister(uint8_t reg, uint8_t val) {
// no-op if register is not in DS3231 range
if (reg > 0x12) return;
goToRegisterOpen(reg);
Wire.write(val);
Wire.endTransmission();
}
/**
* Moves the I2C register-pointer to the given register
*/
void DS3231::goToRegister(uint8_t reg) {
// no-op if register is not in DS3231 range
if (reg > 0x12) return;
Wire.beginTransmission(DS3231_ADDRESS);
Wire.write(reg);
Wire.endTransmission();
}
/**
* Moves the I2C register-pointer to the given register (without closing the connection) on the DS3231
*/
void DS3231::goToRegisterOpen(uint8_t reg) {
// no-op if register is not in DS3231 range
if (reg > 0x12) return;
Wire.beginTransmission(DS3231_ADDRESS);
Wire.write(reg);
}
/**
*
*/
void DS3231::clearAlarmFlag(uint8_t alarm) {
// no-op if user input is incorrect
if (alarm > 2) return;
uint8_t reg = getRegister(0x0F);
reg &= ~(1 << (alarm - 1));
writeRegister(0x0F, reg);
}
/**
*
*/
inline void DS3231::resetAlarm(uint8_t alarm) {
clearAlarmFlag(alarm);
}