Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: ILI9341_Clock_Nucleo
Fork of DS3231 by
DS3231.h
00001 /** mbded library for driving the PMAXIM DS3231 Real Time Clock 00002 * datasheet link : http://datasheets.maximintegrated.com/en/ds/DS3231.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 // DS3231 Library test program 00011 // remi cormier 2012 00012 00013 #include "mbed.h" 00014 #include "DS3231.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 DS3231 RTC(p28,p27); 00028 00029 00030 int main() 00031 {printf("\r\n\nDS3231 Library test program\r\nremi cormier 2012\r\n\n"); 00032 00033 RTC.setI2Cfrequency(400000); 00034 00035 //RTC.writeRegister(DS3231_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(DS3231_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 #include "mbed.h" 00071 00072 #ifndef MBED_DS3231_H 00073 #define MBED_DS3231_H 00074 00075 //DS3231 8 bit adress 00076 #define DS3231_Address 0xD0 00077 00078 //DS3231 registers 00079 #define DS3231_Seconds 0x00 00080 #define DS3231_Minutes 0x01 00081 #define DS3231_Hours 0x02 00082 // DS3231 Hours bits 00083 #define DS3231_bit_AM_PM 0x20 00084 #define DS3231_bit_12_24 0x40 00085 00086 #define DS3231_Day 0x03 00087 #define DS3231_Date 0x04 00088 #define DS3231_Month_Century 0x05 00089 #define DS3231_Year 0x06 00090 #define DS3231_Alarm1_Seconds 0x07 00091 #define DS3231_Alarm1_Minutes 0x08 00092 #define DS3231_Alarm1_Hours 0x09 00093 #define DS3231_Alarm1_Day_Date 0x0A 00094 #define DS3231_Alarm2_Minutes 0x0B 00095 #define DS3231_Alarm2_Hours 0x0C 00096 #define DS3231_Alarm_2_Day_Date 0x0D 00097 00098 #define DS3231_Control 0x0E 00099 // DS3231 Control bits 00100 #define DS3231_bit_A1IE 1 00101 #define DS3231_bit_A2IE 2 00102 #define DS3231_bit_INTCN 4 00103 #define DS3231_bit_SQW_1Hz 0 00104 #define DS3231_bit_SQW_1024Hz 8 00105 #define DS3231_bit_SQW_4096Hz 16 00106 #define DS3231_bit_SQW_8192Hz 24 00107 #define DS3231_bit_CONV 32 00108 #define DS3231_bit_BBSQW 64 00109 #define DS3231_bit_EOSCb 128 00110 00111 00112 #define DS3231_Control_Status 0x0F 00113 // DS3231 Control/Status bits 00114 #define DS3231_bit_A1F 0x01 00115 #define DS3231_bit_A2F 0x02 00116 #define DS3231_bit_BSY 0x04 00117 #define DS3231_bit_EN32kHz 0x08 00118 #define DS3231_bit_OSF 0x80 00119 00120 #define DS3231_Aging_Offset 0x10 00121 #define DS3231_MSB_Temp 0x11 00122 #define DS3231_LSB_Temp 0x12 00123 00124 /* Interface to MAXIM DS3231 RTC */ 00125 class DS3231 00126 {public : 00127 /** Create an instance of the DS3231 connected to specfied I2C pins 00128 * 00129 * @param sda The I2C data pin 00130 * @param scl The I2C clock pin 00131 */ 00132 DS3231(PinName sda, PinName scl); 00133 00134 /** set I2C bus speed 00135 * @param frequency : I2C clocl frequenct (Hz) 00136 */ 00137 void setI2Cfrequency(int frequency); 00138 00139 /** Read the temperature 00140 * 00141 * @return The temperature 00142 */ 00143 float readTemp(); 00144 00145 /** Read the time registers 00146 * @param hours 00147 * @param minutes 00148 * @param seconds 00149 */ 00150 void readTime(int *hours, int *minutes, int *seconds); 00151 00152 /** force temperature conversion 00153 * 00154 */ 00155 void convertTemperature(); 00156 00157 /** Set the time registers 00158 * @param hours 00159 * @param minutes 00160 * @param seconds 00161 */ 00162 void setTime(int hours, int minutes, int seconds); 00163 00164 /** Read the date registers 00165 * @param date 00166 * @param month 00167 * @param year 00168 */ 00169 void readDate(int *date, int *month, int *year); 00170 00171 /** Set the date registers 00172 * @param dayOfWeek : day of week 00173 * @param date 00174 * @param month 00175 * @param year 00176 */ 00177 void setDate(int dayOfWeek, int date, int month, int year); 00178 00179 /** Read the date and time registers 00180 * @param dayOfWeek : day of week 00181 * @param date 00182 * @param month 00183 * @param year 00184 * @param hours 00185 * @param minutes 00186 * @param seconds 00187 */ 00188 void readDateTime(int *dayOfWeek, int *date, int *month, int *year, int *hours, int *minutes, int *seconds); 00189 00190 /** Read a register 00191 * @param reg : register address 00192 * @return The register content 00193 */ 00194 int readRegister(char reg); 00195 00196 /** Write to a register 00197 * @param reg : register address 00198 * @param The register content 00199 */ 00200 void writeRegister(int reg,char byte); 00201 00202 /** set OSF (Oscillator Stop Flag) bit to 0 in Control Status register 00203 * should be done just after power up DS3231 00204 * OSF bit is automaticaly set to 1 when on power up or when the DS3231 oscillator stops 00205 */ 00206 void eraseOSF(); 00207 00208 /** Return OSF bit. If true the oscillator stopped or the DS3231 just powered up 00209 * @return The OSF bit 00210 */ 00211 bool OSF(); 00212 00213 bool error; 00214 00215 private : 00216 I2C i2c; 00217 int bcd2dec(int k); // bcd to decimal conversion 00218 int dec2bcd(int k); // decimal to bcd conversion 00219 void decodeTime(int regHours, int regMinutes, int regSeconds,int *Hours, int *Minutes, int *Seconds); 00220 void decodeDate(int regDate,int regMonth, int regYear, int *date, int *month, int *year); 00221 }; 00222 00223 00224 #endif
Generated on Tue Jul 12 2022 19:53:48 by
1.7.2
