MAXIM DS3231 accurate Real Time Clock Library
Fork of DS3231 by
Embed:
(wiki syntax)
Show/hide line numbers
ARM_RTC.h
00001 /** mbded library for driving the PMAXIM ARM_RTC Real Time Clock 00002 * datasheet link : http://datasheets.maximintegrated.com/en/ds/ARM_RTC.pdf 00003 * breakout : MACETECH ChronoDot V2.1 High Precision RTC 00004 * remi cormier 2012 00005 * WARNING : sda and sdl should be pulled up with 2.2k resistor 00006 */ 00007 00008 /** Example code 00009 * @code 00010 // ARM_RTC Library test program 00011 // remi cormier 2012 00012 00013 #include "mbed.h" 00014 #include "ARM_RTC.h" 00015 00016 Serial pc(USBTX, USBRX); 00017 00018 int hour; 00019 int minute; 00020 int second; 00021 00022 int dayOfWeek; 00023 int date; 00024 int month; 00025 int year; 00026 00027 ARM_RTC RTC(p28,p27); 00028 00029 00030 int main() 00031 {printf("\r\n\nARM_RTC Library test program\r\nremi cormier 2012\r\n\n"); 00032 00033 RTC.setI2Cfrequency(400000); 00034 00035 //RTC.writeRegister(ARM_RTC_Aging_Offset,0); // uncomment to set Aging Offset 1LSB = approx. 0.1 ppm according from datasheet = 0.05 ppm @ 21 °C from my measurments 00036 00037 RTC.convertTemperature(); 00038 00039 int reg=RTC.readRegister(ARM_RTC_Aging_Offset); 00040 if (reg>127) 00041 {reg=reg-256;} 00042 pc.printf("Aging offset : %i\r\n",reg); 00043 00044 pc.printf("OSF flag : %i",RTC.OSF()); 00045 pc.printf("\r\n"); 00046 00047 RTC.readDate(&date,&month,&year); 00048 pc.printf("date : %02i-%02i-%02i",date,month,year); 00049 pc.printf("\r\n"); 00050 00051 //RTC.setTime(19,48,45); // uncomment to set time 00052 00053 RTC.readTime(&hour,&minute,&second); 00054 pc.printf("time : %02i:%02i:%02i",hour,minute,second); 00055 pc.printf("\r\n"); 00056 00057 //RTC.setDate(6,22,12,2012); // uncomment to set date 00058 00059 RTC.readDateTime(&dayOfWeek,&date,&month,&year,&hour,&minute,&second); 00060 pc.printf("date time : %i / %02i-%02i-%02i %02i:%02i:%02i",dayOfWeek,date,month,year,hour,minute,second); 00061 pc.printf("\r\n"); 00062 00063 pc.printf("temperature :%6.2f",RTC.readTemp()); 00064 pc.printf("\r\n"); 00065 } 00066 * @endcode 00067 */ 00068 00069 /* 00070 http://www.cplusplus.com/reference/ctime/strftime/ 00071 %a Abbreviated weekday name * Thu 00072 %A Full weekday name * Thursday 00073 %b Abbreviated month name * Aug 00074 %B Full month name * August 00075 %d Day of the month, zero-padded (01-31) 23 00076 %e Day of the month, space-padded ( 1-31) 23 00077 %F Short YYYY-MM-DD date, equivalent to %Y-%m-%d 2001-08-23 00078 %H Hour in 24h format (00-23) 14 00079 %j Day of the year (001-366) 235 00080 %m Month as a decimal number (01-12) 08 00081 %M Minute (00-59) 55 00082 %R 24-hour HH:MM time, equivalent to %H:%M 14:55 00083 %S Second (00-61) 02 00084 %T ISO 8601 time format (HH:MM:SS), equivalent to %H:%M:%S 14:55:02 00085 %u ISO 8601 weekday as number with Monday as 1 (1-7) 4 00086 %V ISO 8601 week number (00-53) 34 00087 %w Weekday as a decimal number with Sunday as 0 (0-6) 4 00088 %W Week number with the first Monday as the first day of week one (00-53) 34 00089 %X Time representation * 14:55:02 00090 %y Year, last two digits (00-99) 01 00091 %Y Year 2001 00092 00093 http://www.cplusplus.com/reference/ctime/tm/ 00094 Member Type Meaning Range 00095 tm_sec int seconds after the minute 0-61* 00096 tm_min int minutes after the hour 0-59 00097 tm_hour int hours since midnight 0-23 00098 tm_mday int day of the month 1-31 00099 tm_mon int months since January 0-11 00100 tm_year int years since 1900 00101 tm_wday int days since Sunday 0-6 (0 = Sunday) 00102 tm_yday int days since January 1 0-365 00103 tm_isdst int Daylight Saving Time flag 00104 The Daylight Saving Time flag (tm_isdst) is greater than zero if Daylight Saving Time is in effect, 00105 zero if Daylight Saving Time is not in effect, and less than zero if the information is not available. 00106 * tm_sec is generally 0-59. The extra range is to accommodate for leap seconds in certain systems. 00107 00108 Member Type Meaning Range 00109 tm_sec int seconds after the minute 0-61* 00110 tm_min int minutes after the hour 0-59 00111 tm_hour int hours since midnight 0-23 00112 tm_mday int day of the month 1-31 00113 tm_mon int months since January 0-11 00114 tm_year int years since 1900 00115 tm_wday int days since Sunday 0-6 00116 tm_yday int days since January 1 0-365 00117 tm_isdst int Daylight Saving Time flag 00118 00119 http://www.epochconverter.com/programming/c 00120 Convert from epoch to human readable date 00121 time_t now; 00122 struct tm ts; 00123 char buf[80]; 00124 // Get current time 00125 time(&now); 00126 // Format time, "ddd yyyy-mm-dd hh:mm:ss zzz" 00127 ts = *localtime(&now); 00128 strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S %Z", &ts); 00129 printf("%s\n", buf); 00130 00131 Convert from human readable date to epoch 00132 struct tm t; 00133 time_t t_of_day; 00134 t.tm_year = 2011-1900; 00135 t.tm_mon = 7; // Month, 0 - jan 00136 t.tm_mday = 8; // Day of the month 00137 t.tm_hour = 16; 00138 t.tm_min = 11; 00139 t.tm_sec = 42; 00140 t.tm_isdst = -1; // Is DST on? 1 = yes, 0 = no, -1 = unknown 00141 t_of_day = mktime(&t); 00142 printf("seconds since the Epoch: %ld\n", (long) t_of_day) 00143 00144 Convert to struct tm 00145 time_t rawtime; 00146 struct tm * t; 00147 time(&rawtime); 00148 t = localtime(&rawtime); 00149 printf("%d\n", t->tm_sec); 00150 00151 00152 https://github.com/raburton/esp8266/blob/master/drivers/ARM_RTC.c 00153 https://github.com/raburton/esp8266/blob/master/drivers/ARM_RTC.h 00154 00155 */ 00156 #include <mbed.h> 00157 #include "TableSummerTime.h" 00158 #include "TableDayLight.h" 00159 00160 #ifndef MBED_ARM_RTC_H 00161 #define MBED_ARM_RTC_H 00162 00163 //ARM_RTC 8 bit adress 00164 #define ARM_RTC_ADDR 0x68 // 0x68 << 1 = 0xD0 00165 00166 #define ARM_RTC_STAT_OSCILLATOR 0x80 00167 #define ARM_RTC_STAT_32KHZ 0x08 00168 #define ARM_RTC_STAT_BUSY 0x04 00169 #define ARM_RTC_STAT_ALARM_2 0x02 00170 #define ARM_RTC_STAT_ALARM_1 0x01 00171 00172 //ARM_RTC registers 00173 #define ARM_RTC_Seconds 0x00 00174 #define ARM_RTC_Minutes 0x01 00175 #define ARM_RTC_Hours 0x02 00176 // ARM_RTC Hours bits 00177 #define ARM_RTC_bit_AM_PM 0x20 00178 #define ARM_RTC_bit_12_24 0x40 00179 00180 #define ARM_RTC_Day 0x03 00181 #define ARM_RTC_Date 0x04 00182 #define ARM_RTC_Month_Century 0x05 00183 #define ARM_RTC_Year 0x06 00184 00185 #define ARM_RTC_Alarm1_Seconds 0x07 00186 #define ARM_RTC_Alarm1_Minutes 0x08 00187 #define ARM_RTC_Alarm1_Hours 0x09 00188 #define ARM_RTC_Alarm1_Day_Date 0x0A 00189 00190 #define ARM_RTC_Alarm2_Minutes 0x0B 00191 #define ARM_RTC_Alarm2_Hours 0x0C 00192 #define ARM_RTC_Alarm_2_Day_Date 0x0D 00193 00194 #define ARM_RTC_Control 0x0E 00195 00196 // ARM_RTC Control bits 00197 #define ARM_RTC_bit_A1IE 0x01 00198 #define ARM_RTC_bit_A2IE 0x02 00199 #define ARM_RTC_bit_INTCN 0x04 00200 #define ARM_RTC_bit_SQW_1Hz 0x00 00201 #define ARM_RTC_bit_SQW_1024Hz 0x08 00202 #define ARM_RTC_bit_SQW_4096Hz 0x10 00203 #define ARM_RTC_bit_SQW_8192Hz 0x18 00204 #define ARM_RTC_bit_CONV 0x20 00205 #define ARM_RTC_bit_BBSQW 0x40 00206 #define ARM_RTC_bit_EOSCb 0x80 00207 00208 #define ARM_RTC_CTRL_ALARM1_INT 0x01 00209 #define ARM_RTC_CTRL_ALARM2_INT 0x02 00210 #define ARM_RTC_CTRL_ALARM_INTS 0x04 00211 #define ARM_RTC_CTRL_SQWAVE_1HZ 0x00 00212 #define ARM_RTC_CTRL_SQWAVE_1024HZ 0x08 00213 #define ARM_RTC_CTRL_SQWAVE_4096HZ 0x10 00214 #define ARM_RTC_CTRL_SQWAVE_8192HZ 0x18 00215 #define ARM_RTC_CTRL_TEMPCONV 0x20 00216 #define ARM_RTC_CTRL_SQUAREWAVE_BB 0x40 00217 #define ARM_RTC_CTRL_OSCILLATOR 0x80 00218 00219 #define ARM_RTC_ALARM_NONE 0 00220 #define ARM_RTC_ALARM_1 1 00221 #define ARM_RTC_ALARM_2 2 00222 #define ARM_RTC_ALARM_BOTH 3 00223 00224 #define ARM_RTC_ALARM1_EVERY_SECOND 0 00225 #define ARM_RTC_ALARM1_MATCH_SEC 1 00226 #define ARM_RTC_ALARM1_MATCH_SECMIN 2 00227 #define ARM_RTC_ALARM1_MATCH_SECMINHOUR 3 00228 #define ARM_RTC_ALARM1_MATCH_SECMINHOURDAY 4 00229 #define ARM_RTC_ALARM1_MATCH_SECMINHOURDATE 5 00230 00231 #define ARM_RTC_ALARM2_EVERY_MIN 0 00232 #define ARM_RTC_ALARM2_MATCH_MIN 1 00233 #define ARM_RTC_ALARM2_MATCH_MINHOUR 2 00234 #define ARM_RTC_ALARM2_MATCH_MINHOURDAY 3 00235 #define ARM_RTC_ALARM2_MATCH_MINHOURDATE 4 00236 00237 #define ARM_RTC_ALARM_WDAY 0x40 00238 #define ARM_RTC_ALARM_NOTSET 0x80 00239 00240 #define ARM_RTC_ADDR_TIME 0x00 00241 #define ARM_RTC_ADDR_ALARM1 0x07 00242 #define ARM_RTC_ADDR_ALARM2 0x0b 00243 #define ARM_RTC_ADDR_CONTROL 0x0e 00244 #define ARM_RTC_ADDR_STATUS 0x0f 00245 #define ARM_RTC_ADDR_AGING 0x10 00246 #define ARM_RTC_ADDR_TEMP 0x11 00247 00248 #define ARM_RTC_SET 0 00249 #define ARM_RTC_CLEAR 1 00250 #define ARM_RTC_REPLACE 2 00251 00252 #define ARM_RTC_12HOUR_FLAG 0x40 00253 #define ARM_RTC_12HOUR_MASK 0x1f 00254 #define ARM_RTC_PM_FLAG 0x20 00255 #define ARM_RTC_MONTH_MASK 0x1f 00256 00257 #define ARM_RTC_Control_Status 0x0F 00258 00259 // ARM_RTC Control/Status bits 00260 #define ARM_RTC_bit_BSY 0x04 00261 #define ARM_RTC_bit_EN32kHz 0x08 00262 #define ARM_RTC_bit_OSF 0x80 00263 00264 #define ARM_RTC_Aging_Offset 0x10 00265 #define ARM_RTC_MSB_Temp 0x11 00266 #define ARM_RTC_LSB_Temp 0x12 00267 00268 #define NTP_OFFSET 2208988800ULL 00269 00270 /* Interface to MAXIM ARM_RTC RTC */ 00271 class ARM_RTC 00272 { 00273 public : 00274 /** Create an instance of the ARM_RTC connected to specfied I2C pins 00275 * 00276 * @param sda The I2C data pin 00277 * @param scl The I2C clock pin 00278 */ 00279 ARM_RTC(); 00280 00281 bool checkTimeLost(void); 00282 00283 /** Set the time registers 00284 * @param seconds since 1900 00285 */ 00286 void setDateTimeSecsSince1900(uint32_t secsSince1900); 00287 00288 /** Set the time registers 00289 * @param seconds since 1970 00290 */ 00291 void setDateTimeSecsSince1970(uint32_t secsSince1970); 00292 00293 /** Set the time registers 00294 * @return seconds since 1970 00295 */ 00296 uint32_t getDateTimeSecsSince1970(void); 00297 time_t getDateTimeSecsSince1970TZ(void); 00298 00299 /** Set the time zone 00300 * @param hours offset 00301 * @return void 00302 */ 00303 void setTimeZone(double TZ); 00304 00305 /** Get the time zone 00306 * @param void 00307 * @return hours offset 00308 */ 00309 double getTimeZone(void); 00310 00311 /** Set the date registers 00312 * // @param dayOfWeek : day of week 00313 * @param date 00314 * @param month 00315 * @param year 00316 */ 00317 void setDate(int date, int month, int year); 00318 00319 /** Set the time registers 00320 * @param hours 00321 * @param minutes 00322 * @param seconds 00323 */ 00324 void setTime(int hours, int minutes, int seconds); 00325 00326 /** Read the date and time and set system clock 00327 */ 00328 void setSystemClock(void); 00329 00330 void readDateTime(void); 00331 00332 /** Set the time 00333 * @param hours 00334 * @param minutes 00335 * @param seconds 00336 * @param day 00337 * @param month 00338 * @param year 00339 */ 00340 void setDateTime(int day, int month, int year, int hours, int minutes, int seconds); 00341 00342 /** Set the time registers 00343 * @param struct tm *time 00344 */ 00345 void setDateTime(struct tm *time); 00346 00347 /** Get the time registers 00348 * @return struct tm *time 00349 */ 00350 void getDateTime(struct tm *time); 00351 struct tm *getDateTime(void); 00352 00353 int getDayOfWeek(int date, int month, int year); 00354 00355 char * getDayOfWeekName(void); 00356 00357 char * getFormatedDateTime(char *format); 00358 00359 bool error; 00360 00361 bool getSummerTime(void); 00362 00363 int dayOfYearC(void); 00364 char * getSunRise(void); 00365 char * getSunSet(void); 00366 char * getDayLength(void); 00367 int getSunRiseMinute(void); 00368 int getSunSetMinute(void); 00369 bool checkSunRise(void); 00370 00371 void substr(char *s, char *d, int pos, int len); 00372 char * substr(char *s, int pos, int len); 00373 00374 int bcd2dec(int k); // bcd to decimal conversion 00375 int dec2bcd(int k); // decimal to bcd conversion 00376 00377 private : 00378 // I2C i2c; 00379 // char ARM_RTC_Address; 00380 // void decodeTime(int regHours, int regMinutes, int regSeconds,int *Hours, int *Minutes, int *Seconds); 00381 // void decodeDate(int regDate,int regMonth, int regYear, int *date, int *month, int *year); 00382 00383 char buffer[40]; 00384 00385 uint32_t timeZoneOffset; 00386 char timeZoneName[32]; 00387 bool dayLightSaving; 00388 00389 struct tm t; 00390 // t.tm_sec = -1; 00391 // t.tm_min = -1; 00392 // t.tm_hour = -1; 00393 // t.tm_mday = -1; 00394 // t.tm_wday = -1; 00395 // t.tm_yday = -1; 00396 // t.tm_mon = -1; 00397 // t.tm_year = -1; 00398 00399 time_t secondsEpoch; 00400 00401 struct DateTime{ 00402 int year; 00403 int mon; 00404 int mday; 00405 int wday; 00406 int yday; 00407 int hour; 00408 int min; 00409 int sec; 00410 }; 00411 00412 00413 }; 00414 00415 #endif
Generated on Tue Jul 19 2022 23:05:54 by 1.7.2