uRTCLib https://github.com/Naguissa/uRTCLib

Loading...
Searching...
No Matches
uRTCLib.h
Go to the documentation of this file.
1
36#ifndef URTCLIB
40 #define URTCLIB
41 #include "Arduino.h"
42 #ifndef URTCLIB_WIRE
43 #if defined(ARDUINO_attiny) || defined(ARDUINO_AVR_ATTINYX4) || defined(ARDUINO_AVR_ATTINYX5) || defined(ARDUINO_AVR_ATTINYX7) || defined(ARDUINO_AVR_ATTINYX8) || defined(ARDUINO_AVR_ATTINYX61) || defined(ARDUINO_AVR_ATTINY43) || defined(ARDUINO_AVR_ATTINY828) || defined(ARDUINO_AVR_ATTINY1634) || defined(ARDUINO_AVR_ATTINYX313)
44 #include <TinyWireM.h> // I2C Master lib for ATTinys which use USI
45 #define URTCLIB_WIRE TinyWireM
46 #else
47 #include <Wire.h>
48 #define URTCLIB_WIRE Wire
49 #endif
50 #endif
51
57 #define URTCLIB_ADDRESS 0x68
58
59 /************ MODELS ***********/
63 #define URTCLIB_MODEL_DS1307 1
67 #define URTCLIB_MODEL_DS3231 2
71 #define URTCLIB_MODEL_DS3232 3
72
73
74 /************ WEEK DAYS ***********/
78 #define URTCLIB_WEEKDAY_SUNDAY 1
82 #define URTCLIB_WEEKDAY_MONDAY 2
86 #define URTCLIB_WEEKDAY_TUESDAY 3
90 #define URTCLIB_WEEKDAY_WEDNESDAY 4
94 #define URTCLIB_WEEKDAY_THURSDAY 5
98 #define URTCLIB_WEEKDAY_FRIDAY 6
102 #define URTCLIB_WEEKDAY_SATURDAY 7
103
104
105
106 /************ ALARM SELECTION: ***********/
107 //Note: Not valid for DS1307!
108
123 #define URTCLIB_ALARM_TYPE_1_NONE 0b00000000
124
128 #define URTCLIB_ALARM_TYPE_1_ALL_S 0b00101111
129
133 #define URTCLIB_ALARM_TYPE_1_FIXED_S 0b00101110
134
138 #define URTCLIB_ALARM_TYPE_1_FIXED_MS 0b00101100
139
143 #define URTCLIB_ALARM_TYPE_1_FIXED_HMS 0b00101000
144
148 #define URTCLIB_ALARM_TYPE_1_FIXED_DHMS 0b00100000
149
153 #define URTCLIB_ALARM_TYPE_1_FIXED_DOWHMS 0b00110000
154
169 #define URTCLIB_ALARM_TYPE_2_NONE 0b10000000
170
174 #define URTCLIB_ALARM_TYPE_2_ALL_M 0b10101110
175
179 #define URTCLIB_ALARM_TYPE_2_FIXED_M 0b10101100
180
184 #define URTCLIB_ALARM_TYPE_2_FIXED_HM 0b10101000
185
189 #define URTCLIB_ALARM_TYPE_2_FIXED_DHM 0b10100000
190
194 #define URTCLIB_ALARM_TYPE_2_FIXED_DOWHM 0b10110000
195
196
200 #define URTCLIB_ALARM_1 URTCLIB_ALARM_TYPE_1_NONE
201
205 #define URTCLIB_ALARM_2 URTCLIB_ALARM_TYPE_2_NONE
206
210 #define URTCLIB_ALARM_ANY 0b01010101
211
212
213 /************ SQWG SELECTION: ***********/
214
220 #define URTCLIB_SQWG_OFF_0 0b11111111
221
225 #define URTCLIB_SQWG_OFF_1 0b11111110
226
230 #define URTCLIB_SQWG_1H 0b00000000
231
237 #define URTCLIB_SQWG_1024H 0b00001000
238
242 #define URTCLIB_SQWG_4096H 0b00010000
243
247 #define URTCLIB_SQWG_8192H 0b00011000
248
254 #define URTCLIB_SQWG_32768H 0b00000011
255
256
257 /************ TEMPERATURE ***********/
263 #define URTCLIB_TEMP_ERROR 32767
264
265
266 /************ MISC ***********/
267
268
272 #define uRTCLIB_decToBcd(val) ((uint8_t) ((val / 10 * 16) + (val % 10)))
273
277 #define uRTCLIB_bcdToDec(val) ((uint8_t) ((val / 16 * 10) + (val % 16)))
278
279 // ESP yield function (ESP32 has no need for that on dual core, but it has on single core version)
280 #if ARDUINO_ARCH_ESP8266
286 #define uRTCLIB_YIELD yield();
287 #else
288 #if ARDUINO_ARCH_ESP32
294 #define uRTCLIB_YIELD yield();
295 #else
296 #define uRTCLIB_YIELD
297 #endif
298 #endif
299
300 class uRTCLib {
301 public:
302 /******* Constructors *******/
306 uRTCLib();
312 uRTCLib(const int);
322 uRTCLib(const int, const uint8_t);
323
324 /******* RTC functions ********/
330 bool refresh();
336 uint8_t second();
342 uint8_t minute();
348 uint8_t hour();
358 uint8_t hourModeAndAmPm();
364 uint8_t day();
370 uint8_t month();
376 uint8_t year();
389 uint8_t dayOfWeek();
399 int16_t temp();
418 void set(const uint8_t, const uint8_t, const uint8_t, const uint8_t, const uint8_t, const uint8_t, const uint8_t);
427 void set_12hour_mode(const bool);
433 void set_rtc_address(const int);
442 void set_model(const uint8_t);
451 uint8_t model();
452
453 /******* Power ********/
466 bool getEOSCFlag();
480 bool lostPower();
488 void lostPowerClear();
504 bool enableBattery(); // Only DS3231 and DS3232.
511 bool disableBattery(); // Only DS3231 and DS3232.
512
513
514 /******** Alarms ************/
541 bool alarmSet(const uint8_t, const uint8_t, const uint8_t, const uint8_t, const uint8_t); // Seconds will be ignored on Alarm 2
551 bool alarmDisable(const uint8_t);
561 bool alarmClearFlag(const uint8_t);
587 uint8_t alarmMode(const uint8_t);
597 uint8_t alarmSecond(const uint8_t);
607 uint8_t alarmMinute(const uint8_t);
617 uint8_t alarmHour(const uint8_t);
627 uint8_t alarmDayDow(const uint8_t);
642 bool alarmTriggered(const uint8_t);
643
644 /*********** SQWG ************/
659 uint8_t sqwgMode();
672 bool sqwgSetMode(const uint8_t);
673
674
675 /************ RAM *************/
676 // Only DS1307 and DS3232.
677 // DS1307: Addresses 08h to 3Fh so we offset 08h positions and limit to 38h as maximum address
678 // DS3232: Addresses 14h to FFh so we offset 14h positions and limit to EBh as maximum address
686 byte ramRead(const uint8_t);
695 bool ramWrite(const uint8_t, byte);
696
697 /************ Aging *************/
698 // Only DS3231 and DS3232. Address 0x10h
704 int8_t agingGet();
712 bool agingSet(int8_t);
713
714 /************ 32K Generator pin *************/
715 // Only DS3231 and DS3232. On DS1307 we map it to SqWG
722 bool enable32KOut();
728 bool disable32KOut();
734 bool status32KOut();
735
736
737 private:
738 // Address
739 int _rtc_address = URTCLIB_ADDRESS;
740
741 // RTC read data
742 uint8_t _second = 0;
743 uint8_t _minute = 0;
744 uint8_t _hour = 0;
745 uint8_t _day = 0;
746 uint8_t _month = 0;
747 uint8_t _year = 0;
748 uint8_t _dayOfWeek = 0;
749 int16_t _temp = 9999;
750
751 // Model, for alarms and RAM
752 uint8_t _model = URTCLIB_MODEL_DS3232;
753
754 // Alarms:
755 uint8_t _a1_mode = URTCLIB_ALARM_TYPE_1_NONE;
756 uint8_t _a1_second = 0;
757 uint8_t _a1_minute = 0;
758 uint8_t _a1_hour = 0;
759 uint8_t _a1_day_dow = 0;
760 //bool _a1_triggered_flag = _controlStatus LSB Bit 0
761
762 uint8_t _a2_mode = URTCLIB_ALARM_TYPE_2_NONE;
763 uint8_t _a2_minute = 0;
764 uint8_t _a2_hour = 0;
765 uint8_t _a2_day_dow = 0;
766 // bool _a2_triggered_flag = _controlStatus Bit 1
767
768 // Aging
769 int8_t _aging = 0;
770
771 // SQWG
772 uint8_t _sqwg_mode = URTCLIB_SQWG_OFF_1;
773
774 // Keep record of various Flags
775 // _controlStatus MSB Bit 7 _lost_power = (bool) (_controlStatus & 0b10000000);
776 // _controlStatus Bit 6 _eosc = (bool) (_controlStatus & 0b01000000);
777 // _controlStatus Bit 5 _12hrMode = (bool) (_controlStatus & 0b00100000);
778 // _controlStatus Bit 4 _pmNotAm = (bool) (_controlStatus & 0b00010000); // am or pm if 12 hour mode
779 // _controlStatus Bit 3 _32k = (bool) (_controlStatus & 0b00001000);
780 // _controlStatus Bit 2
781 // _controlStatus Bit 1 _a2_triggered_flag = (bool) (_controlStatus & 0b00000010);
782 // _controlStatus LSB Bit 0 _a1_triggered_flag = (bool) (_controlStatus & 0b00000001);
783 uint8_t _controlStatus = 0x00;
784
785 };
786
787#endif
788
789
uRTCLib
DS1307, DS3231 and DS3232 RTCs basic library.
Definition uRTCLib.h:300
uRTCLib::enable32KOut
bool enable32KOut()
Enables 32K pin output.
Definition uRTCLib.cpp:1598
uRTCLib::second
uint8_t second()
Returns actual second.
Definition uRTCLib.cpp:546
uRTCLib::set_rtc_address
void set_rtc_address(const int)
Sets RTC i2 addres.
Definition uRTCLib.cpp:638
uRTCLib::set_model
void set_model(const uint8_t)
Sets RTC Model.
Definition uRTCLib.cpp:651
uRTCLib::set_12hour_mode
void set_12hour_mode(const bool)
Set clock in 12 or 24 hour mode 12 hour mode has 1-12 hours and AM or PM flag 24 hour mode has 0-23 h...
Definition uRTCLib.cpp:723
uRTCLib::status32KOut
bool status32KOut()
Checks 32K pin output status.
Definition uRTCLib.cpp:1677
uRTCLib::disable32KOut
bool disable32KOut()
Disable 32K pin output.
Definition uRTCLib.cpp:1637
uRTCLib::alarmClearFlag
bool alarmClearFlag(const uint8_t)
Clears an alarm flag.
Definition uRTCLib.cpp:1036
uRTCLib::alarmSet
bool alarmSet(const uint8_t, const uint8_t, const uint8_t, const uint8_t, const uint8_t)
Sets any alarm.
Definition uRTCLib.cpp:821
uRTCLib::month
uint8_t month()
Returns actual month.
Definition uRTCLib.cpp:603
uRTCLib::temp
int16_t temp()
Returns actual temperature.
Definition uRTCLib.cpp:534
uRTCLib::alarmTriggered
bool alarmTriggered(const uint8_t)
Checks if any alarm has been triggered.
Definition uRTCLib.cpp:1277
uRTCLib::alarmDayDow
uint8_t alarmDayDow(const uint8_t)
Returns actual alarm day or DOW.
Definition uRTCLib.cpp:1239
uRTCLib::day
uint8_t day()
Returns actual day.
Definition uRTCLib.cpp:594
uRTCLib::alarmHour
uint8_t alarmHour(const uint8_t)
Returns actual alarm hour.
Definition uRTCLib.cpp:1207
uRTCLib::lostPower
bool lostPower()
Returns lost power VBAT staus.
Definition uRTCLib.cpp:343
uRTCLib::ramWrite
bool ramWrite(const uint8_t, byte)
Writes a byte to RTC RAM.
Definition uRTCLib.cpp:1497
uRTCLib::alarmDisable
bool alarmDisable(const uint8_t)
Disables an alarm.
Definition uRTCLib.cpp:980
uRTCLib::set
void set(const uint8_t, const uint8_t, const uint8_t, const uint8_t, const uint8_t, const uint8_t, const uint8_t)
Sets RTC datetime data.
Definition uRTCLib.cpp:685
uRTCLib::alarmMode
uint8_t alarmMode(const uint8_t)
Returns actual alarm mode.
Definition uRTCLib.cpp:1110
uRTCLib::uRTCLib
uRTCLib()
Constructor.
Definition uRTCLib.cpp:51
uRTCLib::enableBattery
bool enableBattery()
Enable VBAT operation when VCC power is lost.
Definition uRTCLib.cpp:420
uRTCLib::sqwgMode
uint8_t sqwgMode()
Changes SQWG mode, including turning it off.
Definition uRTCLib.cpp:1444
uRTCLib::dayOfWeek
uint8_t dayOfWeek()
Returns actual Day Of Week.
Definition uRTCLib.cpp:628
uRTCLib::hour
uint8_t hour()
Returns actual hour.
Definition uRTCLib.cpp:565
uRTCLib::agingSet
bool agingSet(int8_t)
Sets aging value on the RTC.
Definition uRTCLib.cpp:1544
uRTCLib::agingGet
int8_t agingGet()
Reads actual aging value on the RTC.
Definition uRTCLib.cpp:1533
uRTCLib::alarmMinute
uint8_t alarmMinute(const uint8_t)
Returns actual alarm minute.
Definition uRTCLib.cpp:1174
uRTCLib::lostPowerClear
void lostPowerClear()
Clears lost power VBAT staus.
Definition uRTCLib.cpp:354
uRTCLib::alarmSecond
uint8_t alarmSecond(const uint8_t)
Returns actual alarm second.
Definition uRTCLib.cpp:1142
uRTCLib::sqwgSetMode
bool sqwgSetMode(const uint8_t)
Gets current SQWG mode.
Definition uRTCLib.cpp:1309
uRTCLib::getEOSCFlag
bool getEOSCFlag()
Returns Enable Oscillator Flag.
Definition uRTCLib.cpp:326
uRTCLib::disableBattery
bool disableBattery()
Disable VBAT operation when VCC power is lost.
Definition uRTCLib.cpp:477
uRTCLib::hourModeAndAmPm
uint8_t hourModeAndAmPm()
Returns whether clock is in 12 or 24 hour mode and AM or PM if in 12 hour mode 0 = 24 hour mode (0-23...
Definition uRTCLib.cpp:578
uRTCLib::year
uint8_t year()
Returns actual year.
Definition uRTCLib.cpp:612
uRTCLib::minute
uint8_t minute()
Returns actual minute.
Definition uRTCLib.cpp:555
uRTCLib::model
uint8_t model()
Gets RTC Model.
Definition uRTCLib.cpp:663
uRTCLib::ramRead
byte ramRead(const uint8_t)
Reads a byte from RTC RAM.
Definition uRTCLib.cpp:1459
uRTCLib::refresh
bool refresh()
Refresh data from HW RTC.
Definition uRTCLib.cpp:81
URTCLIB_ADDRESS
#define URTCLIB_ADDRESS
Default RTC I2C address.
Definition uRTCLib.h:57
URTCLIB_MODEL_DS3232
#define URTCLIB_MODEL_DS3232
Model definition, DS3232.
Definition uRTCLib.h:71
URTCLIB_SQWG_OFF_1
#define URTCLIB_SQWG_OFF_1
SQWG OFF, keeps output hight.
Definition uRTCLib.h:225
URTCLIB_ALARM_TYPE_1_NONE
#define URTCLIB_ALARM_TYPE_1_NONE
Alarm 1 - Disabled.
Definition uRTCLib.h:123
URTCLIB_ALARM_TYPE_2_NONE
#define URTCLIB_ALARM_TYPE_2_NONE
Alarm 2 - Disabled.
Definition uRTCLib.h:169