Erriez DS3231 high precision I2C RTC library for Arduino  2.0.0
This is a DS3231 high precision I2C Real Time Clock library for Arduino by Erriez.
ErriezDS3231.h
Go to the documentation of this file.
1 /*
2  * MIT License
3  *
4  * Copyright (c) 2020 Erriez
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
33 #ifndef ERRIEZ_DS3231_H_
34 #define ERRIEZ_DS3231_H_
35 
36 #include <stdint.h>
37 #include <time.h>
38 
40 #define DS3231_REG_SECONDS 0x00
41 #define DS3231_REG_MINUTES 0x01
42 #define DS3231_REG_HOURS 0x02
43 #define DS3231_REG_DAY_WEEK 0x03
44 #define DS3231_REG_DAY_MONTH 0x04
45 #define DS3231_REG_MONTH 0x05
46 #define DS3231_REG_YEAR 0x06
47 
48 #define DS3231_REG_ALARM1_SEC 0x07
49 #define DS3231_REG_ALARM1_MIN 0x08
50 #define DS3231_REG_ALARM1_HOUR 0x09
51 #define DS3231_REG_ALARM1_DD 0x0A
52 #define DS3231_REG_ALARM2_MIN 0x0B
53 #define DS3231_REG_ALARM2_HOUR 0x0C
54 #define DS3231_REG_ALARM2_DD 0x0D
55 
56 #define DS3231_REG_CONTROL 0x0E
57 #define DS3231_REG_STATUS 0x0F
58 #define DS3231_REG_AGING_OFFSET 0x10
59 #define DS3231_REG_TEMP_MSB 0x11
60 #define DS3231_REG_TEMP_LSB 0x12
61 
62 #define DS3231_NUM_REGS 19
64 
65 #define DS3231_HOUR_12H_24H 6
67 #define DS3231_HOUR_AM_PM 5
68 
69 #define DS3231_MONTH_CENTURY 7
70 
71 #define DS3231_CTRL_EOSC 7
72 #define DS3231_CTRL_BBSQW 6
73 #define DS3231_CTRL_CONV 5
74 #define DS3231_CTRL_RS2 4
75 #define DS3231_CTRL_RS1 3
76 #define DS3231_CTRL_INTCN 2
77 #define DS3231_CTRL_A2IE 1
78 #define DS3231_CTRL_A1IE 0
79 
80 #define DS3231_STAT_OSF 7
81 #define DS3231_STAT_EN32KHZ 3
82 #define DS3231_STAT_BSY 2
83 #define DS3231_STAT_A2F 1
84 #define DS3231_STAT_A1F 0
85 
86 #define DS3231_A1M1 7
87 #define DS3231_A1M2 7
88 #define DS3231_A1M3 7
89 #define DS3231_A1M4 7
90 #define DS3231_A2M2 7
91 #define DS3231_A2M3 7
92 #define DS3231_A2M4 7
93 #define DS3231_DYDT 6
94 
95 #define DS3231_ADDR (0xD0 >> 1)
97 
99 #define SECONDS_FROM_1970_TO_2000 946684800
100 
104 typedef enum {
105  Alarm1 = 1,
106  Alarm2 = 2
107 } AlarmId;
108 
112 typedef enum
113 {
118  Alarm1MatchDay = 0x10,
120 } Alarm1Type;
121 
125 typedef enum
126 {
130  Alarm2MatchDay = 0x10,
132 } Alarm2Type;
133 
137 typedef enum {
143 } SquareWave;
144 
145 
150 {
151 public:
152  // Initialize
153  bool begin();
154 
155  // Oscillator functions
156  bool isRunning();
157  bool clockEnable(bool enable=true);
158 
159  // Set/get date/time
160  time_t getEpoch();
161  bool setEpoch(time_t t);
162  bool read(struct tm *dt);
163  bool write(const struct tm *dt);
164  bool setTime(uint8_t hour, uint8_t min, uint8_t sec);
165  bool getTime(uint8_t *hour, uint8_t *min, uint8_t *sec);
166  bool setDateTime(uint8_t hour, uint8_t min, uint8_t sec,
167  uint8_t mday, uint8_t mon, uint16_t year,
168  uint8_t wday);
169  bool getDateTime(uint8_t *hour, uint8_t *min, uint8_t *sec,
170  uint8_t *mday, uint8_t *mon, uint16_t *year,
171  uint8_t *wday);
172 
173  // Alarm functions
174  bool setAlarm1(Alarm1Type alarmType,
175  uint8_t dayDate, uint8_t hours, uint8_t minutes, uint8_t seconds);
176  bool setAlarm2(Alarm2Type alarmType, uint8_t dayDate, uint8_t hours, uint8_t minutes);
177  bool alarmInterruptEnable(AlarmId alarmId, bool enable);
178  bool getAlarmFlag(AlarmId alarmId);
179  bool clearAlarmFlag(AlarmId alarmId);
180 
181  // Output signal control
182  bool setSquareWave(SquareWave squareWave);
183  bool outputClockPinEnable(bool enable);
184 
185  // Aging offset compensation
186  bool setAgingOffset(int8_t val);
187  int8_t getAgingOffset();
188 
189  // Temperature functions
191  bool getTemperature(int8_t *temperature, uint8_t *fraction);
192 
193  // BCD conversions
194  uint8_t bcdToDec(uint8_t bcd);
195  uint8_t decToBcd(uint8_t dec);
196 
197  // Read/write register
198  uint8_t readRegister(uint8_t reg);
199  bool writeRegister(uint8_t reg, uint8_t value);
200 
201  // Read/write buffer
202  bool readBuffer(uint8_t reg, void *buffer, uint8_t len);
203  bool writeBuffer(uint8_t reg, void *buffer, uint8_t len);
204 };
205 
206 #endif // ERRIEZ_DS3231_H_
uint8_t readRegister(uint8_t reg)
Read register.
bool getDateTime(uint8_t *hour, uint8_t *min, uint8_t *sec, uint8_t *mday, uint8_t *mon, uint16_t *year, uint8_t *wday)
Get date time.
Alarm when minutes and seconds match.
Definition: ErriezDS3231.h:116
bool alarmInterruptEnable(AlarmId alarmId, bool enable)
Enable or disable Alarm 1 or 2 interrupt.
uint8_t bcdToDec(uint8_t bcd)
BCD to decimal conversion.
time_t getEpoch()
Read Unix UTC epoch time_t.
SQW 8192Hz.
Definition: ErriezDS3231.h:142
bool clearAlarmFlag(AlarmId alarmId)
Clear alarm flag.
bool setAlarm1(Alarm1Type alarmType, uint8_t dayDate, uint8_t hours, uint8_t minutes, uint8_t seconds)
Set Alarm 1.
Alarm when day, hours, minutes, and seconds match.
Definition: ErriezDS3231.h:119
DS3231 RTC class.
Definition: ErriezDS3231.h:149
bool startTemperatureConversion()
Start temperature conversion.
bool setAgingOffset(int8_t val)
Set aging offset register.
Alarm2Type
Alarm 2 types enum.
Definition: ErriezDS3231.h:125
AlarmId
Alarm ID.
Definition: ErriezDS3231.h:104
bool readBuffer(uint8_t reg, void *buffer, uint8_t len)
Read buffer from RTC.
bool setDateTime(uint8_t hour, uint8_t min, uint8_t sec, uint8_t mday, uint8_t mon, uint16_t year, uint8_t wday)
Set date time.
bool write(const struct tm *dt)
Write date and time to RTC.
#define DS3231_CTRL_INTCN
Interrupt control.
Definition: ErriezDS3231.h:76
bool writeRegister(uint8_t reg, uint8_t value)
Write register.
Alarm once per second.
Definition: ErriezDS3231.h:114
Alarm when hours, minutes, and seconds match.
Definition: ErriezDS3231.h:117
Alarm ID 2.
Definition: ErriezDS3231.h:106
SQW 4096Hz.
Definition: ErriezDS3231.h:141
Alarm when hours and minutes match.
Definition: ErriezDS3231.h:129
bool read(struct tm *dt)
Read date and time from RTC.
uint8_t decToBcd(uint8_t dec)
Decimal to BCD conversion.
SQW 1024Hz.
Definition: ErriezDS3231.h:140
bool getTime(uint8_t *hour, uint8_t *min, uint8_t *sec)
Read time from RTC.
#define DS3231_CTRL_RS1
Square wave rate-select 1.
Definition: ErriezDS3231.h:75
#define DS3231_CTRL_RS2
Square wave rate-select 2.
Definition: ErriezDS3231.h:74
Alarm once per minute (00 seconds of every minute)
Definition: ErriezDS3231.h:127
bool clockEnable(bool enable=true)
Enable or disable oscillator when running on V-BAT.
bool setTime(uint8_t hour, uint8_t min, uint8_t sec)
Write time to RTC.
Alarm1Type
Alarm 1 types enum.
Definition: ErriezDS3231.h:112
bool setEpoch(time_t t)
Write Unix epoch UTC time to RTC.
bool getTemperature(int8_t *temperature, uint8_t *fraction)
Read temperature.
Alarm when seconds match.
Definition: ErriezDS3231.h:115
bool getAlarmFlag(AlarmId alarmId)
Get Alarm 1 or 2 flag.
SQW disable.
Definition: ErriezDS3231.h:138
Alarm when minutes match.
Definition: ErriezDS3231.h:128
Alarm when day, hours, and minutes match.
Definition: ErriezDS3231.h:131
bool begin()
Initialize and detect DS3231 RTC.
bool isRunning()
Read RTC OSF (Oscillator Stop Flag) from status register.
bool setAlarm2(Alarm2Type alarmType, uint8_t dayDate, uint8_t hours, uint8_t minutes)
Set Alarm 2.
Alarm when date, hours, minutes, and seconds match.
Definition: ErriezDS3231.h:118
bool writeBuffer(uint8_t reg, void *buffer, uint8_t len)
Write buffer to RTC.
Alarm when date, hours, and minutes match.
Definition: ErriezDS3231.h:130
bool setSquareWave(SquareWave squareWave)
Configure SQW (Square Wave) output pin.
SquareWave
Squarewave enum.
Definition: ErriezDS3231.h:137
Alarm ID 1.
Definition: ErriezDS3231.h:105
int8_t getAgingOffset()
Get aging offset register.
SQW 1Hz.
Definition: ErriezDS3231.h:139
bool outputClockPinEnable(bool enable)
Enable or disable 32kHz output clock pin.