MAXIM DS3231 accurate Real Time Clock Library
Fork of DS3231 by
DS3231.h@0:11dfd7ac4188, 2012-12-22 (annotated)
- Committer:
- cromda
- Date:
- Sat Dec 22 19:23:19 2012 +0000
- Revision:
- 0:11dfd7ac4188
- Child:
- 1:84ccc64067d8
MAXIM DS3231 RTC
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cromda | 0:11dfd7ac4188 | 1 | /** mbded library for driving the PMAXIM DS3231 Real Time Clock |
cromda | 0:11dfd7ac4188 | 2 | * datasheet link : http://datasheets.maximintegrated.com/en/ds/DS3231.pdf |
cromda | 0:11dfd7ac4188 | 3 | * breakout : MACETECH ChronoDot V2.1 High Precision RTC |
cromda | 0:11dfd7ac4188 | 4 | * remi cormier 2012 |
cromda | 0:11dfd7ac4188 | 5 | * WARNING : sda and sdl should be pulled up with 2.2k resistor |
cromda | 0:11dfd7ac4188 | 6 | */ |
cromda | 0:11dfd7ac4188 | 7 | |
cromda | 0:11dfd7ac4188 | 8 | /** Example code |
cromda | 0:11dfd7ac4188 | 9 | * @code |
cromda | 0:11dfd7ac4188 | 10 | // DS3231 Library test program |
cromda | 0:11dfd7ac4188 | 11 | // remi cormier 2012 |
cromda | 0:11dfd7ac4188 | 12 | |
cromda | 0:11dfd7ac4188 | 13 | #include "mbed.h" |
cromda | 0:11dfd7ac4188 | 14 | #include "DS3231.h" |
cromda | 0:11dfd7ac4188 | 15 | |
cromda | 0:11dfd7ac4188 | 16 | Serial pc(USBTX, USBRX); |
cromda | 0:11dfd7ac4188 | 17 | |
cromda | 0:11dfd7ac4188 | 18 | int hour; |
cromda | 0:11dfd7ac4188 | 19 | int minute; |
cromda | 0:11dfd7ac4188 | 20 | int second; |
cromda | 0:11dfd7ac4188 | 21 | |
cromda | 0:11dfd7ac4188 | 22 | int dayOfWeek; |
cromda | 0:11dfd7ac4188 | 23 | int date; |
cromda | 0:11dfd7ac4188 | 24 | int month; |
cromda | 0:11dfd7ac4188 | 25 | int year; |
cromda | 0:11dfd7ac4188 | 26 | |
cromda | 0:11dfd7ac4188 | 27 | DS3231 RTC(p28,p27); |
cromda | 0:11dfd7ac4188 | 28 | |
cromda | 0:11dfd7ac4188 | 29 | |
cromda | 0:11dfd7ac4188 | 30 | int main() |
cromda | 0:11dfd7ac4188 | 31 | {printf("\r\n\nDS3231 Library test program\r\nremi cormier 2012\r\n\n"); |
cromda | 0:11dfd7ac4188 | 32 | |
cromda | 0:11dfd7ac4188 | 33 | RTC.setI2Cfrequency(400000); |
cromda | 0:11dfd7ac4188 | 34 | |
cromda | 0:11dfd7ac4188 | 35 | //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 |
cromda | 0:11dfd7ac4188 | 36 | |
cromda | 0:11dfd7ac4188 | 37 | int reg=RTC.readRegister(DS3231_Aging_Offset); |
cromda | 0:11dfd7ac4188 | 38 | if (reg>127) |
cromda | 0:11dfd7ac4188 | 39 | {reg=reg-256;} |
cromda | 0:11dfd7ac4188 | 40 | pc.printf("Aging offset : %i\r\n",reg); |
cromda | 0:11dfd7ac4188 | 41 | |
cromda | 0:11dfd7ac4188 | 42 | pc.printf("OSF flag : %i",RTC.OSF()); |
cromda | 0:11dfd7ac4188 | 43 | pc.printf("\r\n"); |
cromda | 0:11dfd7ac4188 | 44 | |
cromda | 0:11dfd7ac4188 | 45 | RTC.readDate(&date,&month,&year); |
cromda | 0:11dfd7ac4188 | 46 | pc.printf("date : %02i-%02i-%02i",date,month,year); |
cromda | 0:11dfd7ac4188 | 47 | pc.printf("\r\n"); |
cromda | 0:11dfd7ac4188 | 48 | |
cromda | 0:11dfd7ac4188 | 49 | //RTC.setTime(19,48,45); // uncomment to set time |
cromda | 0:11dfd7ac4188 | 50 | |
cromda | 0:11dfd7ac4188 | 51 | RTC.readTime(&hour,&minute,&second); |
cromda | 0:11dfd7ac4188 | 52 | pc.printf("time : %02i:%02i:%02i",hour,minute,second); |
cromda | 0:11dfd7ac4188 | 53 | pc.printf("\r\n"); |
cromda | 0:11dfd7ac4188 | 54 | |
cromda | 0:11dfd7ac4188 | 55 | //RTC.setDate(6,22,12,2012); // uncomment to set date |
cromda | 0:11dfd7ac4188 | 56 | |
cromda | 0:11dfd7ac4188 | 57 | RTC.readDateTime(&dayOfWeek,&date,&month,&year,&hour,&minute,&second); |
cromda | 0:11dfd7ac4188 | 58 | pc.printf("date time : %i / %02i-%02i-%02i %02i:%02i:%02i",dayOfWeek,date,month,year,hour,minute,second); |
cromda | 0:11dfd7ac4188 | 59 | pc.printf("\r\n"); |
cromda | 0:11dfd7ac4188 | 60 | |
cromda | 0:11dfd7ac4188 | 61 | pc.printf("temperature :%6.2f",RTC.readTemp()); |
cromda | 0:11dfd7ac4188 | 62 | pc.printf("\r\n"); |
cromda | 0:11dfd7ac4188 | 63 | } |
cromda | 0:11dfd7ac4188 | 64 | * @endcode |
cromda | 0:11dfd7ac4188 | 65 | */ |
cromda | 0:11dfd7ac4188 | 66 | |
cromda | 0:11dfd7ac4188 | 67 | |
cromda | 0:11dfd7ac4188 | 68 | #include "mbed.h" |
cromda | 0:11dfd7ac4188 | 69 | |
cromda | 0:11dfd7ac4188 | 70 | #ifndef MBED_DS3231_H |
cromda | 0:11dfd7ac4188 | 71 | #define MBED_DS3231_H |
cromda | 0:11dfd7ac4188 | 72 | |
cromda | 0:11dfd7ac4188 | 73 | //DS3231 8 bit adress |
cromda | 0:11dfd7ac4188 | 74 | #define DS3231_Address 0xD0 |
cromda | 0:11dfd7ac4188 | 75 | |
cromda | 0:11dfd7ac4188 | 76 | //DS3231 registers |
cromda | 0:11dfd7ac4188 | 77 | #define DS3231_Seconds 0x00 |
cromda | 0:11dfd7ac4188 | 78 | #define DS3231_Minutes 0x01 |
cromda | 0:11dfd7ac4188 | 79 | #define DS3231_Hours 0x02 |
cromda | 0:11dfd7ac4188 | 80 | // DS3231 Hours bits |
cromda | 0:11dfd7ac4188 | 81 | #define DS3231_bit_AM_PM 0x20 |
cromda | 0:11dfd7ac4188 | 82 | #define DS3231_bit_12_24 0x40 |
cromda | 0:11dfd7ac4188 | 83 | |
cromda | 0:11dfd7ac4188 | 84 | #define DS3231_Day 0x03 |
cromda | 0:11dfd7ac4188 | 85 | #define DS3231_Date 0x04 |
cromda | 0:11dfd7ac4188 | 86 | #define DS3231_Month_Century 0x05 |
cromda | 0:11dfd7ac4188 | 87 | #define DS3231_Year 0x06 |
cromda | 0:11dfd7ac4188 | 88 | #define DS3231_Alarm1_Seconds 0x07 |
cromda | 0:11dfd7ac4188 | 89 | #define DS3231_Alarm1_Minutes 0x08 |
cromda | 0:11dfd7ac4188 | 90 | #define DS3231_Alarm1_Hours 0x09 |
cromda | 0:11dfd7ac4188 | 91 | #define DS3231_Alarm1_Day_Date 0x0A |
cromda | 0:11dfd7ac4188 | 92 | #define DS3231_Alarm2_Minutes 0x0B |
cromda | 0:11dfd7ac4188 | 93 | #define DS3231_Alarm2_Hours 0x0C |
cromda | 0:11dfd7ac4188 | 94 | #define DS3231_Alarm_2_Day_Date 0x0D |
cromda | 0:11dfd7ac4188 | 95 | |
cromda | 0:11dfd7ac4188 | 96 | #define DS3231_Control 0x0E |
cromda | 0:11dfd7ac4188 | 97 | // DS3231 Control bits |
cromda | 0:11dfd7ac4188 | 98 | #define DS3231_bit_A1IE 1 |
cromda | 0:11dfd7ac4188 | 99 | #define DS3231_bit_A2IE 2 |
cromda | 0:11dfd7ac4188 | 100 | #define DS3231_bit_INTCN 4 |
cromda | 0:11dfd7ac4188 | 101 | #define DS3231_bit_SQW_1Hz 0 |
cromda | 0:11dfd7ac4188 | 102 | #define DS3231_bit_SQW_1024Hz 8 |
cromda | 0:11dfd7ac4188 | 103 | #define DS3231_bit_SQW_4096Hz 16 |
cromda | 0:11dfd7ac4188 | 104 | #define DS3231_bit_SQW_8192Hz 24 |
cromda | 0:11dfd7ac4188 | 105 | #define DS3231_bit_CONV 32 |
cromda | 0:11dfd7ac4188 | 106 | #define DS3231_bit_BBSQW 64 |
cromda | 0:11dfd7ac4188 | 107 | #define DS3231_bit_EOSCb 128 |
cromda | 0:11dfd7ac4188 | 108 | |
cromda | 0:11dfd7ac4188 | 109 | |
cromda | 0:11dfd7ac4188 | 110 | #define DS3231_Control_Status 0x0F |
cromda | 0:11dfd7ac4188 | 111 | // DS3231 Control/Status bits |
cromda | 0:11dfd7ac4188 | 112 | #define DS3231_bit_BSY 0x04 |
cromda | 0:11dfd7ac4188 | 113 | #define DS3231_bit_EN32kHz 0x08 |
cromda | 0:11dfd7ac4188 | 114 | #define DS3231_bit_OSF 0x80 |
cromda | 0:11dfd7ac4188 | 115 | |
cromda | 0:11dfd7ac4188 | 116 | #define DS3231_Aging_Offset 0x10 |
cromda | 0:11dfd7ac4188 | 117 | #define DS3231_MSB_Temp 0x11 |
cromda | 0:11dfd7ac4188 | 118 | #define DS3231_LSB_Temp 0x12 |
cromda | 0:11dfd7ac4188 | 119 | |
cromda | 0:11dfd7ac4188 | 120 | /* Interface to MAXIM DS3231 RTC */ |
cromda | 0:11dfd7ac4188 | 121 | class DS3231 |
cromda | 0:11dfd7ac4188 | 122 | {public : |
cromda | 0:11dfd7ac4188 | 123 | /** Create an instance of the DS3231 connected to specfied I2C pins |
cromda | 0:11dfd7ac4188 | 124 | * |
cromda | 0:11dfd7ac4188 | 125 | * @param sda The I2C data pin |
cromda | 0:11dfd7ac4188 | 126 | * @param scl The I2C clock pin |
cromda | 0:11dfd7ac4188 | 127 | */ |
cromda | 0:11dfd7ac4188 | 128 | DS3231(PinName sda, PinName scl); |
cromda | 0:11dfd7ac4188 | 129 | |
cromda | 0:11dfd7ac4188 | 130 | /** set I2C bus speed |
cromda | 0:11dfd7ac4188 | 131 | * @param frequency : I2C clocl frequenct (Hz) |
cromda | 0:11dfd7ac4188 | 132 | */ |
cromda | 0:11dfd7ac4188 | 133 | void setI2Cfrequency(int frequency); |
cromda | 0:11dfd7ac4188 | 134 | |
cromda | 0:11dfd7ac4188 | 135 | /** Read the temperature |
cromda | 0:11dfd7ac4188 | 136 | * |
cromda | 0:11dfd7ac4188 | 137 | * @return The temperature |
cromda | 0:11dfd7ac4188 | 138 | */ |
cromda | 0:11dfd7ac4188 | 139 | float readTemp(); |
cromda | 0:11dfd7ac4188 | 140 | |
cromda | 0:11dfd7ac4188 | 141 | /** Read the time registers |
cromda | 0:11dfd7ac4188 | 142 | * @param hours |
cromda | 0:11dfd7ac4188 | 143 | * @param minutes |
cromda | 0:11dfd7ac4188 | 144 | * @param seconds |
cromda | 0:11dfd7ac4188 | 145 | */ |
cromda | 0:11dfd7ac4188 | 146 | void readTime(int *hours, int *minutes, int *seconds); |
cromda | 0:11dfd7ac4188 | 147 | |
cromda | 0:11dfd7ac4188 | 148 | |
cromda | 0:11dfd7ac4188 | 149 | /** Set the time registers |
cromda | 0:11dfd7ac4188 | 150 | * @param hours |
cromda | 0:11dfd7ac4188 | 151 | * @param minutes |
cromda | 0:11dfd7ac4188 | 152 | * @param seconds |
cromda | 0:11dfd7ac4188 | 153 | */ |
cromda | 0:11dfd7ac4188 | 154 | void setTime(int hours, int minutes, int seconds); |
cromda | 0:11dfd7ac4188 | 155 | |
cromda | 0:11dfd7ac4188 | 156 | /** Read the date registers |
cromda | 0:11dfd7ac4188 | 157 | * @param date |
cromda | 0:11dfd7ac4188 | 158 | * @param month |
cromda | 0:11dfd7ac4188 | 159 | * @param year |
cromda | 0:11dfd7ac4188 | 160 | */ |
cromda | 0:11dfd7ac4188 | 161 | void readDate(int *date, int *month, int *year); |
cromda | 0:11dfd7ac4188 | 162 | |
cromda | 0:11dfd7ac4188 | 163 | /** Set the date registers |
cromda | 0:11dfd7ac4188 | 164 | * @param dayOfWeek : day of week |
cromda | 0:11dfd7ac4188 | 165 | * @param date |
cromda | 0:11dfd7ac4188 | 166 | * @param month |
cromda | 0:11dfd7ac4188 | 167 | * @param year |
cromda | 0:11dfd7ac4188 | 168 | */ |
cromda | 0:11dfd7ac4188 | 169 | void setDate(int dayOfWeek, int date, int month, int year); |
cromda | 0:11dfd7ac4188 | 170 | |
cromda | 0:11dfd7ac4188 | 171 | /** Read the date and time registers |
cromda | 0:11dfd7ac4188 | 172 | * @param dayOfWeek : day of week |
cromda | 0:11dfd7ac4188 | 173 | * @param date |
cromda | 0:11dfd7ac4188 | 174 | * @param month |
cromda | 0:11dfd7ac4188 | 175 | * @param year |
cromda | 0:11dfd7ac4188 | 176 | * @param hours |
cromda | 0:11dfd7ac4188 | 177 | * @param minutes |
cromda | 0:11dfd7ac4188 | 178 | * @param seconds |
cromda | 0:11dfd7ac4188 | 179 | */ |
cromda | 0:11dfd7ac4188 | 180 | void readDateTime(int *dayOfWeek, int *date, int *month, int *year, int *hours, int *minutes, int *seconds); |
cromda | 0:11dfd7ac4188 | 181 | |
cromda | 0:11dfd7ac4188 | 182 | /** Read a register |
cromda | 0:11dfd7ac4188 | 183 | * @param reg : register address |
cromda | 0:11dfd7ac4188 | 184 | * @return The register content |
cromda | 0:11dfd7ac4188 | 185 | */ |
cromda | 0:11dfd7ac4188 | 186 | int readRegister(char reg); |
cromda | 0:11dfd7ac4188 | 187 | |
cromda | 0:11dfd7ac4188 | 188 | /** Write to a register |
cromda | 0:11dfd7ac4188 | 189 | * @param reg : register address |
cromda | 0:11dfd7ac4188 | 190 | * @param The register content |
cromda | 0:11dfd7ac4188 | 191 | */ |
cromda | 0:11dfd7ac4188 | 192 | void writeRegister(int reg,char byte); |
cromda | 0:11dfd7ac4188 | 193 | |
cromda | 0:11dfd7ac4188 | 194 | /** set OSF (Oscillator Stop Flag) bit to 0 in Control Status register |
cromda | 0:11dfd7ac4188 | 195 | * should be done just after power up DS3231 |
cromda | 0:11dfd7ac4188 | 196 | * OSF bit is automaticaly set to 1 when on power up or when the DS3231 oscillator stops |
cromda | 0:11dfd7ac4188 | 197 | */ |
cromda | 0:11dfd7ac4188 | 198 | void eraseOSF(); |
cromda | 0:11dfd7ac4188 | 199 | |
cromda | 0:11dfd7ac4188 | 200 | /** Return OSF bit. If true the oscillator stopped or the DS3231 just powered up |
cromda | 0:11dfd7ac4188 | 201 | * @return The OSF bit |
cromda | 0:11dfd7ac4188 | 202 | */ |
cromda | 0:11dfd7ac4188 | 203 | bool OSF(); |
cromda | 0:11dfd7ac4188 | 204 | |
cromda | 0:11dfd7ac4188 | 205 | bool error; |
cromda | 0:11dfd7ac4188 | 206 | |
cromda | 0:11dfd7ac4188 | 207 | private : |
cromda | 0:11dfd7ac4188 | 208 | I2C i2c; |
cromda | 0:11dfd7ac4188 | 209 | int bcd2dec(int k); // bcd to decimal conversion |
cromda | 0:11dfd7ac4188 | 210 | int dec2bcd(int k); // decimal to bcd conversion |
cromda | 0:11dfd7ac4188 | 211 | void decodeTime(int regHours, int regMinutes, int regSeconds,int *Hours, int *Minutes, int *Seconds); |
cromda | 0:11dfd7ac4188 | 212 | void decodeDate(int regDate,int regMonth, int regYear, int *date, int *month, int *year); |
cromda | 0:11dfd7ac4188 | 213 | }; |
cromda | 0:11dfd7ac4188 | 214 | |
cromda | 0:11dfd7ac4188 | 215 | |
cromda | 0:11dfd7ac4188 | 216 | #endif |