forked from maniacbug/RTClib
-
Notifications
You must be signed in to change notification settings - Fork 20
/
RTC_DS3231.cpp
401 lines (316 loc) · 9.68 KB
/
RTC_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
// Based on Code by JeeLabs http://news.jeelabs.org/code/
// Released to the public domain! Enjoy!
//
// Modified and expanded by Red Byer 7/24/2013 to work with 3231 better
// www.redstoyland.com Find the code under "mizraith" on github
//
// See .h file for the additions
#if ARDUINO < 100
#include <WProgram.h>
#else
#include <Arduino.h>
#endif
#include <avr/pgmspace.h>
#include <Wire.h>
#include "RTClib.h"
#include "RTC_DS3231.h"
#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_DS3231 implementation
uint8_t RTC_DS3231::begin(void)
{
return 1;
}
uint8_t RTC_DS3231::isrunning(void)
{
Wire.beginTransmission(DS3231_ADDRESS);
Wire.SEND(DS3231_REG_STATUS_CTL);
Wire.endTransmission();
Wire.requestFrom(DS3231_ADDRESS, 1);
uint8_t ss = Wire.RECEIVE();
return !(ss>>7);
}
/**
* Set the datetime of the RTC
**/
void RTC_DS3231::adjust(const DateTime& dt)
{
//set the address pointer and then start writing
Wire.beginTransmission(DS3231_ADDRESS);
Wire.SEND( DS3231_REG_SECONDS ); // was just '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();
}
/**
* Get the datetime from the RTC
**/
DateTime RTC_DS3231::now()
{
//set the address pointer in preparation for read
Wire.beginTransmission(DS3231_ADDRESS);
Wire.SEND( DS3231_REG_SECONDS ); // was just '0'
Wire.endTransmission();
Wire.requestFrom(DS3231_ADDRESS, 7);
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);
}
/**
* Return temperature as a float in degrees C
* Data is 10bits provided in 2 bytes (11h and and 2bits in 12h)
* Resolution of 0.25C
* e.g. 00011001 01 = 25.25C
* 00011001 = 25
* 01 = 0.25 * 1 = .25
*
* This function has not been tested
*/
float RTC_DS3231::getTempAsFloat()
{
//set the address pointer in preparation for read
Wire.beginTransmission(DS3231_ADDRESS);
Wire.SEND( DS3231_REG_TEMP_MSB ); // was just '0'
Wire.endTransmission();
Wire.requestFrom(DS3231_ADDRESS, 2);
int8_t sig = Wire.RECEIVE(); //signed MSB
uint8_t fract = Wire.RECEIVE() & 0b11000000; //rest should be zeroes anyway.
fract = fract >> 6; // now in 2 lsb's
float temp = (float)fract * 0.25; // total up, .00, .25, .50
if(sig < 0) {
temp = sig - temp; // calculate the fract correctly
} else {
temp = sig + temp; // add 25 + .25 = 25.25
}
return temp;
}
/**
* Return temperature as a 2 bytes in degrees C
* MSB is the significant 00011001 = 25
* LSB is the mantissa 00011001 = .25
* Display by writing them out to the display MSB . LSB
*
* Data from the 3231 10bits provided in 2 bytes (11h and and 2bits in 12h)
* Resolution of 0.25C
* e.g. 00011001 01 = 25.25C
* 00011001 = 25
* 01 = 0.25 * 1 = .25
*
*/
int16_t RTC_DS3231::getTempAsWord()
{
//set the address pointer in preparation for read
Wire.beginTransmission(DS3231_ADDRESS);
Wire.SEND( DS3231_REG_TEMP_MSB ); // was just '0'
Wire.endTransmission();
Wire.requestFrom(DS3231_ADDRESS, 2);
int8_t sig = Wire.RECEIVE(); //signed MSB
uint8_t fract = Wire.RECEIVE() & 0b11000000; //rest should be zeroes anyway.
int16_t temp = sig; //put into lower byte
temp = temp << 8 ; //shift to upper byte
fract = fract >> 6; // shift upper 2 bits to lowest 2
fract = fract * 25; // multiply up
temp = temp + fract;
return temp;
}
/**
* Enable or disable the 32kHz pin. When 1 it
* will output 32768kHz.
*
*/
void RTC_DS3231::enable32kHz(uint8_t enable)
{
Wire.beginTransmission(DS3231_ADDRESS);
Wire.SEND( DS3231_REG_STATUS_CTL );
Wire.endTransmission();
// status register
Wire.requestFrom(DS3231_ADDRESS, 1);
uint8_t sreg = bcd2bin(Wire.RECEIVE()); // do we need to wrap in bcd2bin?
if (enable == true) {
sreg |= 0b00001000; // Enable EN32KHZ bit
} else {
sreg &= ~0b00001000; // else set EN32KHZ bit to 0
}
Wire.beginTransmission(DS3231_ADDRESS);
Wire.SEND( DS3231_REG_STATUS_CTL );
Wire.SEND(sreg);
Wire.endTransmission();
}
/**
* Force the temp sensor to convert the temp
* into digital code and execute the TCXO
* algorithm. The DS3231 normally does
* this every 64 seconds anyway.
*
* NOTE: This is a BLOCKING method. You have been warned.
*
* This method has not been fully debugged.
*/
void RTC_DS3231::forceTempConv(uint8_t block)
{
Wire.beginTransmission(DS3231_ADDRESS);
Wire.SEND( DS3231_REG_CONTROL );
Wire.endTransmission();
// control register
Wire.requestFrom(DS3231_ADDRESS, 1);
uint8_t creg = Wire.RECEIVE(); // do we need the bcd2bin
creg |= 0b00100000; // Write CONV bit
Wire.beginTransmission(DS3231_ADDRESS);
Wire.SEND(DS3231_REG_CONTROL);
Wire.SEND(creg);
Wire.endTransmission();
do
{
// Block until CONV is 0
Wire.beginTransmission(DS3231_ADDRESS);
Wire.SEND(DS3231_REG_CONTROL);
Wire.endTransmission();
Wire.requestFrom(DS3231_ADDRESS, 1);
} while ((block && (Wire.RECEIVE() & 0b00100000) != 0));
}
/**
* Enable or disable the output to the SQW pin.
* Send 1 or true to enable.
* This function does _not_ enable battery backed output
* which helps conserve battery life.
*
*/
void RTC_DS3231::SQWEnable(uint8_t enable)
{
Wire.beginTransmission(DS3231_ADDRESS);
Wire.SEND(DS3231_REG_CONTROL);
Wire.endTransmission();
// control register
Wire.requestFrom(DS3231_ADDRESS, 1);
uint8_t creg = Wire.RECEIVE(); //do we need the bcd2bin
if (enable == true) {
creg &= ~0b00000100; // Clear INTCN bit to output the square wave
} else {
creg |= 0b00000100; // Set INTCN to 1 -- disables SQW
}
Wire.beginTransmission(DS3231_ADDRESS);
Wire.SEND(DS3231_REG_CONTROL);
Wire.SEND(creg);
Wire.endTransmission();
}
/**
* Enable or disable the output to the SQW pin.
* Send 1 or true to enable.
* This method enables BOTH the square wave
* and the battery backed output.
*
*/
void RTC_DS3231::BBSQWEnable(uint8_t enable)
{
Wire.beginTransmission(DS3231_ADDRESS);
Wire.SEND(DS3231_REG_CONTROL);
Wire.endTransmission();
// control register
Wire.requestFrom(DS3231_ADDRESS, 1);
uint8_t creg = Wire.RECEIVE(); //do we need the bcd2bin
if (enable == true) {
creg |= 0b01000000; // Enable BBSQW if required so SQW continues to output (battery life reduction).
creg &= ~0b00000100; // Clear INTCN bit to output the square wave
} else {
creg &= ~0b01000000; // Set BBSQW 0 to disable battery back
//creg |= 0b00000100; // Set INTCN to 1 -- disables SQW
}
Wire.beginTransmission(DS3231_ADDRESS);
Wire.SEND(DS3231_REG_CONTROL);
Wire.SEND(creg);
Wire.endTransmission();
}
/**
* Set the output frequence of the squarewave SQW pin
*
* HINT: Use the defined frequencies in the .h file
*
*/
void RTC_DS3231::SQWFrequency(uint8_t freq)
{
Wire.beginTransmission(DS3231_ADDRESS);
Wire.SEND(DS3231_REG_CONTROL);
Wire.endTransmission();
// control register
Wire.requestFrom(DS3231_ADDRESS, 1);
uint8_t creg = Wire.RECEIVE();
creg &= ~0b00011000; // Set to 0
creg |= freq; // Set freq bits
Wire.beginTransmission(DS3231_ADDRESS);
Wire.SEND(DS3231_REG_CONTROL);
Wire.SEND(creg);
Wire.endTransmission();
}
uint8_t RTC_DS3231::getDayOfWeek( void ) {
Wire.beginTransmission(DS3231_ADDRESS);
Wire.SEND( DS3231_REG_DAYOFWEEK );
Wire.endTransmission();
// control register
Wire.requestFrom(DS3231_ADDRESS, 1);
uint8_t day = Wire.RECEIVE();
return day;
}
void RTC_DS3231::setDayOfWeek( uint8_t day) {
day = day & 0b0000011; //mask off all but two lower bigs
Wire.beginTransmission(DS3231_ADDRESS);
Wire.SEND( DS3231_REG_DAYOFWEEK );
Wire.SEND(day);
Wire.endTransmission();
}
void RTC_DS3231::getControlRegisterData(char &datastr) {
Wire.beginTransmission(DS3231_ADDRESS);
Wire.write( DS3231_REG_CONTROL );
Wire.endTransmission();
// control registers
Wire.requestFrom(DS3231_ADDRESS, 2);
uint8_t creg = Wire.RECEIVE(); // do we need the bcd2bin
uint8_t sreg = Wire.RECEIVE();
char cregstr[] = "00000000";
char sregstr[] = "00000000";
getBinaryString(creg, cregstr);
getBinaryString(sreg, sregstr);
strcpy(&datastr, "\n----- DS3231 Information -----\ncreg: ");
strcat(&datastr, cregstr);
strcat(&datastr, "\nsreg: ");
strcat(&datastr, sregstr);
strcat(&datastr, "\n------------------------------\n");
return;
}
/********************************************************
* UTILITY and WORKER METHODS
*********************************************************/
/**
* Takes one byte and loads up bytestr[8] with the value
* eg "8" loads bytestr with "00000111"
*/
void RTC_DS3231::getBinaryString(uint8_t byteval, char bytestr[])
{
uint8_t bitv;
int i = 0;
for (i = 0; i < 8; i++) {
bitv = (byteval >> i) & 1;
if (bitv == 0) {
bytestr[7-i] = '0';
} else {
bytestr[7-i] = '1';
}
}
}
// vim:ci:sw=4 sts=4 ft=cpp