-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRTClib.cpp
More file actions
executable file
·1089 lines (971 loc) · 32.7 KB
/
RTClib.cpp
File metadata and controls
executable file
·1089 lines (971 loc) · 32.7 KB
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Code by JeeLabs http://news.jeelabs.org/code/
// Released to the public domain! Enjoy!
#include <Wire.h>
#include <SPI.h> // used by DS3234
#include <avr/pgmspace.h>
#include "RTClib.h"
#define DS1307_ADDRESS 0x68
#define DS3231_ADDRESS 0x68
#define SECONDS_PER_DAY 86400L
#define SECONDS_FROM_1970_TO_2000 946684800
#if (ARDUINO >= 100)
#include <Arduino.h> // capital A so it is error prone on case-sensitive filesystems
#else
#include <WProgram.h>
#endif
int i = 0; //The new wire library needs to take an int when you are sending for the zero register
////////////////////////////////////////////////////////////////////////////////
// utility code, some of this could be exposed in the DateTime API if needed
const uint8_t daysInMonth [] PROGMEM = { 31,28,31,30,31,30,31,31,30,31,30,31 }; //has to be const or compiler compaints
// number of days since 2000/01/01, valid for 2001..2099
static uint16_t date2days(uint16_t y, uint8_t m, uint8_t d) {
if (y >= 2000)
y -= 2000;
uint16_t days = d;
for (uint8_t i = 1; i < m; ++i)
days += pgm_read_byte(daysInMonth + i - 1);
if (m > 2 && y % 4 == 0)
++days;
return days + 365 * y + (y + 3) / 4 - 1;
}
static long time2long(uint16_t days, uint8_t h, uint8_t m, uint8_t s) {
return ((days * 24L + h) * 60 + m) * 60 + s;
}
////////////////////////////////////////////////////////////////////////////////
// DateTime implementation - ignores time zones and DST changes
// NOTE: also ignores leap seconds, see http://en.wikipedia.org/wiki/Leap_second
DateTime::DateTime (uint32_t t) {
t -= SECONDS_FROM_1970_TO_2000; // bring to 2000 timestamp from 1970
ss = t % 60;
t /= 60;
mm = t % 60;
t /= 60;
hh = t % 24;
uint16_t days = t / 24;
uint8_t leap;
for (yOff = 0; ; ++yOff) {
leap = yOff % 4 == 0;
if (days < 365 + leap)
break;
days -= 365 + leap;
}
for (m = 1; ; ++m) {
uint8_t daysPerMonth = pgm_read_byte(daysInMonth + m - 1);
if (leap && m == 2)
++daysPerMonth;
if (days < daysPerMonth)
break;
days -= daysPerMonth;
}
d = days + 1;
}
DateTime::DateTime (uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t min, uint8_t sec) {
if (year >= 2000)
year -= 2000;
yOff = year;
m = month;
d = day;
hh = hour;
mm = min;
ss = sec;
}
static uint8_t conv2d(const char* p) {
uint8_t v = 0;
if ('0' <= *p && *p <= '9')
v = *p - '0';
return 10 * v + *++p - '0';
}
// A convenient constructor for using "the compiler's time":
// DateTime now (__DATE__, __TIME__);
// NOTE: using PSTR would further reduce the RAM footprint
DateTime::DateTime (const char* date, const char* time) {
// sample input: date = "Dec 26 2009", time = "12:34:56"
yOff = conv2d(date + 9);
// Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
switch (date[0]) {
case 'J': m = date[1] == 'a' ? 1 : m = date[2] == 'n' ? 6 : 7; break;
case 'F': m = 2; break;
case 'A': m = date[2] == 'r' ? 4 : 8; break;
case 'M': m = date[2] == 'r' ? 3 : 5; break;
case 'S': m = 9; break;
case 'O': m = 10; break;
case 'N': m = 11; break;
case 'D': m = 12; break;
}
d = conv2d(date + 4);
hh = conv2d(time);
mm = conv2d(time + 3);
ss = conv2d(time + 6);
}
uint8_t DateTime::dayOfWeek() const {
uint16_t day = date2days(yOff, m, d);
return (day + 6) % 7; // Jan 1, 2000 is a Saturday, i.e. returns 6
}
uint32_t DateTime::unixtime(void) const {
uint32_t t;
uint16_t days = date2days(yOff, m, d);
t = time2long(days, hh, mm, ss);
t += SECONDS_FROM_1970_TO_2000; // seconds from 1970 to 2000
return t;
}
////////////////////////////////////////////////////////////////////////////////
// By Coobro
const char* months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
// as a string
char* DateTime::toString(char* buf, int maxlen) const
{
snprintf(buf,maxlen,"%s %02u %04u %02u:%02u:%02u",
months[m-1],
d,
2000 + yOff,
hh,
mm,
ss
);
return buf;
}
// as a string
char* DateTime::toYMDString(char* buf, int maxlen) const
{
snprintf(buf,maxlen,"%04u/%02u/%02u %02u:%02u:%02u",
2000 + yOff,
m,
d,
hh,
mm,
ss
);
return buf;
}
void DateTime::operator+=(uint32_t additional)
{
DateTime after = DateTime( unixtime() + additional );
*this = after;
}
///////////////////////////////////////////////////////////////////////////////
// BCD conversion tools
static uint8_t bcd2bin (uint8_t val) { return val - 6 * (val >> 4); }
static uint8_t bin2bcd (uint8_t val) { return val + 6 * (val / 10); }
///////////////////////////////////////////////////////////////////////////////
// Arduino 1.0 addition
#if (ARDUINO >= 100)
#define WW_ Wire.write
#define WR_ Wire.read
#else
#define WW_ Wire.send
#define WR_ Wire.receive
#endif
#if ARDUINO < 100
#define SEND(x) send(x)
#define RECEIVE(x) receive(x)
#else
#define SEND(x) write(static_cast<uint8_t>(x))
#define RECEIVE(x) read(x)
#endif
////////////////////////////////////////////////////////////////////////////////
// RTC_DS1307 implementation
uint8_t RTC_DS1307::begin(void) {
return 1;
}
uint8_t RTC_DS1307::isrunning(void) {
Wire.beginTransmission(DS1307_ADDRESS);
WW_(i);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 1);
uint8_t ss = WR_();
return !(ss>>7);
}
void RTC_DS1307::adjust(const DateTime& dt) {
Wire.beginTransmission(DS1307_ADDRESS);
WW_(i);
WW_(bin2bcd(dt.second()));
WW_(bin2bcd(dt.minute()));
WW_(bin2bcd(dt.hour()));
WW_(bin2bcd(0));
WW_(bin2bcd(dt.day()));
WW_(bin2bcd(dt.month()));
WW_(bin2bcd(dt.year() - 2000));
WW_(i);
Wire.endTransmission();
}
DateTime RTC_DS1307::now() {
Wire.beginTransmission(DS1307_ADDRESS);
WW_(i);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
uint8_t ss = bcd2bin(WR_() & 0x7F);
uint8_t mm = bcd2bin(WR_());
uint8_t hh = bcd2bin(WR_());
WR_();
uint8_t d = bcd2bin(WR_());
uint8_t m = bcd2bin(WR_());
uint16_t y = bcd2bin(WR_()) + 2000;
return DateTime (y, m, d, hh, mm, ss);
}
uint8_t RTC_DS1307::readMemory(uint8_t offset, uint8_t* data, uint8_t length) {
uint8_t bytes_read = 0;
Wire.beginTransmission(DS1307_ADDRESS);
WW_(0x08 + offset);
Wire.endTransmission();
Wire.requestFrom((uint8_t)DS1307_ADDRESS, (uint8_t)length);
while (Wire.available() > 0 && bytes_read < length) {
data[bytes_read] = WR_();
bytes_read++;
}
return bytes_read;
}
uint8_t RTC_DS1307::writeMemory(uint8_t offset, uint8_t* data, uint8_t length) {
uint8_t bytes_written;
Wire.beginTransmission(DS1307_ADDRESS);
WW_(0x08 + offset);
#if (ARDUINO >= 100)
bytes_written = WW_(data, length);
Wire.endTransmission();
return bytes_written;
#else
WW_(data, length);
Wire.endTransmission();
return length;
#endif
}
////////////////////////////////////////////////////////////////////////////////
// RTC_DS3231 implementation
uint8_t RTC_DS3231::begin(void)
{
return 1;
}
uint8_t RTC_DS3231::isrunning(void)
{
Wire.beginTransmission(DS3231_ADDRESS);
Wire.SEND(0);
Wire.endTransmission();
Wire.requestFrom(DS3231_ADDRESS, 1);
uint8_t ss = Wire.RECEIVE();
return !(ss>>7);
}
void RTC_DS3231::adjust(const DateTime& dt)
{
Wire.beginTransmission(DS3231_ADDRESS);
Wire.SEND(0);
Wire.SEND(bin2bcd(dt.second()));
Wire.SEND(bin2bcd(dt.minute()));
Wire.SEND(bin2bcd(dt.hour()));
Wire.SEND(bin2bcd(0));
Wire.SEND(bin2bcd(dt.day()));
Wire.SEND(bin2bcd(dt.month()));
Wire.SEND(bin2bcd(dt.year() - 2000));
Wire.SEND(0);
Wire.endTransmission();
}
DateTime RTC_DS3231::now()
{
Wire.beginTransmission(DS3231_ADDRESS);
Wire.SEND(0);
Wire.endTransmission();
Wire.requestFrom(DS3231_ADDRESS, 19);
uint8_t ss = bcd2bin(Wire.RECEIVE() & 0x7F);
uint8_t mm = bcd2bin(Wire.RECEIVE());
uint8_t hh = bcd2bin(Wire.RECEIVE());
Wire.RECEIVE();
uint8_t d = bcd2bin(Wire.RECEIVE());
uint8_t m = bcd2bin(Wire.RECEIVE());
uint16_t y = bcd2bin(Wire.RECEIVE()) + 2000;
return DateTime (y, m, d, hh, mm, ss);
}
float RTC_DS3231::getTemperature() {
// Checks the internal thermometer on the DS3231 and returns the
// temperature as a floating-point value.
byte temp;
Wire.beginTransmission(DS3231_ADDRESS);
Wire.SEND(0x11);
Wire.endTransmission();
Wire.requestFrom(DS3231_ADDRESS, 2);
temp = Wire.RECEIVE(); // Here's the MSB
return float(temp) + 0.25*(Wire.RECEIVE()>>6);
}
void RTC_DS3231::getA1Time(byte& A1Day, byte& A1Hour, byte& A1Minute, byte& A1Second, byte& AlarmBits, bool& A1Dy, bool& A1h12, bool& A1PM) {
byte temp_buffer;
Wire.beginTransmission(DS3231_ADDRESS);
Wire.SEND(0x07);
Wire.endTransmission();
Wire.requestFrom(DS3231_ADDRESS, 4);
temp_buffer = Wire.RECEIVE(); // Get A1M1 and A1 Seconds
A1Second = bcd2bin(temp_buffer & 0b01111111);
// put A1M1 bit in position 0 of DS3231_AlarmBits.
AlarmBits = AlarmBits | (temp_buffer & 0b10000000)>>7;
temp_buffer = Wire.RECEIVE(); // Get A1M2 and A1 minutes
A1Minute = bcd2bin(temp_buffer & 0b01111111);
// put A1M2 bit in position 1 of DS3231_AlarmBits.
AlarmBits = AlarmBits | (temp_buffer & 0b10000000)>>6;
temp_buffer = Wire.RECEIVE(); // Get A1M3 and A1 Hour
// put A1M3 bit in position 2 of DS3231_AlarmBits.
AlarmBits = AlarmBits | (temp_buffer & 0b10000000)>>5;
// determine A1 12/24 mode
A1h12 = temp_buffer & 0b01000000;
if (A1h12) {
A1PM = temp_buffer & 0b00100000; // determine am/pm
A1Hour = bcd2bin(temp_buffer & 0b00011111); // 12-hour
} else {
A1Hour = bcd2bin(temp_buffer & 0b00111111); // 24-hour
}
temp_buffer = Wire.RECEIVE(); // Get A1M4 and A1 Day/Date
// put A1M3 bit in position 3 of DS3231_AlarmBits.
AlarmBits = AlarmBits | (temp_buffer & 0b10000000)>>4;
// determine A1 day or date flag
A1Dy = (temp_buffer & 0b01000000)>>6;
if (A1Dy) {
// alarm is by day of week, not date.
A1Day = bcd2bin(temp_buffer & 0b00001111);
} else {
// alarm is by date, not day of week.
A1Day = bcd2bin(temp_buffer & 0b00111111);
}
}
void RTC_DS3231::getA2Time(byte& A2Day, byte& A2Hour, byte& A2Minute, byte& AlarmBits, bool& A2Dy, bool& A2h12, bool& A2PM) {
byte temp_buffer;
Wire.beginTransmission(DS3231_ADDRESS);
Wire.SEND(0x0b);
Wire.endTransmission();
Wire.requestFrom(DS3231_ADDRESS, 3);
temp_buffer = Wire.RECEIVE(); // Get A2M2 and A2 Minutes
A2Minute = bcd2bin(temp_buffer & 0b01111111);
// put A2M2 bit in position 4 of DS3231_AlarmBits.
AlarmBits = AlarmBits | (temp_buffer & 0b10000000)>>3;
temp_buffer = Wire.RECEIVE(); // Get A2M3 and A2 Hour
// put A2M3 bit in position 5 of DS3231_AlarmBits.
AlarmBits = AlarmBits | (temp_buffer & 0b10000000)>>2;
// determine A2 12/24 mode
A2h12 = temp_buffer & 0b01000000;
if (A2h12) {
A2PM = temp_buffer & 0b00100000; // determine am/pm
A2Hour = bcd2bin(temp_buffer & 0b00011111); // 12-hour
} else {
A2Hour = bcd2bin(temp_buffer & 0b00111111); // 24-hour
}
temp_buffer = Wire.RECEIVE(); // Get A2M4 and A1 Day/Date
// put A2M4 bit in position 6 of DS3231_AlarmBits.
AlarmBits = AlarmBits | (temp_buffer & 0b10000000)>>1;
// determine A2 day or date flag
A2Dy = (temp_buffer & 0b01000000)>>6;
if (A2Dy) {
// alarm is by day of week, not date.
A2Day = bcd2bin(temp_buffer & 0b00001111);
} else {
// alarm is by date, not day of week.
A2Day = bcd2bin(temp_buffer & 0b00111111);
}
}
void RTC_DS3231::setAlarm1Simple(byte hour, byte minute) {
setA1Time(0, hour, minute, 00, 0b00001000, false, false, false);
}
void RTC_DS3231::setAlarm2Simple(byte hour, byte minute) {
setA2Time(0, hour, minute, 0b00001000, false, false, false);
}
void RTC_DS3231::setA1Time(byte A1Day, byte A1Hour, byte A1Minute, byte A1Second, byte AlarmBits, bool A1Dy, bool A1h12, bool A1PM) {
// Sets the alarm-1 date and time on the DS3231, using A1* information
byte temp_buffer;
Wire.beginTransmission(DS3231_ADDRESS);
Wire.SEND(0x07); // A1 starts at 07h
// Send A1 second and A1M1
Wire.SEND(bin2bcd(A1Second) | ((AlarmBits & 0b00000001) << 7));
// Send A1 Minute and A1M2
Wire.SEND(bin2bcd(A1Minute) | ((AlarmBits & 0b00000010) << 6));
// Figure out A1 hour
if (A1h12) {
// Start by converting existing time to h12 if it was given in 24h.
if (A1Hour > 12) {
// well, then, this obviously isn't a h12 time, is it?
A1Hour = A1Hour - 12;
A1PM = true;
}
if (A1PM) {
// Afternoon
// Convert the hour to BCD and add appropriate flags.
temp_buffer = bin2bcd(A1Hour) | 0b01100000;
} else {
// Morning
// Convert the hour to BCD and add appropriate flags.
temp_buffer = bin2bcd(A1Hour) | 0b01000000;
}
} else {
// Now for 24h
temp_buffer = bin2bcd(A1Hour);
}
temp_buffer = temp_buffer | ((AlarmBits & 0b00000100)<<5);
// A1 hour is figured out, send it
Wire.SEND(temp_buffer);
// Figure out A1 day/date and A1M4
temp_buffer = ((AlarmBits & 0b00001000)<<4) | bin2bcd(A1Day);
if (A1Dy) {
// Set A1 Day/Date flag (Otherwise it's zero)
temp_buffer = temp_buffer | 0b01000000;
}
Wire.SEND(temp_buffer);
// All done!
Wire.endTransmission();
}
void RTC_DS3231::setA2Time(byte A2Day, byte A2Hour, byte A2Minute, byte AlarmBits, bool A2Dy, bool A2h12, bool A2PM) {
// Sets the alarm-2 date and time on the DS3231, using A2* information
byte temp_buffer;
Wire.beginTransmission(DS3231_ADDRESS);
Wire.SEND(0x0b); // A1 starts at 0bh
// Send A2 Minute and A2M2
Wire.SEND(bin2bcd(A2Minute) | ((AlarmBits & 0b00010000) << 3));
// Figure out A2 hour
if (A2h12) {
// Start by converting existing time to h12 if it was given in 24h.
if (A2Hour > 12) {
// well, then, this obviously isn't a h12 time, is it?
A2Hour = A2Hour - 12;
A2PM = true;
}
if (A2PM) {
// Afternoon
// Convert the hour to BCD and add appropriate flags.
temp_buffer = bin2bcd(A2Hour) | 0b01100000;
} else {
// Morning
// Convert the hour to BCD and add appropriate flags.
temp_buffer = bin2bcd(A2Hour) | 0b01000000;
}
} else {
// Now for 24h
temp_buffer = bin2bcd(A2Hour);
}
// add in A2M3 bit
temp_buffer = temp_buffer | ((AlarmBits & 0b00100000)<<2);
// A2 hour is figured out, send it
Wire.SEND(temp_buffer);
// Figure out A2 day/date and A2M4
temp_buffer = ((AlarmBits & 0b01000000)<<1) | bin2bcd(A2Day);
if (A2Dy) {
// Set A2 Day/Date flag (Otherwise it's zero)
temp_buffer = temp_buffer | 0b01000000;
}
Wire.SEND(temp_buffer);
// All done!
Wire.endTransmission();
}
void RTC_DS3231::turnOnAlarm(byte Alarm) {
// turns on alarm number "Alarm". Defaults to 2 if Alarm is not 1.
byte temp_buffer = readControlByte(0);
// turn on control byte INTCN and A1IE or A2IE
if (Alarm == 1) {
temp_buffer = temp_buffer | 0b00000101;
} else {
temp_buffer = temp_buffer | 0b00000110;
}
writeControlByte(temp_buffer, 0);
}
void RTC_DS3231::turnOffAlarm(byte Alarm) {
// turns off alarm number "Alarm". Defaults to 2 if Alarm is not 1.
// Leaves interrupt pin alone.
byte temp_buffer = readControlByte(0);
// modify control byte A1IE or A2IE
if (Alarm == 1) {
temp_buffer = temp_buffer & 0b11111110;
} else {
temp_buffer = temp_buffer & 0b11111101;
}
writeControlByte(temp_buffer, 0);
}
bool RTC_DS3231::checkAlarmEnabled(byte Alarm) {
// Checks whether the given alarm is enabled.
byte result = 0x0;
byte temp_buffer = readControlByte(0);
// Check A1IE or A2IE
if (Alarm == 1) {
result = temp_buffer & 0b00000001;
} else {
result = temp_buffer & 0b00000010;
}
return result;
}
bool RTC_DS3231::checkIfAlarm(byte Alarm) {
// Checks whether alarm 1 or alarm 2 flag is on, returns T/F accordingly.
// Turns flag off, also.
// defaults to checking alarm 2, unless Alarm == 1.
byte result;
byte temp_buffer = readControlByte(1);
if (Alarm == 1) {
// Did alarm 1 go off?
result = temp_buffer & 0b00000001;
// clear flag
temp_buffer = temp_buffer & 0b11111110;
} else {
// Did alarm 2 go off?
result = temp_buffer & 0b00000010;
// clear flag
temp_buffer = temp_buffer & 0b11111101;
}
writeControlByte(temp_buffer, 1);
return result;
}
void RTC_DS3231::enableOscillator(bool TF, bool battery, byte frequency) {
// turns oscillator on or off. True is on, false is off.
// if battery is true, turns on even for battery-only operation,
// otherwise turns off if Vcc is off.
// frequency must be 0, 1, 2, or 3.
// 0 = 1 Hz
// 1 = 1.024 kHz
// 2 = 4.096 kHz
// 3 = 8.192 kHz (Default if frequency byte is out of range)
if (frequency > 3) frequency = 3;
// read control byte in, but zero out current state of RS2 and RS1.
byte temp_buffer = readControlByte(0) & 0b11100111;
if (battery) {
// turn on BBSQW flag
temp_buffer = temp_buffer | 0b01000000;
} else {
// turn off BBSQW flag
temp_buffer = temp_buffer & 0b10111111;
}
if (TF) {
// set ~EOSC to 0 and INTCN to zero.
temp_buffer = temp_buffer & 0b01111011;
} else {
// set ~EOSC to 1, leave INTCN as is.
temp_buffer = temp_buffer | 0b10000000;
}
// shift frequency into bits 3 and 4 and set.
frequency = frequency << 3;
temp_buffer = temp_buffer | frequency;
// And write the control bits
writeControlByte(temp_buffer, 0);
}
void RTC_DS3231::enable32kHz(bool TF) {
// turn 32kHz pin on or off
byte temp_buffer = readControlByte(1);
if (TF) {
// turn on 32kHz pin
temp_buffer = temp_buffer | 0b00001000;
} else {
// turn off 32kHz pin
temp_buffer = temp_buffer & 0b11110111;
}
writeControlByte(temp_buffer, 1);
}
bool RTC_DS3231::oscillatorCheck() {
// Returns false if the oscillator has been off for some reason.
// If this is the case, the time is probably not correct.
byte temp_buffer = readControlByte(1);
bool result = true;
if (temp_buffer & 0b10000000) {
// Oscillator Stop Flag (OSF) is set, so return false.
result = false;
}
return result;
}
byte RTC_DS3231::readControlByte(bool which) {
// Read selected control byte
// first byte (0) is 0x0e, second (1) is 0x0f
Wire.beginTransmission(DS3231_ADDRESS);
if (which) {
// second control byte
Wire.SEND(0x0f);
} else {
// first control byte
Wire.SEND(0x0e);
}
Wire.endTransmission();
Wire.requestFrom(DS3231_ADDRESS, 1);
return Wire.RECEIVE();
}
void RTC_DS3231::writeControlByte(byte control, bool which) {
// Write the selected control byte.
// which=false -> 0x0e, true->0x0f.
Wire.beginTransmission(DS3231_ADDRESS);
if (which) {
Wire.SEND(0x0f);
} else {
Wire.SEND(0x0e);
}
Wire.SEND(control);
Wire.endTransmission();
}
////////////////////////////////////////////////////////////////////////////////
// RTC_DS3234 implementation
// Registers we use
const int CONTROL_R = 0x0e;
const int CONTROL_W = 0x8e;
const int CONTROL_STATUS_R = 0x0f;
const int CONTROL_STATUS_W = 0x8f;
const int SECONDS_R = 0x00;
const int SECONDS_W = 0x80;
// Bits we use
const int EOSC = 7;
const int OSF = 7;
uint8_t RTC_DS3234::begin(void)
{
pinMode(cs_pin,OUTPUT);
cs(HIGH);
SPI.setBitOrder(MSBFIRST);
//Ugh! In order to get this to interop with other SPI devices,
//This has to be done in cs()
SPI.setDataMode(SPI_MODE1);
//Enable oscillator, disable square wave, alarms
cs(LOW);
SPI.transfer(CONTROL_W);
SPI.transfer(0x0);
cs(HIGH);
delay(1);
//Clear oscilator stop flag, 32kHz pin
cs(LOW);
SPI.transfer(CONTROL_STATUS_W);
SPI.transfer(0x0);
cs(HIGH);
delay(1);
return 1;
}
void RTC_DS3234::cs(int _value)
{
SPI.setDataMode(SPI_MODE1);
digitalWrite(cs_pin,_value);
}
uint8_t RTC_DS3234::isrunning(void)
{
cs(LOW);
SPI.transfer(CONTROL_R);
uint8_t ss = SPI.transfer(-1);
cs(HIGH);
return !(ss & _BV(OSF));
}
void RTC_DS3234::adjust(const DateTime& dt)
{
cs(LOW);
SPI.transfer(SECONDS_W);
SPI.transfer(bin2bcd(dt.second()));
SPI.transfer(bin2bcd(dt.minute()));
SPI.transfer(bin2bcd(dt.hour()));
SPI.transfer(bin2bcd(dt.dayOfWeek()));
SPI.transfer(bin2bcd(dt.day()));
SPI.transfer(bin2bcd(dt.month()));
SPI.transfer(bin2bcd(dt.year() - 2000));
cs(HIGH);
}
void RTC_DS3234::setA1Time(byte A1Day, byte A1Hour, byte A1Minute, byte A1Second, byte AlarmBits, bool A1Dy, bool A1h12, bool A1PM)
{
// Sets the alarm-1 date and time on the DS3234, using A1* information
byte temp_buffer;
cs(LOW);
// A1 starts at 87h
SPI.transfer(0x87);
// Send A1 second and A1M1
SPI.transfer(bin2bcd(A1Second) | ((AlarmBits & 0b00000001) << 7));
// Send A1 Minute and A1M2
SPI.transfer(bin2bcd(A1Minute) | ((AlarmBits & 0b00000010) << 6));
// Figure out A1 hour
if (A1h12) {
// Start by converting existing time to h12 if it was given in 24h.
if (A1Hour > 12) {
// well, then, this obviously isn't a h12 time, is it?
A1Hour = A1Hour - 12;
A1PM = true;
}
if (A1PM) {
// Afternoon
// Convert the hour to BCD and add appropriate flags.
temp_buffer = bin2bcd(A1Hour) | 0b01100000;
} else {
// Morning
// Convert the hour to BCD and add appropriate flags.
temp_buffer = bin2bcd(A1Hour) | 0b01000000;
}
} else {
// Now for 24h
temp_buffer = bin2bcd(A1Hour);
}
temp_buffer = temp_buffer | ((AlarmBits & 0b00000100)<<5);
// A1 hour is figured out, send it
SPI.transfer(temp_buffer);
// Figure out A1 day/date and A1M4
temp_buffer = ((AlarmBits & 0b00001000)<<4) | bin2bcd(A1Day);
if (A1Dy) {
// Set A1 Day/Date flag (Otherwise it's zero)
temp_buffer = temp_buffer | 0b01000000;
}
SPI.transfer(temp_buffer);
// All done!
cs(HIGH);
}
void RTC_DS3234::setAlarm1Simple(byte hour, byte minute) {
setA1Time(0, hour, minute, 00, 0b00001000, false, false, false);
}
void RTC_DS3234::getA1Time(byte& A1Day, byte& A1Hour, byte& A1Minute, byte& A1Second, byte& AlarmBits, bool& A1Dy, bool& A1h12, bool& A1PM) {
byte temp_buffer;
cs(LOW);
SPI.transfer(0x07);
temp_buffer = SPI.transfer(0x00); // Get A1M1 and A1 Seconds
A1Second = bcd2bin(temp_buffer & 0b01111111);
// put A1M1 bit in position 0 of DS3231_AlarmBits.
AlarmBits = AlarmBits | (temp_buffer & 0b10000000)>>7;
temp_buffer = SPI.transfer(0x00); // Get A1M2 and A1 minutes
A1Minute = bcd2bin(temp_buffer & 0b01111111);
// put A1M2 bit in position 1 of DS3231_AlarmBits.
AlarmBits = AlarmBits | (temp_buffer & 0b10000000)>>6;
temp_buffer = SPI.transfer(0x00); // Get A1M3 and A1 Hour
// put A1M3 bit in position 2 of DS3231_AlarmBits.
AlarmBits = AlarmBits | (temp_buffer & 0b10000000)>>5;
// determine A1 12/24 mode
A1h12 = temp_buffer & 0b01000000;
if (A1h12) {
A1PM = temp_buffer & 0b00100000; // determine am/pm
A1Hour = bcd2bin(temp_buffer & 0b00011111); // 12-hour
} else {
A1Hour = bcd2bin(temp_buffer & 0b00111111); // 24-hour
}
temp_buffer = SPI.transfer(0x00); // Get A1M4 and A1 Day/Date
// put A1M3 bit in position 3 of DS3231_AlarmBits.
AlarmBits = AlarmBits | (temp_buffer & 0b10000000)>>4;
// determine A1 day or date flag
A1Dy = (temp_buffer & 0b01000000)>>6;
if (A1Dy) {
// alarm is by day of week, not date.
A1Day = bcd2bin(temp_buffer & 0b00001111);
} else {
// alarm is by date, not day of week.
A1Day = bcd2bin(temp_buffer & 0b00111111);
}
cs(HIGH);
}
void RTC_DS3234::setA2Time(byte A2Day, byte A2Hour, byte A2Minute, byte AlarmBits, bool A2Dy, bool A2h12, bool A2PM) {
// Sets the alarm-2 date and time on the DS3231, using A2* information
byte temp_buffer;
cs(LOW);
SPI.transfer(0x8b); // A1 starts at 8bh
// Send A2 Minute and A2M2
SPI.transfer(bin2bcd(A2Minute) | ((AlarmBits & 0b00010000) << 3));
// Figure out A2 hour
if (A2h12) {
// Start by converting existing time to h12 if it was given in 24h.
if (A2Hour > 12) {
// well, then, this obviously isn't a h12 time, is it?
A2Hour = A2Hour - 12;
A2PM = true;
}
if (A2PM) {
// Afternoon
// Convert the hour to BCD and add appropriate flags.
temp_buffer = bin2bcd(A2Hour) | 0b01100000;
} else {
// Morning
// Convert the hour to BCD and add appropriate flags.
temp_buffer = bin2bcd(A2Hour) | 0b01000000;
}
} else {
// Now for 24h
temp_buffer = bin2bcd(A2Hour);
}
// add in A2M3 bit
temp_buffer = temp_buffer | ((AlarmBits & 0b00100000)<<2);
// A2 hour is figured out, send it
SPI.transfer(temp_buffer);
// Figure out A2 day/date and A2M4
temp_buffer = ((AlarmBits & 0b01000000)<<1) | bin2bcd(A2Day);
if (A2Dy) {
// Set A2 Day/Date flag (Otherwise it's zero)
temp_buffer = temp_buffer | 0b01000000;
}
SPI.transfer(temp_buffer);
// All done!
cs(HIGH);
}
void RTC_DS3234::setAlarm2Simple(byte hour, byte minute) {
setA2Time(0, hour, minute, 0b00001000, false, false, false);
}
void RTC_DS3234::getA2Time(byte& A2Day, byte& A2Hour, byte& A2Minute, byte& AlarmBits, bool& A2Dy, bool& A2h12, bool& A2PM) {
byte temp_buffer;
cs(LOW);
SPI.transfer(0x0b);
temp_buffer = SPI.transfer(0x00); // Get A2M2 and A2 Minutes
A2Minute = bcd2bin(temp_buffer & 0b01111111);
// put A2M2 bit in position 4 of DS3231_AlarmBits.
AlarmBits = AlarmBits | (temp_buffer & 0b10000000)>>3;
temp_buffer = SPI.transfer(0x00); // Get A2M3 and A2 Hour
// put A2M3 bit in position 5 of DS3231_AlarmBits.
AlarmBits = AlarmBits | (temp_buffer & 0b10000000)>>2;
// determine A2 12/24 mode
A2h12 = temp_buffer & 0b01000000;
if (A2h12) {
A2PM = temp_buffer & 0b00100000; // determine am/pm
A2Hour = bcd2bin(temp_buffer & 0b00011111); // 12-hour
} else {
A2Hour = bcd2bin(temp_buffer & 0b00111111); // 24-hour
}
temp_buffer = SPI.transfer(0x00); // Get A2M4 and A1 Day/Date
// put A2M4 bit in position 6 of DS3231_AlarmBits.
AlarmBits = AlarmBits | (temp_buffer & 0b10000000)>>1;
// determine A2 day or date flag
A2Dy = (temp_buffer & 0b01000000)>>6;
if (A2Dy) {
// alarm is by day of week, not date.
A2Day = bcd2bin(temp_buffer & 0b00001111);
} else {
// alarm is by date, not day of week.
A2Day = bcd2bin(temp_buffer & 0b00111111);
}
cs(HIGH);
}
void RTC_DS3234::turnOnAlarm(byte Alarm) {
// turns on alarm number "Alarm". Defaults to 2 if Alarm is not 1.
byte temp_buffer = readControlByte(0);
// modify control byte
if (Alarm == 1) {
temp_buffer = temp_buffer | 0b00000101;
} else {
temp_buffer = temp_buffer | 0b00000110;
}
writeControlByte(temp_buffer, 0);
}
void RTC_DS3234::turnOffAlarm(byte Alarm) {
// turns off alarm number "Alarm". Defaults to 2 if Alarm is not 1.
// Leaves interrupt pin alone.
byte temp_buffer = readControlByte(0);
// modify control byte
if (Alarm == 1) {
temp_buffer = temp_buffer & 0b11111110;
} else {
temp_buffer = temp_buffer & 0b11111101;
}
writeControlByte(temp_buffer, 0);
}
bool RTC_DS3234::checkAlarmEnabled(byte Alarm) {
// Checks whether the given alarm is enabled.
byte result = 0x0;
byte temp_buffer = readControlByte(0);
if (Alarm == 1) {
result = temp_buffer & 0b00000001;
} else {
result = temp_buffer & 0b00000010;
}
return result;
}
bool RTC_DS3234::checkIfAlarm(byte Alarm) {
// Checks whether alarm 1 or alarm 2 flag is on, returns T/F accordingly.
// Turns flag off, also.
// defaults to checking alarm 2, unless Alarm == 1.
byte result;
byte temp_buffer = readControlByte(1);
if (Alarm == 1) {
// Did alarm 1 go off?
result = temp_buffer & 0b00000001;
// clear flag
temp_buffer = temp_buffer & 0b11111110;
} else {
// Did alarm 2 go off?
result = temp_buffer & 0b00000010;
// clear flag
temp_buffer = temp_buffer & 0b11111101;
}
writeControlByte(temp_buffer, 1);
return result;
}
DateTime RTC_DS3234::now()
{
cs(LOW);
SPI.transfer(SECONDS_R);
uint8_t ss = bcd2bin(SPI.transfer(-1) & 0x7F);
uint8_t mm = bcd2bin(SPI.transfer(-1));
uint8_t hh = bcd2bin(SPI.transfer(-1));
SPI.transfer(-1);
uint8_t d = bcd2bin(SPI.transfer(-1));
uint8_t m = bcd2bin(SPI.transfer(-1));
uint16_t y = bcd2bin(SPI.transfer(-1)) + 2000;
cs(HIGH);
return DateTime (y, m, d, hh, mm, ss);
}
void RTC_DS3234::enableOscillator(bool TF, bool battery, byte frequency) {
// turns oscillator on or off. True is on, false is off.
// if battery is true, turns on even for battery-only operation,
// otherwise turns off if Vcc is off.
// frequency must be 0, 1, 2, or 3.
// 0 = 1 Hz
// 1 = 1.024 kHz
// 2 = 4.096 kHz
// 3 = 8.192 kHz (Default if frequency byte is out of range)
// Acronyms:
// BBSQW - Battery-Backed Square-Wave Enable
// ~EOSC - Enable Oscillator
// INTCN - Interrupt Control
if (frequency > 3) frequency = 3;
// read control byte in, but zero out current state of RS2 and RS1.