From 187a982dd5eae74c49cc00441706529a3be48211 Mon Sep 17 00:00:00 2001 From: JChristensen Date: Sun, 1 Apr 2018 15:18:14 -0400 Subject: [PATCH] Move defines not needed in .h file to .cpp file. Cosmetic and style changes. --- examples/SetSerial/SetSerial.ino | 8 +- examples/TimeRTC/TimeRTC.ino | 10 +- examples/alarm_ex1/alarm_ex1.ino | 4 +- examples/alarm_ex2/alarm_ex2.ino | 6 +- examples/alarm_ex3/alarm_ex3.ino | 8 +- examples/alarm_ex4/alarm_ex4.ino | 6 +- examples/alarm_ex5/alarm_ex5.ino | 4 +- examples/alarm_ex7/alarm_ex7.ino | 6 +- .../tiny3232_KnockBang/tiny3232_KnockBang.ino | 2 +- library.properties | 2 +- src/DS3232RTC.cpp | 306 +++++++++++------- src/DS3232RTC.h | 133 ++------ 12 files changed, 238 insertions(+), 257 deletions(-) diff --git a/examples/SetSerial/SetSerial.ino b/examples/SetSerial/SetSerial.ino index d413d48..97b8559 100644 --- a/examples/SetSerial/SetSerial.ino +++ b/examples/SetSerial/SetSerial.ino @@ -26,14 +26,14 @@ // in the input will result in an incorrect RTC setting. // // Jack Christensen 08Aug2013 - + #include // https://github.com/JChristensen/DS3232RTC #include // http://arduiniana.org/libraries/streaming/ void setup() { Serial.begin(115200); - + // setSyncProvider() causes the Time library to synchronize with the // external RTC by calling RTC.get() every five minutes by default. setSyncProvider(RTC.get); @@ -68,7 +68,7 @@ void loop() tm.Second = Serial.parseInt(); t = makeTime(tm); RTC.set(t); // use the time_t value to ensure correct weekday is set - setTime(t); + setTime(t); Serial << F("RTC set to: "); printDateTime(t); Serial << endl; @@ -76,7 +76,7 @@ void loop() while (Serial.available() > 0) Serial.read(); } } - + t = now(); if (t != tLast) { tLast = t; diff --git a/examples/TimeRTC/TimeRTC.ino b/examples/TimeRTC/TimeRTC.ino index 929e482..9ca32dd 100644 --- a/examples/TimeRTC/TimeRTC.ino +++ b/examples/TimeRTC/TimeRTC.ino @@ -11,15 +11,15 @@ void setup() { Serial.begin(9600); setSyncProvider(RTC.get); // the function to get the time from the RTC - if(timeStatus() != timeSet) + if(timeStatus() != timeSet) Serial.println("Unable to sync with the RTC"); else - Serial.println("RTC has set the system time"); + Serial.println("RTC has set the system time"); } void loop() { - digitalClockDisplay(); + digitalClockDisplay(); delay(1000); } @@ -34,8 +34,8 @@ void digitalClockDisplay() Serial.print(' '); Serial.print(month()); Serial.print(' '); - Serial.print(year()); - Serial.println(); + Serial.print(year()); + Serial.println(); } void printDigits(int digits) diff --git a/examples/alarm_ex1/alarm_ex1.ino b/examples/alarm_ex1/alarm_ex1.ino index 6edf6f4..6343ef2 100644 --- a/examples/alarm_ex1/alarm_ex1.ino +++ b/examples/alarm_ex1/alarm_ex1.ino @@ -21,7 +21,7 @@ void setup() { Serial.begin(115200); - + // initialize the alarms to known values, clear the alarm flags, clear the alarm interrupt flags RTC.setAlarm(ALM1_MATCH_DATE, 0, 0, 0, 1); RTC.setAlarm(ALM2_MATCH_DATE, 0, 0, 0, 1); @@ -33,7 +33,7 @@ void setup() // set the RTC time and date to the compile time RTC.set(compileTime()); - + // set Alarm 1 to occur at 5 seconds after every minute RTC.setAlarm(ALM1_MATCH_SECONDS, 5, 0, 0, 1); // clear the alarm flag diff --git a/examples/alarm_ex2/alarm_ex2.ino b/examples/alarm_ex2/alarm_ex2.ino index 8bec402..8c57256 100644 --- a/examples/alarm_ex2/alarm_ex2.ino +++ b/examples/alarm_ex2/alarm_ex2.ino @@ -16,7 +16,7 @@ // Connect RTC INT/SQW to Arduino pin 2. // // Jack Christensen 16Sep2017 - + #include // https://github.com/JChristensen/DS3232RTC #include // http://arduiniana.org/libraries/streaming/ @@ -36,7 +36,7 @@ void setup() RTC.alarmInterrupt(ALARM_1, false); RTC.alarmInterrupt(ALARM_2, false); RTC.squareWave(SQWAVE_NONE); - + tmElements_t tm; tm.Hour = 00; // set the RTC to an arbitrary time tm.Minute = 00; @@ -45,7 +45,7 @@ void setup() tm.Month = 9; tm.Year = 2017 - 1970; // tmElements_t.Year is the offset from 1970 RTC.write(tm); // set the RTC from the tm structure - + // set Alarm 1 to occur at 5 seconds after every minute RTC.setAlarm(ALM1_MATCH_SECONDS, 5, 0, 0, 0); // clear the alarm flag diff --git a/examples/alarm_ex3/alarm_ex3.ino b/examples/alarm_ex3/alarm_ex3.ino index d64d333..e9a9b6f 100644 --- a/examples/alarm_ex3/alarm_ex3.ino +++ b/examples/alarm_ex3/alarm_ex3.ino @@ -17,7 +17,7 @@ // Connect RTC SCL to Arduino pin A5. // // Jack Christensen 16Sep2017 - + #include // https://github.com/JChristensen/DS3232RTC #include // http://arduiniana.org/libraries/streaming/ @@ -45,11 +45,11 @@ void setup() tm.Year = 2017 - 1970; // tmElements_t.Year is the offset from 1970 time_t t = makeTime(tm); // change the tm structure into time_t (seconds since epoch) time_t alarmTime = t + ALARM_INTERVAL; // calculate the alarm time - + // set the current time RTC.set(t); // set the alarm - RTC.setAlarm(ALM1_MATCH_HOURS, second(alarmTime), minute(alarmTime), hour(alarmTime), 0); + RTC.setAlarm(ALM1_MATCH_HOURS, second(alarmTime), minute(alarmTime), hour(alarmTime), 0); // clear the alarm flag RTC.alarm(ALARM_1); @@ -68,7 +68,7 @@ void loop() // calculate the next alarm time time_t alarmTime = t + ALARM_INTERVAL; // set the alarm - RTC.setAlarm(ALM1_MATCH_HOURS, second(alarmTime), minute(alarmTime), hour(alarmTime), 0); + RTC.setAlarm(ALM1_MATCH_HOURS, second(alarmTime), minute(alarmTime), hour(alarmTime), 0); Serial << millis() << " ALARM_1 "; printDateTime(t); diff --git a/examples/alarm_ex4/alarm_ex4.ino b/examples/alarm_ex4/alarm_ex4.ino index 6afaa0d..b0b25fe 100644 --- a/examples/alarm_ex4/alarm_ex4.ino +++ b/examples/alarm_ex4/alarm_ex4.ino @@ -14,7 +14,7 @@ // Connect RTC SCL to Arduino pin A5. // // Jack Christensen 16Sep2017 - + #include // https://github.com/JChristensen/DS3232RTC #include // http://arduiniana.org/libraries/streaming/ @@ -30,7 +30,7 @@ void setup() RTC.alarmInterrupt(ALARM_1, false); RTC.alarmInterrupt(ALARM_2, false); RTC.squareWave(SQWAVE_NONE); - + tmElements_t tm; tm.Hour = 00; // set the RTC to an arbitrary time tm.Minute = 00; @@ -39,7 +39,7 @@ void setup() tm.Month = 9; tm.Year = 2017 - 1970; // tmElements_t.Year is the offset from 1970 RTC.write(tm); // set the RTC from the tm structure - + // set Alarm 1 to occur once per second RTC.setAlarm(ALM1_EVERY_SECOND, 0, 0, 0, 0); // clear the alarm flag diff --git a/examples/alarm_ex5/alarm_ex5.ino b/examples/alarm_ex5/alarm_ex5.ino index bd85340..cd0cca0 100644 --- a/examples/alarm_ex5/alarm_ex5.ino +++ b/examples/alarm_ex5/alarm_ex5.ino @@ -30,7 +30,7 @@ void setup() RTC.alarmInterrupt(ALARM_1, false); RTC.alarmInterrupt(ALARM_2, false); RTC.squareWave(SQWAVE_NONE); - + tmElements_t tm; tm.Hour = 00; // set the RTC to an arbitrary time tm.Minute = 00; @@ -39,7 +39,7 @@ void setup() tm.Month = 9; tm.Year = 2017 - 1970; // tmElements_t.Year is the offset from 1970 RTC.write(tm); // set the RTC from the tm structure - + // set Alarm 2 to occur once per minute RTC.setAlarm(ALM2_EVERY_MINUTE, 0, 0, 0, 0); // clear the alarm flag diff --git a/examples/alarm_ex7/alarm_ex7.ino b/examples/alarm_ex7/alarm_ex7.ino index 19da7b7..801384b 100644 --- a/examples/alarm_ex7/alarm_ex7.ino +++ b/examples/alarm_ex7/alarm_ex7.ino @@ -27,7 +27,7 @@ void setup() { Serial.begin(115200); pinMode(2, INPUT_PULLUP); - + // initialize the alarms to known values, clear the alarm flags, clear the alarm interrupt flags RTC.setAlarm(ALM1_MATCH_DATE, 0, 0, 0, 1); RTC.setAlarm(ALM2_MATCH_DATE, 0, 0, 0, 1); @@ -36,7 +36,7 @@ void setup() RTC.alarmInterrupt(ALARM_1, false); RTC.alarmInterrupt(ALARM_2, false); RTC.squareWave(SQWAVE_NONE); - + tmElements_t tm; tm.Hour = 06; // set the RTC time to 06:29:50 tm.Minute = 29; @@ -45,7 +45,7 @@ void setup() tm.Month = 9; tm.Year = 2017 - 1970; // tmElements_t.Year is the offset from 1970 RTC.write(tm); // set the RTC from the tm structure - + // set Alarm 2 for 06:30:00 RTC.setAlarm(ALM2_MATCH_HOURS, 0, 30, 6, 0); // clear the alarm flags diff --git a/examples/tiny3232_KnockBang/tiny3232_KnockBang.ino b/examples/tiny3232_KnockBang/tiny3232_KnockBang.ino index 1816d67..99cdf9c 100644 --- a/examples/tiny3232_KnockBang/tiny3232_KnockBang.ino +++ b/examples/tiny3232_KnockBang/tiny3232_KnockBang.ino @@ -23,7 +23,7 @@ void setup() { Debug.begin(250000); - + // setSyncProvider() causes the Time library to synchronize with the // external RTC by calling RTC.get() every five minutes by default. setSyncProvider(RTC.get); diff --git a/library.properties b/library.properties index 2259c99..ae87446 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=DS3232RTC -version=1.2.1 +version=1.2.2 author=Jack Christensen maintainer=Jack Christensen sentence=Arduino Library for Maxim Integrated DS3232 and DS3231 Real-Time Clocks. diff --git a/src/DS3232RTC.cpp b/src/DS3232RTC.cpp index bfb4e22..a8e08dc 100644 --- a/src/DS3232RTC.cpp +++ b/src/DS3232RTC.cpp @@ -18,46 +18,133 @@ // to indicate whether I2C initialization should occur in the // constructor; this parameter defaults to true if not given. +#if defined(ARDUINO) && ARDUINO >= 100 +#include +#else +#include +#endif + +// define release-independent I2C functions +#if defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) +#include +#define i2cBegin TinyWireM.begin +#define i2cBeginTransmission TinyWireM.beginTransmission +#define i2cEndTransmission TinyWireM.endTransmission +#define i2cRequestFrom TinyWireM.requestFrom +#define i2cRead TinyWireM.receive +#define i2cWrite TinyWireM.send +#elif ARDUINO >= 100 +#include +#define i2cBegin Wire.begin +#define i2cBeginTransmission Wire.beginTransmission +#define i2cEndTransmission Wire.endTransmission +#define i2cRequestFrom Wire.requestFrom +#define i2cRead Wire.read +#define i2cWrite Wire.write +#else +#include +#define i2cBegin Wire.begin +#define i2cBeginTransmission Wire.beginTransmission +#define i2cEndTransmission Wire.endTransmission +#define i2cRequestFrom Wire.requestFrom +#define i2cRead Wire.receive +#define i2cWrite Wire.send +#endif + +#include //https://github.com/PaulStoffregen/Time #include -byte DS3232RTC::errCode; //for debug +// DS3232 I2C Address +#define RTC_ADDR 0x68 + +// DS3232 Register Addresses +#define RTC_SECONDS 0x00 +#define RTC_MINUTES 0x01 +#define RTC_HOURS 0x02 +#define RTC_DAY 0x03 +#define RTC_DATE 0x04 +#define RTC_MONTH 0x05 +#define RTC_YEAR 0x06 +#define ALM1_SECONDS 0x07 +#define ALM1_MINUTES 0x08 +#define ALM1_HOURS 0x09 +#define ALM1_DAYDATE 0x0A +#define ALM2_MINUTES 0x0B +#define ALM2_HOURS 0x0C +#define ALM2_DAYDATE 0x0D +#define RTC_CONTROL 0x0E +#define RTC_STATUS 0x0F +#define RTC_AGING 0x10 +#define RTC_TEMP_MSB 0x11 +#define RTC_TEMP_LSB 0x12 +#define SRAM_START_ADDR 0x14 // first SRAM address +#define SRAM_SIZE 236 // number of bytes of SRAM + +// Alarm mask bits +#define A1M1 7 +#define A1M2 7 +#define A1M3 7 +#define A1M4 7 +#define A2M2 7 +#define A2M3 7 +#define A2M4 7 -/*----------------------------------------------------------------------* - * Constructor. Initializes the I2C bus by default, but better * - * practice is to pass false in the constructor and call * - * the begin() function in the setup code. * - *----------------------------------------------------------------------*/ +// Control register bits +#define EOSC 7 +#define BBSQW 6 +#define CONV 5 +#define RS2 4 +#define RS1 3 +#define INTCN 2 +#define A2IE 1 +#define A1IE 0 + +// Status register bits +#define OSF 7 +#define BB32KHZ 6 +#define CRATE1 5 +#define CRATE0 4 +#define EN32KHZ 3 +#define BSY 2 +#define A2F 1 +#define A1F 0 + +// Other +#define DS1307_CH 7 // for DS1307 compatibility, Clock Halt bit in Seconds register +#define HR1224 6 // Hours register 12 or 24 hour mode (24 hour mode==0) +#define CENTURY 7 // Century bit in Month register +#define DYDT 6 // Day/Date flag bit in alarm Day/Date registers + +byte DS3232RTC::errCode; // for debug + +// Constructor. Initializes the I2C bus by default, but better +// practice is to pass false in the constructor and call +// the begin() function in the setup code. DS3232RTC::DS3232RTC(bool initI2C) { if (initI2C) i2cBegin(); } -/*----------------------------------------------------------------------* - * Initialize the I2C bus. * - *----------------------------------------------------------------------*/ +// Initialize the I2C bus. void DS3232RTC::begin() { i2cBegin(); } - -/*----------------------------------------------------------------------* - * Reads the current time from the RTC and returns it as a time_t * - * value. Returns a zero value if an I2C error occurred (e.g. RTC * - * not present). * - *----------------------------------------------------------------------*/ + +// Read the current time from the RTC and return it as a time_t +// value. Returns a zero value if an I2C error occurred (e.g. RTC +// not present). time_t DS3232RTC::get() { tmElements_t tm; - + if ( read(tm) ) return 0; return( makeTime(tm) ); } -/*----------------------------------------------------------------------* - * Sets the RTC to the given time_t value and clears the * - * oscillator stop flag (OSF) in the Control/Status register. * - * Returns the I2C status (zero if successful). * - *----------------------------------------------------------------------*/ +// Set the RTC to the given time_t value and clear the +// oscillator stop flag (OSF) in the Control/Status register. +// Returns the I2C status (zero if successful). byte DS3232RTC::set(time_t t) { tmElements_t tm; @@ -66,56 +153,50 @@ byte DS3232RTC::set(time_t t) return ( write(tm) ); } -/*----------------------------------------------------------------------* - * Reads the current time from the RTC and returns it in a tmElements_t * - * structure. Returns the I2C status (zero if successful). * - *----------------------------------------------------------------------*/ +// Read the current time from the RTC and return it in a tmElements_t +// structure. Returns the I2C status (zero if successful). byte DS3232RTC::read(tmElements_t &tm) { i2cBeginTransmission(RTC_ADDR); i2cWrite((uint8_t)RTC_SECONDS); if ( byte e = i2cEndTransmission() ) { errCode = e; return e; } - //request 7 bytes (secs, min, hr, dow, date, mth, yr) + // request 7 bytes (secs, min, hr, dow, date, mth, yr) i2cRequestFrom(RTC_ADDR, tmNbrFields); - tm.Second = bcd2dec(i2cRead() & ~_BV(DS1307_CH)); + tm.Second = bcd2dec(i2cRead() & ~_BV(DS1307_CH)); tm.Minute = bcd2dec(i2cRead()); - tm.Hour = bcd2dec(i2cRead() & ~_BV(HR1224)); //assumes 24hr clock + tm.Hour = bcd2dec(i2cRead() & ~_BV(HR1224)); // assumes 24hr clock tm.Wday = i2cRead(); tm.Day = bcd2dec(i2cRead()); - tm.Month = bcd2dec(i2cRead() & ~_BV(CENTURY)); //don't use the Century bit + tm.Month = bcd2dec(i2cRead() & ~_BV(CENTURY)); // don't use the Century bit tm.Year = y2kYearToTm(bcd2dec(i2cRead())); return 0; } -/*----------------------------------------------------------------------* - * Sets the RTC's time from a tmElements_t structure and clears the * - * oscillator stop flag (OSF) in the Control/Status register. * - * Returns the I2C status (zero if successful). * - *----------------------------------------------------------------------*/ +// Set the RTC time from a tmElements_t structure and clear the +// oscillator stop flag (OSF) in the Control/Status register. +// Returns the I2C status (zero if successful). byte DS3232RTC::write(tmElements_t &tm) { i2cBeginTransmission(RTC_ADDR); i2cWrite((uint8_t)RTC_SECONDS); i2cWrite(dec2bcd(tm.Second)); i2cWrite(dec2bcd(tm.Minute)); - i2cWrite(dec2bcd(tm.Hour)); //sets 24 hour format (Bit 6 == 0) + i2cWrite(dec2bcd(tm.Hour)); // sets 24 hour format (Bit 6 == 0) i2cWrite(tm.Wday); i2cWrite(dec2bcd(tm.Day)); i2cWrite(dec2bcd(tm.Month)); - i2cWrite(dec2bcd(tmYearToY2k(tm.Year))); + i2cWrite(dec2bcd(tmYearToY2k(tm.Year))); byte ret = i2cEndTransmission(); - uint8_t s = readRTC(RTC_STATUS); //read the status register - writeRTC( RTC_STATUS, s & ~_BV(OSF) ); //clear the Oscillator Stop Flag + uint8_t s = readRTC(RTC_STATUS); // read the status register + writeRTC( RTC_STATUS, s & ~_BV(OSF) ); // clear the Oscillator Stop Flag return ret; } -/*----------------------------------------------------------------------* - * Write multiple bytes to RTC RAM. * - * Valid address range is 0x00 - 0xFF, no checking. * - * Number of bytes (nBytes) must be between 1 and 31 (Wire library * - * limitation). * - * Returns the I2C status (zero if successful). * - *----------------------------------------------------------------------*/ +// Write multiple bytes to RTC RAM. +// Valid address range is 0x00 - 0xFF, no checking. +// Number of bytes (nBytes) must be between 1 and 31 (Wire library +// limitation). +// Returns the I2C status (zero if successful). byte DS3232RTC::writeRTC(byte addr, byte *values, byte nBytes) { i2cBeginTransmission(RTC_ADDR); @@ -124,23 +205,19 @@ byte DS3232RTC::writeRTC(byte addr, byte *values, byte nBytes) return i2cEndTransmission(); } -/*----------------------------------------------------------------------* - * Write a single byte to RTC RAM. * - * Valid address range is 0x00 - 0xFF, no checking. * - * Returns the I2C status (zero if successful). * - *----------------------------------------------------------------------*/ +// Write a single byte to RTC RAM. +// Valid address range is 0x00 - 0xFF, no checking. +// Returns the I2C status (zero if successful). byte DS3232RTC::writeRTC(byte addr, byte value) { return ( writeRTC(addr, &value, 1) ); } -/*----------------------------------------------------------------------* - * Read multiple bytes from RTC RAM. * - * Valid address range is 0x00 - 0xFF, no checking. * - * Number of bytes (nBytes) must be between 1 and 32 (Wire library * - * limitation). * - * Returns the I2C status (zero if successful). * - *----------------------------------------------------------------------*/ +// Read multiple bytes from RTC RAM. +// Valid address range is 0x00 - 0xFF, no checking. +// Number of bytes (nBytes) must be between 1 and 32 (Wire library +// limitation). +// Returns the I2C status (zero if successful). byte DS3232RTC::readRTC(byte addr, byte *values, byte nBytes) { i2cBeginTransmission(RTC_ADDR); @@ -151,30 +228,26 @@ byte DS3232RTC::readRTC(byte addr, byte *values, byte nBytes) return 0; } -/*----------------------------------------------------------------------* - * Read a single byte from RTC RAM. * - * Valid address range is 0x00 - 0xFF, no checking. * - *----------------------------------------------------------------------*/ +// Read a single byte from RTC RAM. +// Valid address range is 0x00 - 0xFF, no checking. byte DS3232RTC::readRTC(byte addr) { byte b; - + readRTC(addr, &b, 1); return b; } -/*----------------------------------------------------------------------* - * Set an alarm time. Sets the alarm registers only. To cause the * - * INT pin to be asserted on alarm match, use alarmInterrupt(). * - * This method can set either Alarm 1 or Alarm 2, depending on the * - * value of alarmType (use a value from the ALARM_TYPES_t enumeration). * - * When setting Alarm 2, the seconds value must be supplied but is * - * ignored, recommend using zero. (Alarm 2 has no seconds register.) * - *----------------------------------------------------------------------*/ +// Set an alarm time. Sets the alarm registers only. To cause the +// INT pin to be asserted on alarm match, use alarmInterrupt(). +// This method can set either Alarm 1 or Alarm 2, depending on the +// value of alarmType (use a value from the ALARM_TYPES_t enumeration). +// When setting Alarm 2, the seconds value must be supplied but is +// ignored, recommend using zero. (Alarm 2 has no seconds register.) void DS3232RTC::setAlarm(ALARM_TYPES_t alarmType, byte seconds, byte minutes, byte hours, byte daydate) { uint8_t addr; - + seconds = dec2bcd(seconds); minutes = dec2bcd(minutes); hours = dec2bcd(hours); @@ -184,12 +257,14 @@ void DS3232RTC::setAlarm(ALARM_TYPES_t alarmType, byte seconds, byte minutes, by if (alarmType & 0x04) hours |= _BV(A1M3); if (alarmType & 0x10) daydate |= _BV(DYDT); if (alarmType & 0x08) daydate |= _BV(A1M4); - - if ( !(alarmType & 0x80) ) { //alarm 1 + + if ( !(alarmType & 0x80) ) // alarm 1 + { addr = ALM1_SECONDS; writeRTC(addr++, seconds); } - else { + else + { addr = ALM2_MINUTES; } writeRTC(addr++, minutes); @@ -197,121 +272,110 @@ void DS3232RTC::setAlarm(ALARM_TYPES_t alarmType, byte seconds, byte minutes, by writeRTC(addr++, daydate); } -/*----------------------------------------------------------------------* - * Set an alarm time. Sets the alarm registers only. To cause the * - * INT pin to be asserted on alarm match, use alarmInterrupt(). * - * This method can set either Alarm 1 or Alarm 2, depending on the * - * value of alarmType (use a value from the ALARM_TYPES_t enumeration). * - * However, when using this method to set Alarm 1, the seconds value * - * is set to zero. (Alarm 2 has no seconds register.) * - *----------------------------------------------------------------------*/ +// Set an alarm time. Sets the alarm registers only. To cause the +// INT pin to be asserted on alarm match, use alarmInterrupt(). +// This method can set either Alarm 1 or Alarm 2, depending on the +// value of alarmType (use a value from the ALARM_TYPES_t enumeration). +// However, when using this method to set Alarm 1, the seconds value +// is set to zero. (Alarm 2 has no seconds register.) void DS3232RTC::setAlarm(ALARM_TYPES_t alarmType, byte minutes, byte hours, byte daydate) { setAlarm(alarmType, 0, minutes, hours, daydate); } -/*----------------------------------------------------------------------* - * Enable or disable an alarm "interrupt" which asserts the INT pin * - * on the RTC. * - *----------------------------------------------------------------------*/ +// Enable or disable an alarm "interrupt" which asserts the INT pin +// on the RTC. void DS3232RTC::alarmInterrupt(byte alarmNumber, bool interruptEnabled) { uint8_t controlReg, mask; - + controlReg = readRTC(RTC_CONTROL); mask = _BV(A1IE) << (alarmNumber - 1); if (interruptEnabled) controlReg |= mask; else controlReg &= ~mask; - writeRTC(RTC_CONTROL, controlReg); + writeRTC(RTC_CONTROL, controlReg); } -/*----------------------------------------------------------------------* - * Returns true or false depending on whether the given alarm has been * - * triggered, and resets the alarm flag bit. * - *----------------------------------------------------------------------*/ +// Returns true or false depending on whether the given alarm has been +// triggered, and resets the alarm flag bit. bool DS3232RTC::alarm(byte alarmNumber) { uint8_t statusReg, mask; - + statusReg = readRTC(RTC_STATUS); mask = _BV(A1F) << (alarmNumber - 1); - if (statusReg & mask) { + if (statusReg & mask) + { statusReg &= ~mask; writeRTC(RTC_STATUS, statusReg); return true; } - else { + else + { return false; } } -/*----------------------------------------------------------------------* - * Enable or disable the square wave output. * - * Use a value from the SQWAVE_FREQS_t enumeration for the parameter. * - *----------------------------------------------------------------------*/ +// Enable or disable the square wave output. +// Use a value from the SQWAVE_FREQS_t enumeration for the parameter. void DS3232RTC::squareWave(SQWAVE_FREQS_t freq) { uint8_t controlReg; controlReg = readRTC(RTC_CONTROL); - if (freq >= SQWAVE_NONE) { + if (freq >= SQWAVE_NONE) + { controlReg |= _BV(INTCN); } - else { + else + { controlReg = (controlReg & 0xE3) | (freq << RS1); } writeRTC(RTC_CONTROL, controlReg); } -/*----------------------------------------------------------------------* - * Returns the value of the oscillator stop flag (OSF) bit in the * - * control/status register which indicates that the oscillator is or * - * was stopped, and that the timekeeping data may be invalid. * - * Optionally clears the OSF bit depending on the argument passed. * - *----------------------------------------------------------------------*/ +// Returns the value of the oscillator stop flag (OSF) bit in the +// control/status register which indicates that the oscillator is or * +// was stopped, and that the timekeeping data may be invalid. +// Optionally clears the OSF bit depending on the argument passed. bool DS3232RTC::oscStopped(bool clearOSF) { - uint8_t s = readRTC(RTC_STATUS); //read the status register - bool ret = s & _BV(OSF); //isolate the osc stop flag to return to caller - if (ret && clearOSF) { //clear OSF if it's set and the caller wants to clear it + uint8_t s = readRTC(RTC_STATUS); // read the status register + bool ret = s & _BV(OSF); // isolate the osc stop flag to return to caller + if (ret && clearOSF) // clear OSF if it's set and the caller wants to clear it + { writeRTC( RTC_STATUS, s & ~_BV(OSF) ); } return ret; } -/*----------------------------------------------------------------------* - * Returns the temperature in Celsius times four. * - *----------------------------------------------------------------------*/ +// Returns the temperature in Celsius times four. int DS3232RTC::temperature() { union int16_byte { int i; byte b[2]; } rtcTemp; - + rtcTemp.b[0] = readRTC(RTC_TEMP_LSB); rtcTemp.b[1] = readRTC(RTC_TEMP_MSB); return rtcTemp.i / 64; } -/*----------------------------------------------------------------------* - * Decimal-to-BCD conversion * - *----------------------------------------------------------------------*/ +// Decimal-to-BCD conversion uint8_t DS3232RTC::dec2bcd(uint8_t n) { return n + 6 * (n / 10); } -/*----------------------------------------------------------------------* - * BCD-to-Decimal conversion * - *----------------------------------------------------------------------*/ +// BCD-to-Decimal conversion uint8_t __attribute__ ((noinline)) DS3232RTC::bcd2dec(uint8_t n) { return n - 6 * (n >> 4); } #ifdef ARDUINO_ARCH_AVR -DS3232RTC RTC; //instantiate an RTC object +DS3232RTC RTC; // instantiate an RTC object #endif diff --git a/src/DS3232RTC.h b/src/DS3232RTC.h index 2dfe2f4..0887d0f 100644 --- a/src/DS3232RTC.h +++ b/src/DS3232RTC.h @@ -20,129 +20,42 @@ #ifndef DS3232RTC_H_INCLUDED #define DS3232RTC_H_INCLUDED -#include -#if defined(ARDUINO) && ARDUINO >= 100 -#include -#else -#include -#endif - -//define release-independent I2C functions -#if defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) -#include -#define i2cBegin TinyWireM.begin -#define i2cBeginTransmission TinyWireM.beginTransmission -#define i2cEndTransmission TinyWireM.endTransmission -#define i2cRequestFrom TinyWireM.requestFrom -#define i2cRead TinyWireM.receive -#define i2cWrite TinyWireM.send -#elif ARDUINO >= 100 -#include -#define i2cBegin Wire.begin -#define i2cBeginTransmission Wire.beginTransmission -#define i2cEndTransmission Wire.endTransmission -#define i2cRequestFrom Wire.requestFrom -#define i2cRead Wire.read -#define i2cWrite Wire.write -#else -#include -#define i2cBegin Wire.begin -#define i2cBeginTransmission Wire.beginTransmission -#define i2cEndTransmission Wire.endTransmission -#define i2cRequestFrom Wire.requestFrom -#define i2cRead Wire.receive -#define i2cWrite Wire.send -#endif - -//DS3232 I2C Address -#define RTC_ADDR 0x68 - -//DS3232 Register Addresses -#define RTC_SECONDS 0x00 -#define RTC_MINUTES 0x01 -#define RTC_HOURS 0x02 -#define RTC_DAY 0x03 -#define RTC_DATE 0x04 -#define RTC_MONTH 0x05 -#define RTC_YEAR 0x06 -#define ALM1_SECONDS 0x07 -#define ALM1_MINUTES 0x08 -#define ALM1_HOURS 0x09 -#define ALM1_DAYDATE 0x0A -#define ALM2_MINUTES 0x0B -#define ALM2_HOURS 0x0C -#define ALM2_DAYDATE 0x0D -#define RTC_CONTROL 0x0E -#define RTC_STATUS 0x0F -#define RTC_AGING 0x10 -#define RTC_TEMP_MSB 0x11 -#define RTC_TEMP_LSB 0x12 -#define SRAM_START_ADDR 0x14 //first SRAM address -#define SRAM_SIZE 236 //number of bytes of SRAM - -//Alarm mask bits -#define A1M1 7 -#define A1M2 7 -#define A1M3 7 -#define A1M4 7 -#define A2M2 7 -#define A2M3 7 -#define A2M4 7 - -//Control register bits -#define EOSC 7 -#define BBSQW 6 -#define CONV 5 -#define RS2 4 -#define RS1 3 -#define INTCN 2 -#define A2IE 1 -#define A1IE 0 +#include // https://github.com/PaulStoffregen/Time -//Status register bits -#define OSF 7 -#define BB32KHZ 6 -#define CRATE1 5 -#define CRATE0 4 -#define EN32KHZ 3 -#define BSY 2 -#define A2F 1 -#define A1F 0 - -//Square-wave output frequency (TS2, RS1 bits) -enum SQWAVE_FREQS_t {SQWAVE_1_HZ, SQWAVE_1024_HZ, SQWAVE_4096_HZ, SQWAVE_8192_HZ, SQWAVE_NONE}; - -//Alarm masks +// Alarm masks enum ALARM_TYPES_t { ALM1_EVERY_SECOND = 0x0F, ALM1_MATCH_SECONDS = 0x0E, - ALM1_MATCH_MINUTES = 0x0C, //match minutes *and* seconds - ALM1_MATCH_HOURS = 0x08, //match hours *and* minutes, seconds - ALM1_MATCH_DATE = 0x00, //match date *and* hours, minutes, seconds - ALM1_MATCH_DAY = 0x10, //match day *and* hours, minutes, seconds + ALM1_MATCH_MINUTES = 0x0C, // match minutes *and* seconds + ALM1_MATCH_HOURS = 0x08, // match hours *and* minutes, seconds + ALM1_MATCH_DATE = 0x00, // match date *and* hours, minutes, seconds + ALM1_MATCH_DAY = 0x10, // match day *and* hours, minutes, seconds ALM2_EVERY_MINUTE = 0x8E, - ALM2_MATCH_MINUTES = 0x8C, //match minutes - ALM2_MATCH_HOURS = 0x88, //match hours *and* minutes - ALM2_MATCH_DATE = 0x80, //match date *and* hours, minutes - ALM2_MATCH_DAY = 0x90, //match day *and* hours, minutes + ALM2_MATCH_MINUTES = 0x8C, // match minutes + ALM2_MATCH_HOURS = 0x88, // match hours *and* minutes + ALM2_MATCH_DATE = 0x80, // match date *and* hours, minutes + ALM2_MATCH_DAY = 0x90, // match day *and* hours, minutes }; -#define ALARM_1 1 //constants for calling functions -#define ALARM_2 2 +// Square-wave output frequency (TS2, RS1 bits) +enum SQWAVE_FREQS_t { + SQWAVE_1_HZ, + SQWAVE_1024_HZ, + SQWAVE_4096_HZ, + SQWAVE_8192_HZ, + SQWAVE_NONE +}; -//Other -#define DS1307_CH 7 //for DS1307 compatibility, Clock Halt bit in Seconds register -#define HR1224 6 //Hours register 12 or 24 hour mode (24 hour mode==0) -#define CENTURY 7 //Century bit in Month register -#define DYDT 6 //Day/Date flag bit in alarm Day/Date registers +#define ALARM_1 1 // constants for alarm functions +#define ALARM_2 2 class DS3232RTC { public: DS3232RTC(bool initI2C = true); void begin(); - static time_t get(); //must be static to work with setSyncProvider() in the Time library + static time_t get(); // static needed to work with setSyncProvider() in the Time library byte set(time_t t); static byte read(tmElements_t &tm); byte write(tmElements_t &tm); @@ -172,4 +85,8 @@ extern DS3232RTC RTC; #define _BV(bit) (1 << (bit)) #endif +#ifndef BUFFER_LENGTH // a horrible and limiting kludge for samd (arduino zero) +#define BUFFER_LENGTH 32 +#endif + #endif