Sming Framework API
Sming - Open Source framework for high efficiency WiFi SoC ESP8266 native development with C++ language.
DS3232RTC.h
1 /*----------------------------------------------------------------------*
2  * DS3232RTC.h - Arduino library for the Maxim Integrated DS3232 *
3  * Real-Time Clock. This library is intended for use with the Arduino *
4  * Time.h library, http://www.arduino.cc/playground/Code/Time *
5  * *
6  * This library is a drop-in replacement for the DS1307RTC.h library *
7  * by Michael Margolis that is supplied with the Arduino Time library *
8  * above. To change from using a DS1307 RTC to an DS3232 RTC, it is *
9  * only necessary to change the #include statement to include this *
10  * library instead of DS1307RTC.h. *
11  * *
12  * This library is *not* a drop-in replacement for the newer version *
13  * of the DS1307RTC library at *
14  * http://www.pjrc.com/teensy/td_libs_DS1307RTC.html *
15  * *
16  * In addition, this library implements functions to support the *
17  * additional features of the DS3232. *
18  * *
19  * This library will also work with the DS3231 RTC, which has the same *
20  * features of the DS3232 except: (1) Battery-backed SRAM, (2) Battery- *
21  * backed 32kHz output (BB32kHz bit in Control/Status register 0x0F), *
22  * and (3) Adjustable temperature sensor sample rate (CRATE1:0 bits in *
23  * the Control/Status register). *
24  * *
25  * Whether used with the DS3232 or DS3231, the user is responsible for *
26  * ensuring reads and writes do not exceed the device's address space *
27  * (0x00-0x12 for DS3231, 0x00-0xFF for DS3232); no bounds checking *
28  * is done by this library. *
29  * *
30  * Jack Christensen 06Mar2013 *
31  * http://github.com/JChristensen/DS3232RTC *
32  * *
33  * CC BY-SA 4.0 *
34  * "Arduino DS3232RTC Library" by Jack Christensen is licensed under *
35  * CC BY-SA 4.0, http://creativecommons.org/licenses/by-sa/4.0/ *
36  *----------------------------------------------------------------------*/
37 
38 #ifndef DS3232RTC_h
39 #define DS3232RTC_h
40 
41 #if defined(ARDUINO) && ARDUINO >= 100
42 #include <Arduino.h>
43 #else
44 #include <WProgram.h>
45 #endif
46 
47 
48 //HACK
49 typedef enum {
50  tmSecond, tmMinute, tmHour, tmWday, tmDay,tmMonth, tmYear, tmNbrFields
51 } tmByteFields;
52 
53 typedef struct {
54  uint8_t Second;
55  uint8_t Minute;
56  uint8_t Hour;
57  uint8_t Wday; // day of week, sunday is day 1
58  uint8_t Day;
59  uint8_t Month;
60  uint8_t Year; // offset from 1970;
62 
63 //convenience macros to convert to and from tm years
64 #define tmYearToCalendar(Y) ((Y) + 1970) // full four digit year
65 #define CalendarYrToTm(Y) ((Y) - 1970)
66 #define tmYearToY2k(Y) ((Y) - 30) // offset is from 2000
67 #define y2kYearToTm(Y) ((Y) + 30)
68 
69 /* low level functions to convert to and from system time */
70 void breakTime(time_t time, tmElements_t &tm); // break time_t into elements
71 time_t makeTime(tmElements_t &tm); // convert time elements into time_t
72 
73 //#include <DateTime.h>
74 
75 //DS3232 I2C Address
76 #define RTC_ADDR 0x68
77 
78 //DS3232 Register Addresses
79 #define RTC_SECONDS 0x00
80 #define RTC_MINUTES 0x01
81 #define RTC_HOURS 0x02
82 #define RTC_DAY 0x03
83 #define RTC_DATE 0x04
84 #define RTC_MONTH 0x05
85 #define RTC_YEAR 0x06
86 #define ALM1_SECONDS 0x07
87 #define ALM1_MINUTES 0x08
88 #define ALM1_HOURS 0x09
89 #define ALM1_DAYDATE 0x0A
90 #define ALM2_MINUTES 0x0B
91 #define ALM2_HOURS 0x0C
92 #define ALM2_DAYDATE 0x0D
93 #define RTC_CONTROL 0x0E
94 #define RTC_STATUS 0x0F
95 #define RTC_AGING 0x10
96 #define TEMP_MSB 0x11
97 #define TEMP_LSB 0x12
98 #define SRAM_START_ADDR 0x14 //first SRAM address
99 #define SRAM_SIZE 236 //number of bytes of SRAM
100 
101 //Alarm mask bits
102 #define A1M1 7
103 #define A1M2 7
104 #define A1M3 7
105 #define A1M4 7
106 #define A2M2 7
107 #define A2M3 7
108 #define A2M4 7
109 
110 //Control register bits
111 #define EOSC 7
112 #define BBSQW 6
113 #define CONV 5
114 #define RS2 4
115 #define RS1 3
116 #define INTCN 2
117 #define A2IE 1
118 #define A1IE 0
119 
120 //Status register bits
121 #define OSF 7
122 #define BB32KHZ 6
123 #define CRATE1 5
124 #define CRATE0 4
125 #define EN32KHZ 3
126 #define BSY 2
127 #define A2F 1
128 #define A1F 0
129 
130 //Square-wave output frequency (TS2, RS1 bits)
131 enum SQWAVE_FREQS_t {SQWAVE_1_HZ, SQWAVE_1024_HZ, SQWAVE_4096_HZ, SQWAVE_8192_HZ, SQWAVE_NONE};
132 
133 //Alarm masks
134 enum ALARM_TYPES_t {
135  ALM1_EVERY_SECOND = 0x0F,
136  ALM1_MATCH_SECONDS = 0x0E,
137  ALM1_MATCH_MINUTES = 0x0C, //match minutes *and* seconds
138  ALM1_MATCH_HOURS = 0x08, //match hours *and* minutes, seconds
139  ALM1_MATCH_DATE = 0x00, //match date *and* hours, minutes, seconds
140  ALM1_MATCH_DAY = 0x10, //match day *and* hours, minutes, seconds
141  ALM2_EVERY_MINUTE = 0x8E,
142  ALM2_MATCH_MINUTES = 0x8C, //match minutes
143  ALM2_MATCH_HOURS = 0x88, //match hours *and* minutes
144  ALM2_MATCH_DATE = 0x80, //match date *and* hours, minutes
145  ALM2_MATCH_DAY = 0x90, //match day *and* hours, minutes
146 };
147 
148 #define ALARM_1 1 //constants for calling functions
149 #define ALARM_2 2
150 
151 //Other
152 #define DS1307_CH 7 //for DS1307 compatibility, Clock Halt bit in Seconds register
153 #define HR1224 6 //Hours register 12 or 24 hour mode (24 hour mode==0)
154 #define CENTURY 7 //Century bit in Month register
155 #define DYDT 6 //Day/Date flag bit in alarm Day/Date registers
156 
158 {
159  public:
160  DS3232RTC();
161  static time_t get(void); //must be static to work with setSyncProvider() in the Time library
162  byte set(time_t t);
163  static byte read(tmElements_t &tm);
164  byte write(tmElements_t &tm);
165  byte writeRTC(byte addr, byte *values, byte nBytes);
166  byte writeRTC(byte addr, byte value);
167  byte readRTC(byte addr, byte *values, byte nBytes);
168  byte readRTC(byte addr);
169  void setAlarm(ALARM_TYPES_t alarmType, byte seconds, byte minutes, byte hours, byte daydate);
170  void setAlarm(ALARM_TYPES_t alarmType, byte minutes, byte hours, byte daydate);
171  void alarmInterrupt(byte alarmNumber, bool alarmEnabled);
172  bool alarm(byte alarmNumber);
173  void squareWave(SQWAVE_FREQS_t freq);
174  bool oscStopped(bool clearOSF = true); //defaults to clear the OSF bit if argument not supplied
175  int temperature(void);
176 
177  private:
178  uint8_t dec2bcd(uint8_t n);
179  static uint8_t bcd2dec(uint8_t n);
180 };
181 
182 extern DS3232RTC DSRTC;
183 
184 #endif
Definition: DS3232RTC.h:157
Definition: DS3232RTC.h:53