Skip to content

Commit 8d4bf84

Browse files
committed
6.6.0:
- AlarmTriggered functionality, @derbic7 idea - 32K Output control functions. As DS1307 lacks this pin, it's mapped to SqWG output. - Refresh functionality improved for DS3231 and DS3232; now it refreshes all data with less operations. Now any other function doesn't refresh data from RTC.
1 parent 01d38db commit 8d4bf84

File tree

4 files changed

+189
-65
lines changed

4 files changed

+189
-65
lines changed

contributors.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ David Brown - https://github.com/davidhbrown - Fix century bit on year read.
1111
WoBo68 - https://github.com/WoBo68 - Power lost support for DS1307.
1212
mrfaptastic - https://github.com/mrfaptastic - enableBattery support (DS3231 and DS3232)
1313
Eleksir - https://github.com/eleksir - DS3231 and DS3232 aging factor register get/set
14-
derbic7 - https://github.com/derbic7 - alarmDisable fix for alarm2
14+
derbic7 - https://github.com/derbic7 - alarmDisable fix for alarm2. Also AlarmTriggered functionality idea.
15+

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=uRTCLib
2-
version=6.5.0
2+
version=6.6.0
33
author=Naguissa <naguissa@foroelectro.net>
44
maintainer=Naguissa <naguissa@foroelectro.net>
55
sentence=Really tiny library to basic RTC functionality on Arduino. DS1307, DS3231 and DS3232 RTCs are supported. See https://github.com/Naguissa/uEEPROMLib for EEPROM support. Temperature, Alarms, SQWG, Power lost and RAM support.

src/uRTCLib.cpp

Lines changed: 159 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* @see <a href="https://www.foroelectro.net/librerias-arduino-ide-f29/rtclib-arduino-libreria-simple-y-eficaz-para-rtc-y-t95.html">https://www.foroelectro.net/librerias-arduino-ide-f29/rtclib-arduino-libreria-simple-y-eficaz-para-rtc-y-t95.html</a>
2525
* @see <a href="mailto:naguissa@foroelectro.net">naguissa@foroelectro.net</a>
2626
* @see <a href="https://github.com/Naguissa/uEEPROMLib">See uEEPROMLib for EEPROM support.</a>
27-
* @version 6.5.0
27+
* @version 6.6.0
2828
*/
2929

3030
#include <Arduino.h>
@@ -86,34 +86,40 @@ void uRTCLib::refresh() {
8686
// case URTCLIB_MODEL_DS3231: // Commented out because it's default mode
8787
// case URTCLIB_MODEL_DS3232: // Commented out because it's default mode
8888
default:
89-
URTCLIB_WIRE.requestFrom(_rtc_address, 15);
89+
URTCLIB_WIRE.requestFrom(_rtc_address, 19);
9090
break;
9191
}
92-
92+
// 0x00h
9393
_second = URTCLIB_WIRE.read() & 0b01111111;
9494
uRTCLIB_YIELD
9595
_second = uRTCLIB_bcdToDec(_second);
9696

97+
// 0x01h
9798
_minute = URTCLIB_WIRE.read() & 0b01111111;
9899
uRTCLIB_YIELD
99100
_minute = uRTCLIB_bcdToDec(_minute);
100101

102+
// 0x02h
101103
_hour = URTCLIB_WIRE.read() & 0b00111111;
102104
uRTCLIB_YIELD
103105
_hour = uRTCLIB_bcdToDec(_hour);
104106

107+
// 0x03h
105108
_dayOfWeek = URTCLIB_WIRE.read();
106109
uRTCLIB_YIELD
107110
_dayOfWeek = uRTCLIB_bcdToDec(_dayOfWeek);
108111

112+
// 0x04h
109113
_day = URTCLIB_WIRE.read();
110114
uRTCLIB_YIELD
111115
_day = uRTCLIB_bcdToDec(_day);
112116

117+
// 0x05h
113118
_month = URTCLIB_WIRE.read() & 0b00011111;
114119
uRTCLIB_YIELD
115120
_month = uRTCLIB_bcdToDec(_month);
116121

122+
// 0x06h
117123
_year = URTCLIB_WIRE.read();
118124
uRTCLIB_YIELD
119125
_year = uRTCLIB_bcdToDec(_year);
@@ -124,6 +130,7 @@ void uRTCLib::refresh() {
124130
switch (_model) {
125131
case URTCLIB_MODEL_DS1307:
126132
uint8_t status;
133+
// 0x07h
127134
status = URTCLIB_WIRE.read();
128135
if (status & 0b00010000) {
129136
_sqwg_mode = status & 0b10000000 ? URTCLIB_SQWG_OFF_1 : URTCLIB_SQWG_OFF_0;
@@ -157,22 +164,26 @@ void uRTCLib::refresh() {
157164
_a1_mode = URTCLIB_ALARM_TYPE_1_NONE;
158165
_a2_mode = URTCLIB_ALARM_TYPE_2_NONE;
159166

167+
// 0x07h
160168
_a1_second = URTCLIB_WIRE.read();
161169
uRTCLIB_YIELD
162170
_a1_mode = _a1_mode | ((_a1_second & 0b10000000) >> 7);
163171
_a1_second = uRTCLIB_bcdToDec((_a1_second & 0b01111111)); //parentheses for bitwise operation as argument for uRTCLIB_bcdToDec is required
164172
//otherwise wrong result will be returned by function
165173

174+
// 0x08h
166175
_a1_minute = URTCLIB_WIRE.read();
167176
uRTCLIB_YIELD
168177
_a1_mode = _a1_mode | ((_a1_minute & 0b10000000) >> 6);
169178
_a1_minute = uRTCLIB_bcdToDec((_a1_minute & 0b01111111));
170179

180+
// 0x09h
171181
_a1_hour = URTCLIB_WIRE.read();
172182
uRTCLIB_YIELD
173183
_a1_mode = _a1_mode | ((_a1_hour & 0b10000000) >> 5);
174184
_a1_hour = uRTCLIB_bcdToDec((_a1_hour & 0b00111111));
175185

186+
// 0x0Ah
176187
_a1_day_dow = URTCLIB_WIRE.read();
177188
uRTCLIB_YIELD
178189
_a1_mode = _a1_mode | ((_a1_day_dow & 0b10000000) >> 4);
@@ -182,18 +193,21 @@ void uRTCLib::refresh() {
182193
_a1_day_dow = _a1_day_dow & 0b00111111;
183194
_a1_day_dow = uRTCLIB_bcdToDec(_a1_day_dow);
184195

196+
// 0x0Bh
185197
_a2_minute = URTCLIB_WIRE.read();
186198
uRTCLIB_YIELD
187199
_a2_mode = _a2_mode | ((_a2_minute & 0b10000000) >> 6);
188200
_a2_minute = _a2_minute & 0b01111111;
189201
_a2_minute = uRTCLIB_bcdToDec(_a2_minute);
190202

203+
// 0x0Ch
191204
_a2_hour = URTCLIB_WIRE.read();
192205
uRTCLIB_YIELD
193206
_a2_mode = _a2_mode | ((_a2_hour & 0b10000000) >> 5);
194207
_a2_hour = _a2_hour & 0b00111111;
195208
_a2_hour = uRTCLIB_bcdToDec(_a2_hour);
196209

210+
// 0x0Dh
197211
_a2_day_dow = URTCLIB_WIRE.read();
198212
uRTCLIB_YIELD
199213
_a2_mode = _a2_mode | ((_a2_day_dow & 0b10000000) >> 4);
@@ -204,6 +218,7 @@ void uRTCLib::refresh() {
204218

205219

206220
// Control registers
221+
// 0x0Eh
207222
LSB = URTCLIB_WIRE.read();
208223
uRTCLIB_YIELD
209224

@@ -227,15 +242,34 @@ void uRTCLib::refresh() {
227242
_a2_mode = URTCLIB_ALARM_TYPE_2_NONE;
228243
}
229244

230-
// Temperature registers (11h-12h) get updated automatically every 64s
231-
URTCLIB_WIRE.beginTransmission(_rtc_address);
232-
URTCLIB_WIRE.write(0x11);
233-
URTCLIB_WIRE.endTransmission();
234-
URTCLIB_WIRE.requestFrom(_rtc_address, 2);
245+
246+
247+
// 0x0Fh
248+
LSB = URTCLIB_WIRE.read(); //Control
235249
uRTCLIB_YIELD
236250

251+
_lost_power = (bool) (LSB & 0x10000000b);
252+
_32k = (bool) (LSB & 0x00001000b);
253+
_a2_triggered_flag = (bool) (LSB & 0x00000010b);
254+
_a1_triggered_flag = (bool) (LSB & 0x00000001b);
255+
256+
257+
// 0x10h
258+
_aging = URTCLIB_WIRE.read(); //Aging
259+
uRTCLIB_YIELD
260+
if (_aging & 0b10000000) {
261+
_aging--;
262+
}
263+
264+
265+
// Temperature registers (11h-12h) get updated automatically every 64s
266+
267+
// 0x11h
237268
MSB = URTCLIB_WIRE.read(); //2's complement int portion
269+
uRTCLIB_YIELD
270+
// 0x12h
238271
LSB = URTCLIB_WIRE.read(); //fraction portion
272+
uRTCLIB_YIELD
239273
_temp = 0b0000000000000000 | (MSB << 2) | (LSB >> 6); // 8+2 bits, *25 is the same as number + 2bitdecimals * 100 in base 10
240274
if (MSB & 0b10000000) {
241275
_temp = (_temp | 0b1111110000000000);
@@ -260,35 +294,7 @@ void uRTCLib::refresh() {
260294
* @return True if power was lost (both power sources, VCC and VBAT)
261295
*/
262296
bool uRTCLib::lostPower() {
263-
uint8_t status;
264-
265-
switch (_model) {
266-
case URTCLIB_MODEL_DS1307:
267-
uRTCLIB_YIELD
268-
URTCLIB_WIRE.beginTransmission(_rtc_address);
269-
URTCLIB_WIRE.write(0X00);
270-
URTCLIB_WIRE.endTransmission();
271-
uRTCLIB_YIELD
272-
URTCLIB_WIRE.requestFrom(_rtc_address, 1);
273-
status = URTCLIB_WIRE.read();
274-
uRTCLIB_YIELD
275-
return ((status & 0B10000000) == 0B10000000);
276-
break;
277-
278-
// case URTCLIB_MODEL_DS3231: // Commented out because it's default mode
279-
// case URTCLIB_MODEL_DS3232: // Commented out because it's default mode
280-
default:
281-
uRTCLIB_YIELD
282-
URTCLIB_WIRE.beginTransmission(_rtc_address);
283-
URTCLIB_WIRE.write(0X0F);
284-
URTCLIB_WIRE.endTransmission();
285-
uRTCLIB_YIELD
286-
URTCLIB_WIRE.requestFrom(_rtc_address, 1);
287-
status = URTCLIB_WIRE.read();
288-
uRTCLIB_YIELD
289-
return ((status & 0B10000000) == 0B10000000);
290-
break;
291-
}
297+
return _lost_power;
292298
}
293299

294300
/**
@@ -299,9 +305,8 @@ bool uRTCLib::lostPower() {
299305
* Others have a 'OSF' Oscillator Stop Flag in Register 0Fh
300306
*/
301307
void uRTCLib::lostPowerClear() {
302-
303-
uint8_t status;
304-
308+
uint8_t status;
309+
_lost_power = false;
305310
switch (_model) {
306311
case URTCLIB_MODEL_DS1307:
307312
uRTCLIB_YIELD
@@ -1100,6 +1105,31 @@ uint8_t uRTCLib::alarmDayDow(const uint8_t alarm) {
11001105
}
11011106

11021107

1108+
1109+
/**
1110+
* \brief Checks if any alarm has been triggered
1111+
*
1112+
* @param alarm Alarm number:
1113+
* - #URTCLIB_ALARM_1
1114+
* - #URTCLIB_ALARM_2
1115+
* - #URTCLIB_ALARM_ANY
1116+
*
1117+
* @return Current stored day or dow. 0b11111111 means error.
1118+
*/
1119+
bool uRTCLib::alarmTriggered(const uint8_t alarm) {
1120+
if ((alarm == URTCLIB_ALARM_1 || alarm == URTCLIB_ALARM_ANY) && _a1_triggered_flag) {
1121+
return true;
1122+
}
1123+
if ((alarm == URTCLIB_ALARM_2 || alarm == URTCLIB_ALARM_ANY) && _a2_triggered_flag) {
1124+
return true;
1125+
}
1126+
return false;
1127+
}
1128+
1129+
1130+
1131+
1132+
11031133
/************** SQuare Wave Generator ****************/
11041134

11051135
/**
@@ -1341,26 +1371,7 @@ bool uRTCLib::ramWrite(const uint8_t address, byte data) {
13411371
* @return Aging register value on RTC, 2-complement recalculated (use as regular int8_t)
13421372
*/
13431373
int8_t uRTCLib::agingGet() {
1344-
int8_t ret = 0;
1345-
switch (_model) {
1346-
case URTCLIB_MODEL_DS3231:
1347-
case URTCLIB_MODEL_DS3232:
1348-
URTCLIB_WIRE.beginTransmission(_rtc_address);
1349-
uRTCLIB_YIELD
1350-
URTCLIB_WIRE.write(0x10);
1351-
uRTCLIB_YIELD
1352-
URTCLIB_WIRE.endTransmission();
1353-
uRTCLIB_YIELD
1354-
URTCLIB_WIRE.requestFrom(_rtc_address, 1);
1355-
uRTCLIB_YIELD
1356-
ret = URTCLIB_WIRE.read(); //2's complement portion
1357-
if (ret & 0b10000000) {
1358-
ret--;
1359-
}
1360-
break;
1361-
1362-
}
1363-
return ret;
1374+
return _aging;
13641375
}
13651376

13661377
/**
@@ -1417,4 +1428,92 @@ bool uRTCLib::agingSet(int8_t val) {
14171428
}
14181429

14191430

1431+
1432+
/**
1433+
* \brief Enables 32K pin output
1434+
*
1435+
* As DS1307 doen't have this functionality we map it to SqWG with 32K frequency
1436+
*/
1437+
bool uRTCLib::enable32KOut() {
1438+
_32k = true;
1439+
switch (_model) {
1440+
case URTCLIB_MODEL_DS1307: // As DS1307 doesn't have this pin, map it to SqWG at same frequency
1441+
return sqwgSetMode(URTCLIB_SQWG_32768H);
1442+
break;
1443+
1444+
// case URTCLIB_MODEL_DS3231: // Commented out because it's default mode
1445+
// case URTCLIB_MODEL_DS3232: // Commented out because it's default mode
1446+
default:
1447+
uint8_t status;
1448+
uRTCLIB_YIELD
1449+
URTCLIB_WIRE.beginTransmission(_rtc_address);
1450+
URTCLIB_WIRE.write(0X0F);
1451+
URTCLIB_WIRE.endTransmission();
1452+
uRTCLIB_YIELD
1453+
URTCLIB_WIRE.requestFrom(_rtc_address, 1);
1454+
status = URTCLIB_WIRE.read();
1455+
status |= 0b00001000;
1456+
uRTCLIB_YIELD
1457+
URTCLIB_WIRE.beginTransmission(_rtc_address);
1458+
uRTCLIB_YIELD
1459+
URTCLIB_WIRE.write(0X0F);
1460+
uRTCLIB_YIELD
1461+
URTCLIB_WIRE.write(status);
1462+
uRTCLIB_YIELD
1463+
URTCLIB_WIRE.endTransmission();
1464+
uRTCLIB_YIELD
1465+
return true;
1466+
break;
1467+
}
1468+
}
1469+
1470+
/**
1471+
* \brief Enables 32K pin output
1472+
*
1473+
* As DS1307 doen't have this functionality we map it to SqWG with 32K frequency
1474+
*/
1475+
bool uRTCLib::disable32KOut() {
1476+
_32k = false;
1477+
switch (_model) {
1478+
case URTCLIB_MODEL_DS1307: // As DS1307 doesn't have this pin, map it to SqWG at same frequency
1479+
return sqwgSetMode(URTCLIB_SQWG_OFF_0);
1480+
break;
1481+
1482+
// case URTCLIB_MODEL_DS3231: // Commented out because it's default mode
1483+
// case URTCLIB_MODEL_DS3232: // Commented out because it's default mode
1484+
default:
1485+
uint8_t status;
1486+
uRTCLIB_YIELD
1487+
URTCLIB_WIRE.beginTransmission(_rtc_address);
1488+
URTCLIB_WIRE.write(0X0F);
1489+
URTCLIB_WIRE.endTransmission();
1490+
uRTCLIB_YIELD
1491+
URTCLIB_WIRE.requestFrom(_rtc_address, 1);
1492+
status = URTCLIB_WIRE.read();
1493+
status &= 0b11110111;
1494+
uRTCLIB_YIELD
1495+
URTCLIB_WIRE.beginTransmission(_rtc_address);
1496+
uRTCLIB_YIELD
1497+
URTCLIB_WIRE.write(0X0F);
1498+
uRTCLIB_YIELD
1499+
URTCLIB_WIRE.write(status);
1500+
uRTCLIB_YIELD
1501+
URTCLIB_WIRE.endTransmission();
1502+
uRTCLIB_YIELD
1503+
return true;
1504+
break;
1505+
}
1506+
}
1507+
1508+
1509+
/**
1510+
* \brief Checks 32K pin output status
1511+
*
1512+
* As DS1307 doen't have this functionality we map it to SqWG with 32K frequency
1513+
*/
1514+
bool uRTCLib::status32KOut() {
1515+
return _32k;
1516+
}
1517+
1518+
14201519
/*** EEPROM functionality has been moved to separate library: https://github.com/Naguissa/uEEPROMLib ***/

0 commit comments

Comments
 (0)