Permalink
Browse files

Incorporated DS3231 (Chronodot), DS3234 functions from Coobro

  • Loading branch information...
MrAlvin committed Feb 27, 2012
1 parent 8b0c6e0 commit 6969a8affcd920bda9099863ec42b97715ee9187
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
134 RTClib.h
@@ -1,6 +1,17 @@
// Code by JeeLabs http://news.jeelabs.org/code/
// Released to the public domain! Enjoy!
+// Additions by MrAlvin.
+// Merged text from: github/coobro/RTClib
+
+// Alarm code for DS3231 (Chronodot) heavily used/modified from Eric Ayars DS3231 library
+// His code is located at: http://hacks.ayars.org/2011/04/ds3231-real-time-clock.html
+
+#ifndef __RTCLIB_H__
+#define __RTCLIB_H__
+
+
+
// Simple general-purpose date/time class (no TZ / DST / leap second handling!)
class DateTime {
public:
@@ -20,11 +31,18 @@ class DateTime {
long secondstime() const;
// 32-bit times as seconds since 1/1/1970
uint32_t unixtime(void) const;
-
+ // as a string
+ char* toString(char* buf, int maxlen) const;
+ // add additional time
+ void operator+=(uint32_t);
+
protected:
uint8_t yOff, m, d, hh, mm, ss;
};
+
+
+////////////////////////////////////////////////////////////////////////////////
// RTC based on the DS1307 chip connected via I2C and the Wire library
class RTC_DS1307 {
public:
@@ -34,14 +52,128 @@ class RTC_DS1307 {
static DateTime now();
};
+
+////////////////////////////////////////////////////////////////////////////////
+// RTC based on the DS3231 chip connected via I2C
+class RTC_DS3231
+{
+public:
+ uint8_t begin(void);
+ void adjust(const DateTime& dt);
+ uint8_t isrunning(void);
+ DateTime now();
+
+ // Temperature function
+
+ float getTemperature();
+
+ void getA1Time(byte& A1Day, byte& A1Hour, byte& A1Minute, byte& A1Second, byte& AlarmBits, bool& A1Dy, bool& A1h12, bool& A1PM);
+/* Retrieves everything you could want to know about alarm
+ * one.
+ * A1Dy true makes the alarm go on A1Day = Day of Week,
+ * A1Dy false makes the alarm go on A1Day = Date of month.
+ *
+ * byte AlarmBits sets the behavior of the alarms:
+ * Dy A1M4 A1M3 A1M2 A1M1 Rate
+ * X 1 1 1 1 Once per second
+ * X 1 1 1 0 Alarm when seconds match
+ * X 1 1 0 0 Alarm when min, sec match
+ * X 1 0 0 0 Alarm when hour, min, sec match
+ * 0 0 0 0 0 Alarm when date, h, m, s match
+ * 1 0 0 0 0 Alarm when DoW, h, m, s match
+ *
+ * Dy A2M4 A2M3 A2M2 Rate
+ * X 1 1 1 Once per minute (at seconds = 00)
+ * X 1 1 0 Alarm when minutes match
+ * X 1 0 0 Alarm when hours and minutes match
+ * 0 0 0 0 Alarm when date, hour, min match
+ * 1 0 0 0 Alarm when DoW, hour, min match
+ */
+ void getA2Time(byte& A2Day, byte& A2Hour, byte& A2Minute, byte& AlarmBits, bool& A2Dy, bool& A2h12, bool& A2PM);
+ // Same as getA1Time();, but A2 only goes on seconds == 00.
+ void setA1Time(byte A1Day, byte A1Hour, byte A1Minute, byte A1Second, byte AlarmBits, bool A1Dy, bool A1h12, bool A1PM);
+ // Set the details for Alarm 1
+ void setAlarm1Simple(byte hour, byte minute);
+ // A simple hour/minute alarm.
+ void setA2Time(byte A2Day, byte A2Hour, byte A2Minute, byte AlarmBits, bool A2Dy, bool A2h12, bool A2PM);
+ // Set the details for Alarm 2
+ void setAlarm2Simple(byte hour, byte minute);
+ // A simple hour/minute alarm.
+ void turnOnAlarm(byte Alarm);
+ // Enables alarm 1 or 2 and the external interrupt pin.
+ // If Alarm != 1, it assumes Alarm == 2.
+ void turnOffAlarm(byte Alarm);
+ // Disables alarm 1 or 2 (default is 2 if Alarm != 1);
+ // and leaves the interrupt pin alone.
+ bool checkAlarmEnabled(byte Alarm);
+ // Returns T/F to indicate whether the requested alarm is
+ // enabled. Defaults to 2 if Alarm != 1.
+ bool checkIfAlarm(byte Alarm);
+ // Checks whether the indicated alarm (1 or 2, 2 default);
+ // has been activated.
+
+ // Oscillator functions
+
+ void enableOscillator(bool TF, bool battery, byte frequency);
+ // turns oscillator on or off. True is on, false is off.
+ // if battery is true, turns on even for battery-only operation,
+ // otherwise turns off if Vcc is off.
+ // frequency must be 0, 1, 2, or 3.
+ // 0 = 1 Hz
+ // 1 = 1.024 kHz
+ // 2 = 4.096 kHz
+ // 3 = 8.192 kHz (Default if frequency byte is out of range);
+ void enable32kHz(bool TF);
+ // Turns the 32kHz output pin on (true); or off (false).
+ bool oscillatorCheck();;
+ // Checks the status of the OSF (Oscillator Stop Flag);.
+ // If this returns false, then the clock is probably not
+ // giving you the correct time.
+ // The OSF is cleared by function setSecond();.
+
+protected:
+ void cs(int _value);
+
+private:
+ int cs_pin;
+ byte readControlByte(bool which);
+ // Read selected control byte: (0); reads 0x0e, (1) reads 0x0f
+ void writeControlByte(byte control, bool which);
+ // Write the selected control byte.
+ // which == false -> 0x0e, true->0x0f.
+};
+
+////////////////////////////////////////////////////////////////////////////////
+// RTC based on the DS3234 chip connected via SPI and the SPI library
+class RTC_DS3234
+{
+public:
+ RTC_DS3234(int _cs_pin): cs_pin(_cs_pin) {}
+ uint8_t begin(void);
+ void adjust(const DateTime& dt);
+ uint8_t isrunning(void);
+ DateTime now();
+
+protected:
+ void cs(int _value);
+
+private:
+ int cs_pin;
+};
+
+
+////////////////////////////////////////////////////////////////////////////////
// RTC using the internal millis() clock, has to be initialized before use
// NOTE: this clock won't be correct once the millis() timer rolls over (>49d?)
class RTC_Millis {
public:
static void begin(const DateTime& dt) { adjust(dt); }
static void adjust(const DateTime& dt);
static DateTime now();
+ RTC_Millis(void) { adjust(DateTime(2000,1,1,0,0,0)); }
protected:
static long offset;
};
+
+#endif // __RTCLIB_H__
@@ -1,6 +1,7 @@
// Simple date conversions and calculations
#include <Wire.h>
+#include <SPI.h> // not used here, but needed to prevent a RTClib compile error
#include "RTClib.h"
void showDate(const char* txt, const DateTime& dt) {
@@ -1,6 +1,7 @@
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
+#include <SPI.h> // not used here, but needed to prevent a RTClib compile error
#include "RTClib.h"
RTC_DS1307 RTC;
@@ -58,4 +59,4 @@ void loop () {
Serial.println();
delay(3000);
-}
+}
View
@@ -0,0 +1,49 @@
+// Date, Time and Alarm functions using a DS3231 RTC connected via I2C and Wire lib
+
+#include <Wire.h>
+#include <SPI.h> // not used here, but needed to prevent a RTClib compile error
+#include <RTClib.h>
+
+RTC_DS3231 RTC;
+
+void setup () {
+ Serial.begin(57600);
+ Wire.begin();
+ RTC.begin();
+
+ RTC.adjust(DateTime(__DATE__, __TIME__));
+ if (! RTC.isrunning()) {
+ Serial.println("RTC is NOT running!");
+ // following line sets the RTC to the date & time this sketch was compiled
+ RTC.adjust(DateTime(__DATE__, __TIME__));
+ }
+ DateTime now = RTC.now();
+ RTC.setAlarm1Simple(21, 58);
+ RTC.turnOnAlarm(1);
+ if (RTC.checkAlarmEnabled(1)) {
+ Serial.println("Alarm Enabled");
+ }
+}
+
+void loop () {
+ DateTime now = RTC.now();
+
+ Serial.print(now.year(), DEC);
+ Serial.print('/');
+ Serial.print(now.month(), DEC);
+ Serial.print('/');
+ Serial.print(now.day(), DEC);
+ Serial.print(' ');
+ Serial.print(now.hour(), DEC);
+ Serial.print(':');
+ Serial.print(now.minute(), DEC);
+ Serial.print(':');
+ Serial.print(now.second(), DEC);
+ Serial.println();
+
+ if (RTC.checkIfAlarm(1)) {
+ Serial.println("Alarm Triggered");
+ }
+ Serial.println();
+ delay(3000);
+}
@@ -0,0 +1,63 @@
+// Date, Time and Alarm functions using a DS3231 RTC connected via I2C and Wire lib
+
+#include <Wire.h>
+#include <SPI.h> // not used here, but needed to prevent a RTClib compile error
+#include <avr/sleep.h>
+#include <RTClib.h>
+
+
+RTC_DS3231 RTC;
+int INTERRUPT_PIN = 2;
+volatile int state = LOW;
+
+void setup () {
+ pinMode(INTERRUPT_PIN, INPUT);
+ //pull up the interrupt pin
+ digitalWrite(INTERRUPT_PIN, HIGH);
+
+ Serial.begin(57600);
+ Wire.begin();
+ RTC.begin();
+
+ RTC.adjust(DateTime(__DATE__, __TIME__));
+ DateTime now = RTC.now();
+ RTC.setAlarm1Simple(22, 11);
+ RTC.setAlarm2Simple(11, 10);
+ RTC.turnOnAlarm(1);
+ RTC.turnOnAlarm(2);
+ if (RTC.checkAlarmEnabled(1) && RTC.checkAlarmEnabled(2)) {
+ Serial.println("Alarms Enabled");
+ }
+ attachInterrupt(0, logData, LOW);
+}
+
+void loop () {
+ DateTime now = RTC.now();
+ if (RTC.checkIfAlarm(1) || RTC.checkIfAlarm(2)) {
+ Serial.println("Alarm Triggered");
+ }
+
+ Serial.print(now.hour(), DEC);
+ Serial.print(':');
+ Serial.print(now.minute(), DEC);
+ Serial.print(':');
+ Serial.println(now.second(), DEC);
+ Serial.println("Going to Sleep");
+ delay(600);
+ sleepNow();
+ Serial.println("AWAKE");
+}
+
+void sleepNow() {
+ set_sleep_mode(SLEEP_MODE_PWR_DOWN);
+ sleep_enable();
+ attachInterrupt(0,logData, LOW);
+ sleep_mode();
+ //HERE AFTER WAKING UP
+ sleep_disable();
+ detachInterrupt(0);
+}
+
+void logData() {
+ //do something quick, flip a flag, and handle in loop();
+}
View
@@ -0,0 +1,57 @@
+// Date and time functions using a DS3234 RTC connected via SPI
+
+#include <SPI.h>
+#include <Wire.h> // not used here, but needed to prevent a RTClib compile error
+#include <RTClib.h>
+
+
+// Avoid spurious warnings
+#undef PROGMEM
+#define PROGMEM __attribute__(( section(".progmem.data") ))
+#undef PSTR
+#define PSTR(s) (__extension__({static prog_char __c[] PROGMEM = (s); &__c[0];}))
+
+// Create an RTC instance, using the chip select pin it's connected to
+RTC_DS3234 RTC(8);
+
+void setup () {
+ Serial.begin(57600);
+ Serial.println("RTClib/examples/ds3234/");
+ SPI.begin();
+ RTC.begin();
+
+ if (! RTC.isrunning()) {
+ Serial.println("RTC is NOT running!");
+ Serial.print("Setting time to... ");
+ Serial.print(__DATE__);
+ Serial.print(' ');
+ Serial.println(__TIME__);
+ // following line sets the RTC to the date & time this sketch was compiled
+ RTC.adjust(DateTime(__DATE__, __TIME__));
+ }
+}
+
+void loop () {
+ const int len = 32;
+ static char buf[len];
+
+ DateTime now = RTC.now();
+
+ Serial.println(now.toString(buf,len));
+
+ Serial.print(" since midnight 1/1/1970 = ");
+ Serial.print(now.unixtime());
+ Serial.print("s = ");
+ Serial.print(now.unixtime() / 86400L);
+ Serial.println("d");
+
+ // calculate a date which is 7 days and 30 seconds into the future
+ DateTime future (now.unixtime() + 7 * 86400L + 30 );
+
+ Serial.print(" now + 7d + 30s: ");
+ Serial.println(future.toString(buf,len));
+
+ Serial.println();
+ delay(3000);
+}
+
@@ -2,11 +2,10 @@
#include <Wire.h>
+#include <SPI.h> // not used here, but needed to prevent a RTClib compile error
#include "RTClib.h"
-RTC_DS1307 RTC; // Uncommnt this line if you are using the DS1307 chip or only use the basic functions of DS1337, DS1340, Chronodot (DS3132)
-// RTC_DS1340 RTC; // Uncomment this line if you are using the DS1340 chip
-// Chronodot RTC; // Uncomment this line if you are using the Chronodot (DS3132)
+RTC_DS1307 RTC; // Uncommnt this line if you are using the DS1307 chip or only use the basic functions of DS1337, DS1340, Chronodot (DS3231)
void setup () {
@@ -1,9 +1,8 @@
// Set date and time using a DS1307 RTC connected via I2C
-//
-// Connect SCL to Uno-Analog 0
-// Connect SDA to Uno-Analog 1
+
#include <Wire.h>
+#include <SPI.h> // not used here, but needed to prevent a RTClib compile error
#include "RTClib.h"
RTC_DS1307 RTC; // Setup an instance of DS1307 naming it RTC
@@ -1,6 +1,7 @@
// Date and time functions using just software, based on millis() & timer
#include <Wire.h>
+#include <SPI.h> // not used here, but needed to prevent a RTClib compile error
#include "RTClib.h"
RTC_Millis RTC;
Oops, something went wrong.

0 comments on commit 6969a8a

Please sign in to comment.