|
| 1 | +/*----------------------------------------------------------------------* |
| 2 | + * DS3232RTC.cpp - 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 | + * In addition, this library implements functions to support the * |
| 13 | + * additional features of the DS3232. * |
| 14 | + * * |
| 15 | + * This library will also work with the DS3231 RTC, which has the same * |
| 16 | + * features of the DS3232 except: (1) Battery-backed SRAM, (2) Battery- * |
| 17 | + * backed 32kHz output (BB32kHz bit in Control/Status register 0x0F), * |
| 18 | + * and (3) Adjustable temperature sensor sample rate (CRATE1:0 bits in * |
| 19 | + * the Control/Status register). * |
| 20 | + * * |
| 21 | + * Whether used with the DS3232 or DS3231, the user is responsible for * |
| 22 | + * ensuring reads and writes do not exceed the device's address space * |
| 23 | + * (0x00-0x12 for DS3231, 0x00-0xFF for DS3232); no bounds checking * |
| 24 | + * is done by this library. * |
| 25 | + * * |
| 26 | + * Jack Christensen 06Mar2013 * |
| 27 | + * 28Aug2013 Changed the lower level methods to return the status of * |
| 28 | + * the I2C communication with the RTC. Thanks to * |
| 29 | + * Rob Tillaart for the suggestion. (Fixes issue #1.) * |
| 30 | + * * |
| 31 | + * This work is licensed under the Creative Commons Attribution- * |
| 32 | + * ShareAlike 3.0 Unported License. To view a copy of this license, * |
| 33 | + * visit http://creativecommons.org/licenses/by-sa/3.0/ or send a * |
| 34 | + * letter to Creative Commons, 444 Castro Street, Suite 900, * |
| 35 | + * Mountain View, CA 94041. * |
| 36 | + *----------------------------------------------------------------------*/ |
| 37 | + |
| 38 | +#include <DS3232RTC.h> |
| 39 | + |
| 40 | +//define release-independent I2C functions |
| 41 | +#if defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) |
| 42 | +#include <TinyWireM.h> |
| 43 | +#define i2cBegin TinyWireM.begin |
| 44 | +#define i2cBeginTransmission TinyWireM.beginTransmission |
| 45 | +#define i2cEndTransmission TinyWireM.endTransmission |
| 46 | +#define i2cRequestFrom TinyWireM.requestFrom |
| 47 | +#define i2cRead TinyWireM.receive |
| 48 | +#define i2cWrite TinyWireM.send |
| 49 | +#elif ARDUINO >= 100 |
| 50 | +#include <Wire.h> |
| 51 | +#define i2cBegin Wire.begin |
| 52 | +#define i2cBeginTransmission Wire.beginTransmission |
| 53 | +#define i2cEndTransmission Wire.endTransmission |
| 54 | +#define i2cRequestFrom Wire.requestFrom |
| 55 | +#define i2cRead Wire.read |
| 56 | +#define i2cWrite Wire.write |
| 57 | +#else |
| 58 | +#include <Wire.h> |
| 59 | +#define i2cBegin Wire.begin |
| 60 | +#define i2cBeginTransmission Wire.beginTransmission |
| 61 | +#define i2cEndTransmission Wire.endTransmission |
| 62 | +#define i2cRequestFrom Wire.requestFrom |
| 63 | +#define i2cRead Wire.receive |
| 64 | +#define i2cWrite Wire.send |
| 65 | +#endif |
| 66 | + |
| 67 | +/*----------------------------------------------------------------------* |
| 68 | + * Constructor. * |
| 69 | + *----------------------------------------------------------------------*/ |
| 70 | +DS3232RTC::DS3232RTC() |
| 71 | +{ |
| 72 | + i2cBegin(); |
| 73 | +} |
| 74 | + |
| 75 | +/*----------------------------------------------------------------------* |
| 76 | + * Read the current time from the RTC and return it as a time_t value. * |
| 77 | + * Returns a zero value if an I2C error occurred (e.g. RTC * |
| 78 | + * not present). * |
| 79 | + *----------------------------------------------------------------------*/ |
| 80 | +time_t DS3232RTC::get() |
| 81 | +{ |
| 82 | + tmElements_t tm; |
| 83 | + |
| 84 | + if ( read(tm) ) return 0; |
| 85 | + return( makeTime(tm) ); |
| 86 | +} |
| 87 | + |
| 88 | +/*----------------------------------------------------------------------* |
| 89 | + * Set the RTC to the given time_t value. * |
| 90 | + * Returns the I2C status (zero if successful). * |
| 91 | + *----------------------------------------------------------------------*/ |
| 92 | +byte DS3232RTC::set(time_t t) |
| 93 | +{ |
| 94 | + tmElements_t tm; |
| 95 | + |
| 96 | + breakTime(t, tm); |
| 97 | + return ( write(tm) ); |
| 98 | +} |
| 99 | + |
| 100 | +/*----------------------------------------------------------------------* |
| 101 | + * Read the current time from the RTC and return it in a tmElements_t * |
| 102 | + * structure. Returns the I2C status (zero if successful). * |
| 103 | + *----------------------------------------------------------------------*/ |
| 104 | +byte DS3232RTC::read(tmElements_t &tm) |
| 105 | +{ |
| 106 | + i2cBeginTransmission(RTC_ADDR); |
| 107 | + i2cWrite((uint8_t)RTC_SECONDS); |
| 108 | + if ( byte e = i2cEndTransmission() ) return e; |
| 109 | + //request 7 bytes (secs, min, hr, dow, date, mth, yr) |
| 110 | + i2cRequestFrom(RTC_ADDR, tmNbrFields); |
| 111 | + tm.Second = bcd2dec(i2cRead() & ~_BV(DS1307_CH)); |
| 112 | + tm.Minute = bcd2dec(i2cRead()); |
| 113 | + tm.Hour = bcd2dec(i2cRead() & ~_BV(HR1224)); //assumes 24hr clock |
| 114 | + tm.Wday = i2cRead(); |
| 115 | + tm.Day = bcd2dec(i2cRead()); |
| 116 | + tm.Month = bcd2dec(i2cRead() & ~_BV(CENTURY)); //don't use the Century bit |
| 117 | + tm.Year = y2kYearToTm(bcd2dec(i2cRead())); |
| 118 | + return 0; |
| 119 | +} |
| 120 | + |
| 121 | +/*----------------------------------------------------------------------* |
| 122 | + * Set the RTC's time from a tmElements_t structure. * |
| 123 | + * Returns the I2C status (zero if successful). * |
| 124 | + *----------------------------------------------------------------------*/ |
| 125 | +byte DS3232RTC::write(tmElements_t &tm) |
| 126 | +{ |
| 127 | + i2cBeginTransmission(RTC_ADDR); |
| 128 | + i2cWrite((uint8_t)RTC_SECONDS); |
| 129 | + i2cWrite(dec2bcd(tm.Second)); |
| 130 | + i2cWrite(dec2bcd(tm.Minute)); |
| 131 | + i2cWrite(dec2bcd(tm.Hour)); //sets 24 hour format (Bit 6 == 0) |
| 132 | + i2cWrite(tm.Wday); |
| 133 | + i2cWrite(dec2bcd(tm.Day)); |
| 134 | + i2cWrite(dec2bcd(tm.Month)); |
| 135 | + i2cWrite(dec2bcd(tmYearToY2k(tm.Year))); |
| 136 | + return i2cEndTransmission(); |
| 137 | +} |
| 138 | + |
| 139 | +/*----------------------------------------------------------------------* |
| 140 | + * Write multiple bytes to RTC RAM. * |
| 141 | + * Valid address range is 0x00 - 0xFF, no checking. * |
| 142 | + * Number of bytes (nBytes) must be between 1 and 31 (Wire library * |
| 143 | + * limitation). * |
| 144 | + * Returns the I2C status (zero if successful). * |
| 145 | + *----------------------------------------------------------------------*/ |
| 146 | +byte DS3232RTC::writeRTC(byte addr, byte *values, byte nBytes) |
| 147 | +{ |
| 148 | + i2cBeginTransmission(RTC_ADDR); |
| 149 | + i2cWrite(addr); |
| 150 | + for (byte i=0; i<nBytes; i++) i2cWrite(values[i]); |
| 151 | + return i2cEndTransmission(); |
| 152 | +} |
| 153 | + |
| 154 | +/*----------------------------------------------------------------------* |
| 155 | + * Write a single byte to RTC RAM. * |
| 156 | + * Valid address range is 0x00 - 0xFF, no checking. * |
| 157 | + * Returns the I2C status (zero if successful). * |
| 158 | + *----------------------------------------------------------------------*/ |
| 159 | +byte DS3232RTC::writeRTC(byte addr, byte value) |
| 160 | +{ |
| 161 | + return ( writeRTC(addr, &value, 1) ); |
| 162 | +} |
| 163 | + |
| 164 | +/*----------------------------------------------------------------------* |
| 165 | + * Read multiple bytes from RTC RAM. * |
| 166 | + * Valid address range is 0x00 - 0xFF, no checking. * |
| 167 | + * Number of bytes (nBytes) must be between 1 and 32 (Wire library * |
| 168 | + * limitation). * |
| 169 | + * Returns the I2C status (zero if successful). * |
| 170 | + *----------------------------------------------------------------------*/ |
| 171 | +byte DS3232RTC::readRTC(byte addr, byte *values, byte nBytes) |
| 172 | +{ |
| 173 | + i2cBeginTransmission(RTC_ADDR); |
| 174 | + i2cWrite(addr); |
| 175 | + if ( byte e = i2cEndTransmission() ) return e; |
| 176 | + i2cRequestFrom( (uint8_t)RTC_ADDR, nBytes ); |
| 177 | + for (byte i=0; i<nBytes; i++) values[i] = i2cRead(); |
| 178 | + return 0; |
| 179 | +} |
| 180 | + |
| 181 | +/*----------------------------------------------------------------------* |
| 182 | + * Read a single byte from RTC RAM. * |
| 183 | + * Valid address range is 0x00 - 0xFF, no checking. * |
| 184 | + *----------------------------------------------------------------------*/ |
| 185 | +byte DS3232RTC::readRTC(byte addr) |
| 186 | +{ |
| 187 | + byte b; |
| 188 | + |
| 189 | + readRTC(addr, &b, 1); |
| 190 | + return b; |
| 191 | +} |
| 192 | + |
| 193 | +/*----------------------------------------------------------------------* |
| 194 | + * Set an alarm time. Sets the alarm registers only. To cause the * |
| 195 | + * INT pin to be asserted on alarm match, use alarmInterrupt(). * |
| 196 | + * This method can set either Alarm 1 or Alarm 2, depending on the * |
| 197 | + * value of alarmType (use a value from the ALARM_TYPES_t enumeration). * |
| 198 | + * When setting Alarm 2, the seconds value must be supplied but is * |
| 199 | + * ignored, recommend using zero. (Alarm 2 has no seconds register.) * |
| 200 | + *----------------------------------------------------------------------*/ |
| 201 | +void DS3232RTC::setAlarm(ALARM_TYPES_t alarmType, byte seconds, byte minutes, byte hours, byte daydate) |
| 202 | +{ |
| 203 | + uint8_t addr; |
| 204 | + |
| 205 | + seconds = dec2bcd(seconds); |
| 206 | + minutes = dec2bcd(minutes); |
| 207 | + hours = dec2bcd(hours); |
| 208 | + daydate = dec2bcd(daydate); |
| 209 | + if (alarmType & 0x01) seconds |= _BV(A1M1); |
| 210 | + if (alarmType & 0x02) minutes |= _BV(A1M2); |
| 211 | + if (alarmType & 0x04) hours |= _BV(A1M3); |
| 212 | + if (alarmType & 0x10) hours |= _BV(DYDT); |
| 213 | + if (alarmType & 0x08) daydate |= _BV(A1M4); |
| 214 | + |
| 215 | + if ( !(alarmType & 0x80) ) { //alarm 1 |
| 216 | + addr = ALM1_SECONDS; |
| 217 | + writeRTC(addr++, seconds); |
| 218 | + } |
| 219 | + else { |
| 220 | + addr = ALM2_MINUTES; |
| 221 | + } |
| 222 | + writeRTC(addr++, minutes); |
| 223 | + writeRTC(addr++, hours); |
| 224 | + writeRTC(addr++, daydate); |
| 225 | +} |
| 226 | + |
| 227 | +/*----------------------------------------------------------------------* |
| 228 | + * Set an alarm time. Sets the alarm registers only. To cause the * |
| 229 | + * INT pin to be asserted on alarm match, use alarmInterrupt(). * |
| 230 | + * This method can set either Alarm 1 or Alarm 2, depending on the * |
| 231 | + * value of alarmType (use a value from the ALARM_TYPES_t enumeration). * |
| 232 | + * However, when using this method to set Alarm 1, the seconds value * |
| 233 | + * is set to zero. (Alarm 2 has no seconds register.) * |
| 234 | + *----------------------------------------------------------------------*/ |
| 235 | +void DS3232RTC::setAlarm(ALARM_TYPES_t alarmType, byte minutes, byte hours, byte daydate) |
| 236 | +{ |
| 237 | + setAlarm(alarmType, 0, minutes, hours, daydate); |
| 238 | +} |
| 239 | + |
| 240 | +/*----------------------------------------------------------------------* |
| 241 | + * Enable or disable an alarm "interrupt" which asserts the INT pin * |
| 242 | + * on the RTC. * |
| 243 | + *----------------------------------------------------------------------*/ |
| 244 | +void DS3232RTC::alarmInterrupt(byte alarmNumber, boolean interruptEnabled) |
| 245 | +{ |
| 246 | + uint8_t controlReg, mask; |
| 247 | + |
| 248 | + controlReg = readRTC(RTC_CONTROL); |
| 249 | + mask = _BV(A1IE) << (alarmNumber - 1); |
| 250 | + if (interruptEnabled) |
| 251 | + controlReg |= mask; |
| 252 | + else |
| 253 | + controlReg &= ~mask; |
| 254 | + writeRTC(RTC_CONTROL, controlReg); |
| 255 | +} |
| 256 | + |
| 257 | +/*----------------------------------------------------------------------* |
| 258 | + * Returns true or false depending on whether the given alarm has been * |
| 259 | + * triggered, and resets the alarm flag bit. * |
| 260 | + *----------------------------------------------------------------------*/ |
| 261 | +boolean DS3232RTC::alarm(byte alarmNumber) |
| 262 | +{ |
| 263 | + uint8_t statusReg, mask; |
| 264 | + |
| 265 | + statusReg = readRTC(RTC_STATUS); |
| 266 | + mask = _BV(A1F) << (alarmNumber - 1); |
| 267 | + if (statusReg & mask) { |
| 268 | + statusReg &= ~mask; |
| 269 | + writeRTC(RTC_STATUS, statusReg); |
| 270 | + return true; |
| 271 | + } |
| 272 | + else { |
| 273 | + return false; |
| 274 | + } |
| 275 | +} |
| 276 | + |
| 277 | +/*----------------------------------------------------------------------* |
| 278 | + * Enable or disable the square wave output. * |
| 279 | + * Use a value from the SQWAVE_FREQS_t enumeration for the parameter. * |
| 280 | + *----------------------------------------------------------------------*/ |
| 281 | +void DS3232RTC::squareWave(SQWAVE_FREQS_t freq) |
| 282 | +{ |
| 283 | + uint8_t controlReg; |
| 284 | + |
| 285 | + controlReg = readRTC(RTC_CONTROL); |
| 286 | + if (freq >= SQWAVE_NONE) { |
| 287 | + controlReg |= _BV(INTCN); |
| 288 | + } |
| 289 | + else { |
| 290 | + controlReg = (controlReg & 0xE3) | (freq << RS1); |
| 291 | + } |
| 292 | + writeRTC(RTC_CONTROL, controlReg); |
| 293 | +} |
| 294 | + |
| 295 | +/*----------------------------------------------------------------------* |
| 296 | + * Checks the OSF bit in the status register which indicates that the * |
| 297 | + * oscillator is or was stopped. * |
| 298 | + *----------------------------------------------------------------------*/ |
| 299 | +boolean DS3232RTC::oscStopped(void) |
| 300 | +{ |
| 301 | + return ( readRTC(RTC_STATUS) & _BV(OSF) ); |
| 302 | +} |
| 303 | + |
| 304 | +/*----------------------------------------------------------------------* |
| 305 | + * Returns the temperature in Celsius times four. * |
| 306 | + *----------------------------------------------------------------------*/ |
| 307 | +int DS3232RTC::temperature(void) |
| 308 | +{ |
| 309 | + union int16_byte { |
| 310 | + int i; |
| 311 | + byte b[2]; |
| 312 | + } rtcTemp; |
| 313 | + |
| 314 | + rtcTemp.b[0] = readRTC(TEMP_LSB); |
| 315 | + rtcTemp.b[1] = readRTC(TEMP_MSB); |
| 316 | + return rtcTemp.i >> 6; |
| 317 | +} |
| 318 | + |
| 319 | +/*----------------------------------------------------------------------* |
| 320 | + * Decimal-to-BCD conversion * |
| 321 | + *----------------------------------------------------------------------*/ |
| 322 | +uint8_t DS3232RTC::dec2bcd(uint8_t n) |
| 323 | +{ |
| 324 | + return n + 6 * (n / 10); |
| 325 | +} |
| 326 | + |
| 327 | +/*----------------------------------------------------------------------* |
| 328 | + * BCD-to-Decimal conversion * |
| 329 | + *----------------------------------------------------------------------*/ |
| 330 | +uint8_t __attribute__ ((noinline)) DS3232RTC::bcd2dec(uint8_t n) |
| 331 | +{ |
| 332 | + return n - 6 * (n >> 4); |
| 333 | +} |
| 334 | + |
| 335 | +DS3232RTC RTC = DS3232RTC(); //instantiate an RTC object |
0 commit comments