初始化提交
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
|
||||
// CONNECTIONS:
|
||||
// DS1302 CLK/SCLK --> 5
|
||||
// DS1302 DAT/IO --> 4
|
||||
// DS1302 RST/CE --> 2
|
||||
// DS1302 VCC --> 3.3v - 5v
|
||||
// DS1302 GND --> GND
|
||||
|
||||
#include <ThreeWire.h>
|
||||
#include <RtcDS1302.h>
|
||||
|
||||
ThreeWire myWire(4,5,2); // IO, SCLK, CE
|
||||
RtcDS1302<ThreeWire> Rtc(myWire);
|
||||
|
||||
#define countof(a) (sizeof(a) / sizeof(a[0]))
|
||||
|
||||
const char data[] = "what time is it";
|
||||
|
||||
void setup ()
|
||||
{
|
||||
Serial.begin(57600);
|
||||
|
||||
Serial.print("compiled: ");
|
||||
Serial.print(__DATE__);
|
||||
Serial.println(__TIME__);
|
||||
|
||||
Rtc.Begin();
|
||||
|
||||
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
|
||||
printDateTime(compiled);
|
||||
Serial.println();
|
||||
|
||||
if (!Rtc.IsDateTimeValid())
|
||||
{
|
||||
Serial.println("RTC lost confidence in the DateTime!");
|
||||
Rtc.SetDateTime(compiled);
|
||||
}
|
||||
|
||||
if (Rtc.GetIsWriteProtected())
|
||||
{
|
||||
Serial.println("RTC was write protected, enabling writing now");
|
||||
Rtc.SetIsWriteProtected(false);
|
||||
}
|
||||
|
||||
if (!Rtc.GetIsRunning())
|
||||
{
|
||||
Serial.println("RTC was not actively running, starting now");
|
||||
Rtc.SetIsRunning(true);
|
||||
}
|
||||
|
||||
RtcDateTime now = Rtc.GetDateTime();
|
||||
if (now < compiled)
|
||||
{
|
||||
Serial.println("RTC is older than compile time! (Updating DateTime)");
|
||||
Rtc.SetDateTime(compiled);
|
||||
}
|
||||
|
||||
|
||||
/* comment out on a second run to see that the info is stored long term */
|
||||
// Store something in memory on the RTC
|
||||
uint8_t count = sizeof(data);
|
||||
uint8_t written = Rtc.SetMemory((const uint8_t*)data, count); // this includes a null terminator for the string
|
||||
if (written != count)
|
||||
{
|
||||
Serial.print("something didn't match, count = ");
|
||||
Serial.print(count, DEC);
|
||||
Serial.print(", written = ");
|
||||
Serial.print(written, DEC);
|
||||
Serial.println();
|
||||
}
|
||||
/* end of comment out section */
|
||||
}
|
||||
|
||||
void loop ()
|
||||
{
|
||||
RtcDateTime now = Rtc.GetDateTime();
|
||||
|
||||
printDateTime(now);
|
||||
Serial.println(" +");
|
||||
|
||||
if (!now.IsValid())
|
||||
{
|
||||
// Common Causes:
|
||||
// 1) the battery on the device is low or even missing and the power line was disconnected
|
||||
Serial.println("RTC lost confidence in the DateTime!");
|
||||
}
|
||||
|
||||
delay(5000);
|
||||
|
||||
// read data
|
||||
uint8_t buff[20];
|
||||
const uint8_t count = sizeof(buff);
|
||||
// get our data
|
||||
uint8_t gotten = Rtc.GetMemory(buff, count);
|
||||
|
||||
if (gotten != count)
|
||||
{
|
||||
Serial.print("something didn't match, count = ");
|
||||
Serial.print(count, DEC);
|
||||
Serial.print(", gotten = ");
|
||||
Serial.print(gotten, DEC);
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
Serial.print("data read (");
|
||||
Serial.print(gotten);
|
||||
Serial.print(") = \"");
|
||||
// print the string, but terminate if we get a null
|
||||
for (uint8_t ch = 0; ch < gotten && buff[ch]; ch++)
|
||||
{
|
||||
Serial.print((char)buff[ch]);
|
||||
}
|
||||
Serial.println("\"");
|
||||
|
||||
delay(5000);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void printDateTime(const RtcDateTime& dt)
|
||||
{
|
||||
char datestring[20];
|
||||
|
||||
snprintf_P(datestring,
|
||||
countof(datestring),
|
||||
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
|
||||
dt.Month(),
|
||||
dt.Day(),
|
||||
dt.Year(),
|
||||
dt.Hour(),
|
||||
dt.Minute(),
|
||||
dt.Second() );
|
||||
Serial.print(datestring);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
|
||||
// CONNECTIONS:
|
||||
// DS1302 CLK/SCLK --> 5
|
||||
// DS1302 DAT/IO --> 4
|
||||
// DS1302 RST/CE --> 2
|
||||
// DS1302 VCC --> 3.3v - 5v
|
||||
// DS1302 GND --> GND
|
||||
|
||||
#include <ThreeWire.h>
|
||||
#include <RtcDS1302.h>
|
||||
|
||||
ThreeWire myWire(4,5,2); // IO, SCLK, CE
|
||||
RtcDS1302<ThreeWire> Rtc(myWire);
|
||||
|
||||
void setup ()
|
||||
{
|
||||
Serial.begin(57600);
|
||||
|
||||
Serial.print("compiled: ");
|
||||
Serial.print(__DATE__);
|
||||
Serial.println(__TIME__);
|
||||
|
||||
Rtc.Begin();
|
||||
|
||||
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
|
||||
printDateTime(compiled);
|
||||
Serial.println();
|
||||
|
||||
if (!Rtc.IsDateTimeValid())
|
||||
{
|
||||
// Common Causes:
|
||||
// 1) first time you ran and the device wasn't running yet
|
||||
// 2) the battery on the device is low or even missing
|
||||
|
||||
Serial.println("RTC lost confidence in the DateTime!");
|
||||
Rtc.SetDateTime(compiled);
|
||||
}
|
||||
|
||||
if (Rtc.GetIsWriteProtected())
|
||||
{
|
||||
Serial.println("RTC was write protected, enabling writing now");
|
||||
Rtc.SetIsWriteProtected(false);
|
||||
}
|
||||
|
||||
if (!Rtc.GetIsRunning())
|
||||
{
|
||||
Serial.println("RTC was not actively running, starting now");
|
||||
Rtc.SetIsRunning(true);
|
||||
}
|
||||
|
||||
RtcDateTime now = Rtc.GetDateTime();
|
||||
if (now < compiled)
|
||||
{
|
||||
Serial.println("RTC is older than compile time! (Updating DateTime)");
|
||||
Rtc.SetDateTime(compiled);
|
||||
}
|
||||
else if (now > compiled)
|
||||
{
|
||||
Serial.println("RTC is newer than compile time. (this is expected)");
|
||||
}
|
||||
else if (now == compiled)
|
||||
{
|
||||
Serial.println("RTC is the same as compile time! (not expected but all is fine)");
|
||||
}
|
||||
}
|
||||
|
||||
void loop ()
|
||||
{
|
||||
RtcDateTime now = Rtc.GetDateTime();
|
||||
|
||||
printDateTime(now);
|
||||
Serial.println();
|
||||
|
||||
if (!now.IsValid())
|
||||
{
|
||||
// Common Causes:
|
||||
// 1) the battery on the device is low or even missing and the power line was disconnected
|
||||
Serial.println("RTC lost confidence in the DateTime!");
|
||||
}
|
||||
|
||||
delay(10000); // ten seconds
|
||||
}
|
||||
|
||||
#define countof(a) (sizeof(a) / sizeof(a[0]))
|
||||
|
||||
void printDateTime(const RtcDateTime& dt)
|
||||
{
|
||||
char datestring[20];
|
||||
|
||||
snprintf_P(datestring,
|
||||
countof(datestring),
|
||||
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
|
||||
dt.Month(),
|
||||
dt.Day(),
|
||||
dt.Year(),
|
||||
dt.Hour(),
|
||||
dt.Minute(),
|
||||
dt.Second() );
|
||||
Serial.print(datestring);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,172 @@
|
||||
|
||||
// CONNECTIONS:
|
||||
// DS1307 SDA --> SDA
|
||||
// DS1307 SCL --> SCL
|
||||
// DS1307 VCC --> 5v
|
||||
// DS1307 GND --> GND
|
||||
|
||||
#define countof(a) (sizeof(a) / sizeof(a[0]))
|
||||
|
||||
/* for software wire use below
|
||||
#include <SoftwareWire.h> // must be included here so that Arduino library object file references work
|
||||
#include <RtcDS1307.h>
|
||||
|
||||
SoftwareWire myWire(SDA, SCL);
|
||||
RtcDS1307<SoftwareWire> Rtc(myWire);
|
||||
for software wire use above */
|
||||
|
||||
/* for normal hardware wire use below */
|
||||
#include <Wire.h> // must be included here so that Arduino library object file references work
|
||||
#include <RtcDS1307.h>
|
||||
RtcDS1307<TwoWire> Rtc(Wire);
|
||||
/* for normal hardware wire use above */
|
||||
|
||||
|
||||
const char data[] = "what time is it";
|
||||
|
||||
void setup ()
|
||||
{
|
||||
Serial.begin(57600);
|
||||
|
||||
Serial.print("compiled: ");
|
||||
Serial.print(__DATE__);
|
||||
Serial.println(__TIME__);
|
||||
|
||||
//--------RTC SETUP ------------
|
||||
// if you are using ESP-01 then uncomment the line below to reset the pins to
|
||||
// the available pins for SDA, SCL
|
||||
// Wire.begin(0, 2); // due to limited pins, use pin 0 and 2 for SDA, SCL
|
||||
|
||||
Rtc.Begin();
|
||||
|
||||
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
|
||||
printDateTime(compiled);
|
||||
Serial.println();
|
||||
|
||||
if (!Rtc.IsDateTimeValid())
|
||||
{
|
||||
if (Rtc.LastError() != 0)
|
||||
{
|
||||
// we have a communications error
|
||||
// see https://www.arduino.cc/en/Reference/WireEndTransmission for
|
||||
// what the number means
|
||||
Serial.print("RTC communications error = ");
|
||||
Serial.println(Rtc.LastError());
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("RTC lost confidence in the DateTime!");
|
||||
Rtc.SetDateTime(compiled);
|
||||
}
|
||||
}
|
||||
|
||||
if (!Rtc.GetIsRunning())
|
||||
{
|
||||
Serial.println("RTC was not actively running, starting now");
|
||||
Rtc.SetIsRunning(true);
|
||||
}
|
||||
|
||||
RtcDateTime now = Rtc.GetDateTime();
|
||||
if (now < compiled)
|
||||
{
|
||||
Serial.println("RTC is older than compile time! (Updating DateTime)");
|
||||
Rtc.SetDateTime(compiled);
|
||||
}
|
||||
|
||||
// never assume the Rtc was last configured by you, so
|
||||
// just clear them to your needed state
|
||||
Rtc.SetSquareWavePin(DS1307SquareWaveOut_Low);
|
||||
|
||||
/* comment out on a second run to see that the info is stored long term */
|
||||
// Store something in memory on the RTC
|
||||
Rtc.SetMemory(0, 13);
|
||||
uint8_t written = Rtc.SetMemory(13, (const uint8_t*)data, sizeof(data) - 1); // remove the null terminator strings add
|
||||
Rtc.SetMemory(1, written);
|
||||
/* end of comment out section */
|
||||
}
|
||||
|
||||
void loop ()
|
||||
{
|
||||
if (!Rtc.IsDateTimeValid())
|
||||
{
|
||||
if (Rtc.LastError() != 0)
|
||||
{
|
||||
// we have a communications error
|
||||
// see https://www.arduino.cc/en/Reference/WireEndTransmission for
|
||||
// what the number means
|
||||
Serial.print("RTC communications error = ");
|
||||
Serial.println(Rtc.LastError());
|
||||
}
|
||||
else
|
||||
{
|
||||
// Common Causes:
|
||||
// 1) the battery on the device is low or even missing and the power line was disconnected
|
||||
Serial.println("RTC lost confidence in the DateTime!");
|
||||
}
|
||||
}
|
||||
|
||||
RtcDateTime now = Rtc.GetDateTime();
|
||||
|
||||
printDateTime(now);
|
||||
Serial.println();
|
||||
|
||||
delay(5000);
|
||||
|
||||
// read data
|
||||
|
||||
// get the offset we stored our data from address zero
|
||||
uint8_t address = Rtc.GetMemory(0);
|
||||
if (address != 13)
|
||||
{
|
||||
Serial.println("address didn't match");
|
||||
}
|
||||
else
|
||||
{
|
||||
// get the size of the data from address 1
|
||||
uint8_t count = Rtc.GetMemory(1);
|
||||
uint8_t buff[20];
|
||||
|
||||
// get our data from the address with the given size
|
||||
uint8_t gotten = Rtc.GetMemory(address, buff, count);
|
||||
|
||||
if (gotten != count ||
|
||||
count != sizeof(data) - 1) // remove the extra null terminator strings add
|
||||
{
|
||||
Serial.print("something didn't match, count = ");
|
||||
Serial.print(count, DEC);
|
||||
Serial.print(", gotten = ");
|
||||
Serial.print(gotten, DEC);
|
||||
Serial.println();
|
||||
}
|
||||
Serial.print("data read (");
|
||||
Serial.print(gotten);
|
||||
Serial.print(") = \"");
|
||||
for (uint8_t ch = 0; ch < gotten; ch++)
|
||||
{
|
||||
Serial.print((char)buff[ch]);
|
||||
}
|
||||
Serial.println("\"");
|
||||
}
|
||||
|
||||
|
||||
delay(5000);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void printDateTime(const RtcDateTime& dt)
|
||||
{
|
||||
char datestring[20];
|
||||
|
||||
snprintf_P(datestring,
|
||||
countof(datestring),
|
||||
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
|
||||
dt.Month(),
|
||||
dt.Day(),
|
||||
dt.Year(),
|
||||
dt.Hour(),
|
||||
dt.Minute(),
|
||||
dt.Second() );
|
||||
Serial.print(datestring);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
|
||||
// CONNECTIONS:
|
||||
// DS1307 SDA --> SDA
|
||||
// DS1307 SCL --> SCL
|
||||
// DS1307 VCC --> 5v
|
||||
// DS1307 GND --> GND
|
||||
|
||||
/* for software wire use below
|
||||
#include <SoftwareWire.h> // must be included here so that Arduino library object file references work
|
||||
#include <RtcDS1307.h>
|
||||
|
||||
SoftwareWire myWire(SDA, SCL);
|
||||
RtcDS1307<SoftwareWire> Rtc(myWire);
|
||||
for software wire use above */
|
||||
|
||||
/* for normal hardware wire use below */
|
||||
#include <Wire.h> // must be included here so that Arduino library object file references work
|
||||
#include <RtcDS1307.h>
|
||||
RtcDS1307<TwoWire> Rtc(Wire);
|
||||
/* for normal hardware wire use above */
|
||||
|
||||
void setup ()
|
||||
{
|
||||
Serial.begin(57600);
|
||||
|
||||
Serial.print("compiled: ");
|
||||
Serial.print(__DATE__);
|
||||
Serial.println(__TIME__);
|
||||
|
||||
//--------RTC SETUP ------------
|
||||
// if you are using ESP-01 then uncomment the line below to reset the pins to
|
||||
// the available pins for SDA, SCL
|
||||
// Wire.begin(0, 2); // due to limited pins, use pin 0 and 2 for SDA, SCL
|
||||
|
||||
Rtc.Begin();
|
||||
|
||||
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
|
||||
printDateTime(compiled);
|
||||
Serial.println();
|
||||
|
||||
if (!Rtc.IsDateTimeValid())
|
||||
{
|
||||
if (Rtc.LastError() != 0)
|
||||
{
|
||||
// we have a communications error
|
||||
// see https://www.arduino.cc/en/Reference/WireEndTransmission for
|
||||
// what the number means
|
||||
Serial.print("RTC communications error = ");
|
||||
Serial.println(Rtc.LastError());
|
||||
}
|
||||
else
|
||||
{
|
||||
// Common Causes:
|
||||
// 1) first time you ran and the device wasn't running yet
|
||||
// 2) the battery on the device is low or even missing
|
||||
|
||||
Serial.println("RTC lost confidence in the DateTime!");
|
||||
// following line sets the RTC to the date & time this sketch was compiled
|
||||
// it will also reset the valid flag internally unless the Rtc device is
|
||||
// having an issue
|
||||
|
||||
Rtc.SetDateTime(compiled);
|
||||
}
|
||||
}
|
||||
|
||||
if (!Rtc.GetIsRunning())
|
||||
{
|
||||
Serial.println("RTC was not actively running, starting now");
|
||||
Rtc.SetIsRunning(true);
|
||||
}
|
||||
|
||||
RtcDateTime now = Rtc.GetDateTime();
|
||||
if (now < compiled)
|
||||
{
|
||||
Serial.println("RTC is older than compile time! (Updating DateTime)");
|
||||
Rtc.SetDateTime(compiled);
|
||||
}
|
||||
else if (now > compiled)
|
||||
{
|
||||
Serial.println("RTC is newer than compile time. (this is expected)");
|
||||
}
|
||||
else if (now == compiled)
|
||||
{
|
||||
Serial.println("RTC is the same as compile time! (not expected but all is fine)");
|
||||
}
|
||||
|
||||
// never assume the Rtc was last configured by you, so
|
||||
// just clear them to your needed state
|
||||
Rtc.SetSquareWavePin(DS1307SquareWaveOut_Low);
|
||||
}
|
||||
|
||||
void loop ()
|
||||
{
|
||||
if (!Rtc.IsDateTimeValid())
|
||||
{
|
||||
if (Rtc.LastError() != 0)
|
||||
{
|
||||
// we have a communications error
|
||||
// see https://www.arduino.cc/en/Reference/WireEndTransmission for
|
||||
// what the number means
|
||||
Serial.print("RTC communications error = ");
|
||||
Serial.println(Rtc.LastError());
|
||||
}
|
||||
else
|
||||
{
|
||||
// Common Causes:
|
||||
// 1) the battery on the device is low or even missing and the power line was disconnected
|
||||
Serial.println("RTC lost confidence in the DateTime!");
|
||||
}
|
||||
}
|
||||
|
||||
RtcDateTime now = Rtc.GetDateTime();
|
||||
|
||||
printDateTime(now);
|
||||
Serial.println();
|
||||
|
||||
delay(10000); // ten seconds
|
||||
}
|
||||
|
||||
#define countof(a) (sizeof(a) / sizeof(a[0]))
|
||||
|
||||
void printDateTime(const RtcDateTime& dt)
|
||||
{
|
||||
char datestring[20];
|
||||
|
||||
snprintf_P(datestring,
|
||||
countof(datestring),
|
||||
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
|
||||
dt.Month(),
|
||||
dt.Day(),
|
||||
dt.Year(),
|
||||
dt.Hour(),
|
||||
dt.Minute(),
|
||||
dt.Second() );
|
||||
Serial.print(datestring);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,207 @@
|
||||
|
||||
// CONNECTIONS:
|
||||
// DS3231 SDA --> SDA
|
||||
// DS3231 SCL --> SCL
|
||||
// DS3231 VCC --> 3.3v or 5v
|
||||
// DS3231 GND --> GND
|
||||
// SQW ---> (Pin19) Don't forget to pullup (4.7k to 10k to VCC)
|
||||
|
||||
/* for software wire use below
|
||||
#include <SoftwareWire.h> // must be included here so that Arduino library object file references work
|
||||
#include <RtcDS3231.h>
|
||||
|
||||
SoftwareWire myWire(SDA, SCL);
|
||||
RtcDS3231<SoftwareWire> Rtc(myWire);
|
||||
for software wire use above */
|
||||
|
||||
/* for normal hardware wire use below */
|
||||
#include <Wire.h> // must be included here so that Arduino library object file references work
|
||||
#include <RtcDS3231.h>
|
||||
RtcDS3231<TwoWire> Rtc(Wire);
|
||||
/* for normal hardware wire use above */
|
||||
|
||||
|
||||
// Interrupt Pin Lookup Table
|
||||
// (copied from Arduino Docs)
|
||||
//
|
||||
// CAUTION: The interrupts are Arduino numbers NOT Atmel numbers
|
||||
// and may not match (example, Mega2560 int.4 is actually Atmel Int2)
|
||||
// this is only an issue if you plan to use the lower level interupt features
|
||||
//
|
||||
// Board int.0 int.1 int.2 int.3 int.4 int.5
|
||||
// ---------------------------------------------------------------
|
||||
// Uno, Ethernet 2 3
|
||||
// Mega2560 2 3 21 20 [19] 18
|
||||
// Leonardo 3 2 0 1 7
|
||||
|
||||
#define RtcSquareWavePin 19 // Mega2560
|
||||
#define RtcSquareWaveInterrupt 4 // Mega2560
|
||||
|
||||
// marked volatile so interrupt can safely modify them and
|
||||
// other code can safely read and modify them
|
||||
volatile uint16_t interuptCount = 0;
|
||||
volatile bool interuptFlag = false;
|
||||
|
||||
void ISR_ATTR InteruptServiceRoutine()
|
||||
{
|
||||
// since this interupted any other running code,
|
||||
// don't do anything that takes long and especially avoid
|
||||
// any communications calls within this routine
|
||||
interuptCount++;
|
||||
interuptFlag = true;
|
||||
}
|
||||
|
||||
void setup ()
|
||||
{
|
||||
Serial.begin(57600);
|
||||
|
||||
// set the interupt pin to input mode
|
||||
pinMode(RtcSquareWavePin, INPUT);
|
||||
|
||||
//--------RTC SETUP ------------
|
||||
// if you are using ESP-01 then uncomment the line below to reset the pins to
|
||||
// the available pins for SDA, SCL
|
||||
// Wire.begin(0, 2); // due to limited pins, use pin 0 and 2 for SDA, SCL
|
||||
|
||||
Rtc.Begin();
|
||||
|
||||
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
|
||||
|
||||
if (!Rtc.IsDateTimeValid())
|
||||
{
|
||||
if (Rtc.LastError() != 0)
|
||||
{
|
||||
// we have a communications error
|
||||
// see https://www.arduino.cc/en/Reference/WireEndTransmission for
|
||||
// what the number means
|
||||
Serial.print("RTC communications error = ");
|
||||
Serial.println(Rtc.LastError());
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("RTC lost confidence in the DateTime!");
|
||||
Rtc.SetDateTime(compiled);
|
||||
}
|
||||
}
|
||||
|
||||
if (!Rtc.GetIsRunning())
|
||||
{
|
||||
Serial.println("RTC was not actively running, starting now");
|
||||
Rtc.SetIsRunning(true);
|
||||
}
|
||||
|
||||
RtcDateTime now = Rtc.GetDateTime();
|
||||
if (now < compiled)
|
||||
{
|
||||
Serial.println("RTC is older than compile time! (Updating DateTime)");
|
||||
Rtc.SetDateTime(compiled);
|
||||
}
|
||||
|
||||
Rtc.Enable32kHzPin(false);
|
||||
Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeAlarmBoth);
|
||||
|
||||
// Alarm 1 set to trigger every day when
|
||||
// the hours, minutes, and seconds match
|
||||
RtcDateTime alarmTime = now + 88; // into the future
|
||||
DS3231AlarmOne alarm1(
|
||||
alarmTime.Day(),
|
||||
alarmTime.Hour(),
|
||||
alarmTime.Minute(),
|
||||
alarmTime.Second(),
|
||||
DS3231AlarmOneControl_HoursMinutesSecondsMatch);
|
||||
Rtc.SetAlarmOne(alarm1);
|
||||
|
||||
// Alarm 2 set to trigger at the top of the minute
|
||||
DS3231AlarmTwo alarm2(
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
DS3231AlarmTwoControl_OncePerMinute);
|
||||
Rtc.SetAlarmTwo(alarm2);
|
||||
|
||||
// throw away any old alarm state before we ran
|
||||
Rtc.LatchAlarmsTriggeredFlags();
|
||||
|
||||
// setup external interupt
|
||||
attachInterrupt(RtcSquareWaveInterrupt, InteruptServiceRoutine, FALLING);
|
||||
}
|
||||
|
||||
void loop ()
|
||||
{
|
||||
if (!Rtc.IsDateTimeValid())
|
||||
{
|
||||
if (Rtc.LastError() != 0)
|
||||
{
|
||||
// we have a communications error
|
||||
// see https://www.arduino.cc/en/Reference/WireEndTransmission for
|
||||
// what the number means
|
||||
Serial.print("RTC communications error = ");
|
||||
Serial.println(Rtc.LastError());
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("RTC lost confidence in the DateTime!");
|
||||
}
|
||||
}
|
||||
|
||||
RtcDateTime now = Rtc.GetDateTime();
|
||||
|
||||
printDateTime(now);
|
||||
Serial.println();
|
||||
|
||||
// we only want to show time every 10 seconds
|
||||
// but we want to show responce to the interupt firing
|
||||
for (int timeCount = 0; timeCount < 20; timeCount++)
|
||||
{
|
||||
if (Alarmed())
|
||||
{
|
||||
Serial.print(">>Interupt Count: ");
|
||||
Serial.print(interuptCount);
|
||||
Serial.println("<<");
|
||||
}
|
||||
delay(500);
|
||||
}
|
||||
}
|
||||
|
||||
bool Alarmed()
|
||||
{
|
||||
bool wasAlarmed = false;
|
||||
if (interuptFlag) // check our flag that gets sets in the interupt
|
||||
{
|
||||
wasAlarmed = true;
|
||||
interuptFlag = false; // reset the flag
|
||||
|
||||
// this gives us which alarms triggered and
|
||||
// then allows for others to trigger again
|
||||
DS3231AlarmFlag flag = Rtc.LatchAlarmsTriggeredFlags();
|
||||
|
||||
if (flag & DS3231AlarmFlag_Alarm1)
|
||||
{
|
||||
Serial.println("alarm one triggered");
|
||||
}
|
||||
if (flag & DS3231AlarmFlag_Alarm2)
|
||||
{
|
||||
Serial.println("alarm two triggered");
|
||||
}
|
||||
}
|
||||
return wasAlarmed;
|
||||
}
|
||||
|
||||
#define countof(a) (sizeof(a) / sizeof(a[0]))
|
||||
|
||||
void printDateTime(const RtcDateTime& dt)
|
||||
{
|
||||
char datestring[20];
|
||||
|
||||
snprintf_P(datestring,
|
||||
countof(datestring),
|
||||
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
|
||||
dt.Month(),
|
||||
dt.Day(),
|
||||
dt.Year(),
|
||||
dt.Hour(),
|
||||
dt.Minute(),
|
||||
dt.Second() );
|
||||
Serial.print(datestring);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,197 @@
|
||||
|
||||
// CONNECTIONS:
|
||||
// DS1307 SDA --> SDA
|
||||
// DS1307 SCL --> SCL
|
||||
// DS1307 VCC --> 5v
|
||||
// DS1307 GND --> GND
|
||||
|
||||
#define countof(a) (sizeof(a) / sizeof(a[0]))
|
||||
|
||||
/* for software wire use below
|
||||
#include <SoftwareWire.h> // must be included here so that Arduino library object file references work
|
||||
#include <RtcDS3231.h>
|
||||
#include <EepromAT24C32.h>
|
||||
|
||||
SoftwareWire myWire(SDA, SCL);
|
||||
RtcDS1307<SoftwareWire> Rtc(myWire);
|
||||
/* for software wire use above */
|
||||
|
||||
/* for normal hardware wire use below */
|
||||
#include <Wire.h> // must be included here so that Arduino library object file references work
|
||||
#include <RtcDS3231.h>
|
||||
#include <EepromAT24C32.h>
|
||||
|
||||
RtcDS3231<TwoWire> Rtc(Wire);
|
||||
EepromAt24c32<TwoWire> RtcEeprom(Wire);
|
||||
|
||||
// if you have any of the address pins on the RTC soldered together
|
||||
// then you need to provide the state of those pins, normally they
|
||||
// are connected to vcc with a reading of 1, if soldered they are
|
||||
// grounded with a reading of 0. The bits are in the order A2 A1 A0
|
||||
// thus the following would have the A2 soldered together
|
||||
// EepromAt24c32<TwoWire> RtcEeprom(Wire, 0b011);
|
||||
|
||||
/* for normal hardware wire use above */
|
||||
|
||||
// nothing longer than 32 bytes
|
||||
// rtc eeprom memory is 32 byte pages
|
||||
// writing is limited to each page, so it will wrap at page
|
||||
// boundaries.
|
||||
// But reading is only limited by the buffer in Wire class which
|
||||
// by default is 32
|
||||
const char data[] = "What time is it in Greenwich?";
|
||||
const uint16_t stringAddr = 64; // stored on page boundary
|
||||
|
||||
void setup ()
|
||||
{
|
||||
Serial.begin(57600);
|
||||
|
||||
Serial.print("compiled: ");
|
||||
Serial.print(__DATE__);
|
||||
Serial.println(__TIME__);
|
||||
|
||||
//--------RTC SETUP ------------
|
||||
// if you are using ESP-01 then uncomment the line below to reset the pins to
|
||||
// the available pins for SDA, SCL
|
||||
// Wire.begin(0, 2); // due to limited pins, use pin 0 and 2 for SDA, SCL
|
||||
|
||||
Rtc.Begin();
|
||||
RtcEeprom.Begin();
|
||||
|
||||
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
|
||||
printDateTime(compiled);
|
||||
Serial.println();
|
||||
|
||||
if (!Rtc.IsDateTimeValid())
|
||||
{
|
||||
if (Rtc.LastError() != 0)
|
||||
{
|
||||
// we have a communications error
|
||||
// see https://www.arduino.cc/en/Reference/WireEndTransmission for
|
||||
// what the number means
|
||||
Serial.print("RTC communications error = ");
|
||||
Serial.println(Rtc.LastError());
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("RTC lost confidence in the DateTime!");
|
||||
Rtc.SetDateTime(compiled);
|
||||
}
|
||||
}
|
||||
|
||||
if (!Rtc.GetIsRunning())
|
||||
{
|
||||
Serial.println("RTC was not actively running, starting now");
|
||||
Rtc.SetIsRunning(true);
|
||||
}
|
||||
|
||||
RtcDateTime now = Rtc.GetDateTime();
|
||||
if (now < compiled)
|
||||
{
|
||||
Serial.println("RTC is older than compile time! (Updating DateTime)");
|
||||
Rtc.SetDateTime(compiled);
|
||||
}
|
||||
|
||||
// never assume the Rtc was last configured by you, so
|
||||
// just clear them to your needed state
|
||||
Rtc.Enable32kHzPin(false);
|
||||
Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeNone);
|
||||
|
||||
/* comment out on a second run to see that the info is stored long term */
|
||||
// Store something in memory on the Eeprom
|
||||
|
||||
// store starting address of string
|
||||
RtcEeprom.SetMemory(0, stringAddr);
|
||||
// store the string, nothing longer than 32 bytes due to paging
|
||||
uint8_t written = RtcEeprom.SetMemory(stringAddr, (const uint8_t*)data, sizeof(data) - 1); // remove the null terminator strings add
|
||||
// store the length of the string
|
||||
RtcEeprom.SetMemory(1, written); // store the
|
||||
/* end of comment out section */
|
||||
}
|
||||
|
||||
void loop ()
|
||||
{
|
||||
if (!Rtc.IsDateTimeValid())
|
||||
{
|
||||
if (Rtc.LastError() != 0)
|
||||
{
|
||||
// we have a communications error
|
||||
// see https://www.arduino.cc/en/Reference/WireEndTransmission for
|
||||
// what the number means
|
||||
Serial.print("RTC communications error = ");
|
||||
Serial.println(Rtc.LastError());
|
||||
}
|
||||
else
|
||||
{
|
||||
// Common Causes:
|
||||
// 1) the battery on the device is low or even missing and the power line was disconnected
|
||||
Serial.println("RTC lost confidence in the DateTime!");
|
||||
}
|
||||
}
|
||||
|
||||
RtcDateTime now = Rtc.GetDateTime();
|
||||
|
||||
printDateTime(now);
|
||||
Serial.println();
|
||||
|
||||
delay(5000);
|
||||
|
||||
// read data
|
||||
|
||||
// get the offset we stored our data from address zero
|
||||
uint8_t address = RtcEeprom.GetMemory(0);
|
||||
if (address != stringAddr)
|
||||
{
|
||||
Serial.print("address didn't match ");
|
||||
Serial.println(address);
|
||||
}
|
||||
|
||||
{
|
||||
// get the size of the data from address 1
|
||||
uint8_t count = RtcEeprom.GetMemory(1);
|
||||
uint8_t buff[64];
|
||||
|
||||
// get our data from the address with the given size
|
||||
uint8_t gotten = RtcEeprom.GetMemory(address, buff, count);
|
||||
|
||||
if (gotten != count ||
|
||||
count != sizeof(data) - 1) // remove the extra null terminator strings add
|
||||
{
|
||||
Serial.print("something didn't match, count = ");
|
||||
Serial.print(count, DEC);
|
||||
Serial.print(", gotten = ");
|
||||
Serial.print(gotten, DEC);
|
||||
Serial.println();
|
||||
}
|
||||
Serial.print("data read (");
|
||||
Serial.print(gotten);
|
||||
Serial.print(") = \"");
|
||||
for (uint8_t ch = 0; ch < gotten; ch++)
|
||||
{
|
||||
Serial.print((char)buff[ch]);
|
||||
}
|
||||
Serial.println("\"");
|
||||
}
|
||||
|
||||
|
||||
delay(5000);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void printDateTime(const RtcDateTime& dt)
|
||||
{
|
||||
char datestring[20];
|
||||
|
||||
snprintf_P(datestring,
|
||||
countof(datestring),
|
||||
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
|
||||
dt.Month(),
|
||||
dt.Day(),
|
||||
dt.Year(),
|
||||
dt.Hour(),
|
||||
dt.Minute(),
|
||||
dt.Second() );
|
||||
Serial.print(datestring);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,145 @@
|
||||
|
||||
// CONNECTIONS:
|
||||
// DS3231 SDA --> SDA
|
||||
// DS3231 SCL --> SCL
|
||||
// DS3231 VCC --> 3.3v or 5v
|
||||
// DS3231 GND --> GND
|
||||
|
||||
/* for software wire use below
|
||||
#include <SoftwareWire.h> // must be included here so that Arduino library object file references work
|
||||
#include <RtcDS3231.h>
|
||||
|
||||
SoftwareWire myWire(SDA, SCL);
|
||||
RtcDS3231<SoftwareWire> Rtc(myWire);
|
||||
for software wire use above */
|
||||
|
||||
/* for normal hardware wire use below */
|
||||
#include <Wire.h> // must be included here so that Arduino library object file references work
|
||||
#include <RtcDS3231.h>
|
||||
RtcDS3231<TwoWire> Rtc(Wire);
|
||||
/* for normal hardware wire use above */
|
||||
|
||||
|
||||
void setup ()
|
||||
{
|
||||
Serial.begin(57600);
|
||||
|
||||
Serial.print("compiled: ");
|
||||
Serial.print(__DATE__);
|
||||
Serial.println(__TIME__);
|
||||
|
||||
//--------RTC SETUP ------------
|
||||
// if you are using ESP-01 then uncomment the line below to reset the pins to
|
||||
// the available pins for SDA, SCL
|
||||
// Wire.begin(0, 2); // due to limited pins, use pin 0 and 2 for SDA, SCL
|
||||
|
||||
Rtc.Begin();
|
||||
|
||||
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
|
||||
printDateTime(compiled);
|
||||
Serial.println();
|
||||
|
||||
if (!Rtc.IsDateTimeValid())
|
||||
{
|
||||
if (Rtc.LastError() != 0)
|
||||
{
|
||||
// we have a communications error
|
||||
// see https://www.arduino.cc/en/Reference/WireEndTransmission for
|
||||
// what the number means
|
||||
Serial.print("RTC communications error = ");
|
||||
Serial.println(Rtc.LastError());
|
||||
}
|
||||
else
|
||||
{
|
||||
// Common Causes:
|
||||
// 1) first time you ran and the device wasn't running yet
|
||||
// 2) the battery on the device is low or even missing
|
||||
|
||||
Serial.println("RTC lost confidence in the DateTime!");
|
||||
|
||||
// following line sets the RTC to the date & time this sketch was compiled
|
||||
// it will also reset the valid flag internally unless the Rtc device is
|
||||
// having an issue
|
||||
|
||||
Rtc.SetDateTime(compiled);
|
||||
}
|
||||
}
|
||||
|
||||
if (!Rtc.GetIsRunning())
|
||||
{
|
||||
Serial.println("RTC was not actively running, starting now");
|
||||
Rtc.SetIsRunning(true);
|
||||
}
|
||||
|
||||
RtcDateTime now = Rtc.GetDateTime();
|
||||
if (now < compiled)
|
||||
{
|
||||
Serial.println("RTC is older than compile time! (Updating DateTime)");
|
||||
Rtc.SetDateTime(compiled);
|
||||
}
|
||||
else if (now > compiled)
|
||||
{
|
||||
Serial.println("RTC is newer than compile time. (this is expected)");
|
||||
}
|
||||
else if (now == compiled)
|
||||
{
|
||||
Serial.println("RTC is the same as compile time! (not expected but all is fine)");
|
||||
}
|
||||
|
||||
// never assume the Rtc was last configured by you, so
|
||||
// just clear them to your needed state
|
||||
Rtc.Enable32kHzPin(false);
|
||||
Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeNone);
|
||||
}
|
||||
|
||||
void loop ()
|
||||
{
|
||||
if (!Rtc.IsDateTimeValid())
|
||||
{
|
||||
if (Rtc.LastError() != 0)
|
||||
{
|
||||
// we have a communications error
|
||||
// see https://www.arduino.cc/en/Reference/WireEndTransmission for
|
||||
// what the number means
|
||||
Serial.print("RTC communications error = ");
|
||||
Serial.println(Rtc.LastError());
|
||||
}
|
||||
else
|
||||
{
|
||||
// Common Causes:
|
||||
// 1) the battery on the device is low or even missing and the power line was disconnected
|
||||
Serial.println("RTC lost confidence in the DateTime!");
|
||||
}
|
||||
}
|
||||
|
||||
RtcDateTime now = Rtc.GetDateTime();
|
||||
printDateTime(now);
|
||||
Serial.println();
|
||||
|
||||
RtcTemperature temp = Rtc.GetTemperature();
|
||||
temp.Print(Serial);
|
||||
// you may also get the temperature as a float and print it
|
||||
// Serial.print(temp.AsFloatDegC());
|
||||
Serial.println("C");
|
||||
|
||||
delay(10000); // ten seconds
|
||||
}
|
||||
|
||||
#define countof(a) (sizeof(a) / sizeof(a[0]))
|
||||
|
||||
void printDateTime(const RtcDateTime& dt)
|
||||
{
|
||||
char datestring[20];
|
||||
|
||||
snprintf_P(datestring,
|
||||
countof(datestring),
|
||||
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
|
||||
dt.Month(),
|
||||
dt.Day(),
|
||||
dt.Year(),
|
||||
dt.Hour(),
|
||||
dt.Minute(),
|
||||
dt.Second() );
|
||||
Serial.print(datestring);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
|
||||
// CONNECTIONS:
|
||||
// DS3231 SDA --> SDA
|
||||
// DS3231 SCL --> SCL
|
||||
// DS3231 VCC --> 3.3v or 5v
|
||||
// DS3231 GND --> GND
|
||||
|
||||
/* for software wire use below
|
||||
#include <SoftwareWire.h> // must be included here so that Arduino library object file references work
|
||||
#include <RtcDS3231.h>
|
||||
|
||||
SoftwareWire myWire(SDA, SCL);
|
||||
RtcDS3231<SoftwareWire> Rtc(myWire);
|
||||
for software wire use above */
|
||||
|
||||
/* for normal hardware wire use below */
|
||||
#include <Wire.h> // must be included here so that Arduino library object file references work
|
||||
#include <RtcDS3231.h>
|
||||
RtcDS3231<TwoWire> Rtc(Wire);
|
||||
/* for normal hardware wire use above */
|
||||
|
||||
|
||||
void setup ()
|
||||
{
|
||||
Serial.begin(57600);
|
||||
|
||||
Serial.print("compiled: ");
|
||||
Serial.print(__DATE__);
|
||||
Serial.println(__TIME__);
|
||||
|
||||
//--------RTC SETUP ------------
|
||||
// if you are using ESP-01 then uncomment the line below to reset the pins to
|
||||
// the available pins for SDA, SCL
|
||||
// Wire.begin(0, 2); // due to limited pins, use pin 0 and 2 for SDA, SCL
|
||||
|
||||
Rtc.Begin();
|
||||
|
||||
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
|
||||
printDateTime(compiled);
|
||||
Serial.println();
|
||||
|
||||
if (!Rtc.IsDateTimeValid())
|
||||
{
|
||||
Serial.println("RTC lost confidence in the DateTime!");
|
||||
Rtc.SetDateTime(compiled);
|
||||
}
|
||||
|
||||
if (!Rtc.GetIsRunning())
|
||||
{
|
||||
Serial.println("RTC was not actively running, starting now");
|
||||
Rtc.SetIsRunning(true);
|
||||
}
|
||||
|
||||
RtcDateTime now = Rtc.GetDateTime();
|
||||
if (now < compiled)
|
||||
{
|
||||
Serial.println("RTC is older than compile time! (Updating DateTime)");
|
||||
Rtc.SetDateTime(compiled);
|
||||
}
|
||||
|
||||
// never assume the Rtc was last configured by you, so
|
||||
// just clear them to your needed state
|
||||
Rtc.Enable32kHzPin(false);
|
||||
Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeNone);
|
||||
}
|
||||
|
||||
void loop ()
|
||||
{
|
||||
RtcDateTime now = Rtc.GetDateTime();
|
||||
|
||||
printDateTime(now);
|
||||
Serial.println();
|
||||
|
||||
for(;;)
|
||||
{
|
||||
Rtc.SetIsRunning(false);
|
||||
Serial.println(">>> Rtc ready for storage <<<");
|
||||
|
||||
delay(10000); // ten seconds
|
||||
}
|
||||
}
|
||||
|
||||
#define countof(a) (sizeof(a) / sizeof(a[0]))
|
||||
|
||||
void printDateTime(const RtcDateTime& dt)
|
||||
{
|
||||
char datestring[20];
|
||||
|
||||
snprintf_P(datestring,
|
||||
countof(datestring),
|
||||
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
|
||||
dt.Month(),
|
||||
dt.Day(),
|
||||
dt.Year(),
|
||||
dt.Hour(),
|
||||
dt.Minute(),
|
||||
dt.Second() );
|
||||
Serial.print(datestring);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,179 @@
|
||||
|
||||
// Reference for connecting SPI see https://www.arduino.cc/en/Reference/SPI
|
||||
// CONNECTIONS:
|
||||
// DS3234 MISO --> MISO
|
||||
// DS3234 MOSI --> MOSI
|
||||
// DS3234 CLK --> CLK (SCK)
|
||||
// DS3234 CS (SS) --> 5 (pin used to select the DS3234 on the SPI)
|
||||
// DS3234 VCC --> 3.3v or 5v
|
||||
// DS3234 GND --> GND
|
||||
// SQW ---> (Pin19) Don't forget to pullup (4.7k to 10k to VCC)
|
||||
|
||||
const uint8_t DS3234_CS_PIN = 5;
|
||||
|
||||
#include <SPI.h>
|
||||
#include <RtcDS3234.h>
|
||||
|
||||
RtcDS3234<SPIClass> Rtc(SPI, DS3234_CS_PIN);
|
||||
|
||||
// Interrupt Pin Lookup Table
|
||||
// (copied from Arduino Docs)
|
||||
//
|
||||
// CAUTION: The interrupts are Arduino numbers NOT Atmel numbers
|
||||
// and may not match (example, Mega2560 int.4 is actually Atmel Int2)
|
||||
// this is only an issue if you plan to use the lower level interupt features
|
||||
//
|
||||
// Board int.0 int.1 int.2 int.3 int.4 int.5
|
||||
// ---------------------------------------------------------------
|
||||
// Uno, Ethernet 2 3
|
||||
// Mega2560 2 3 21 20 [19] 18
|
||||
// Leonardo 3 2 0 1 7
|
||||
// esp8266 (pin and interrupt should be the same thing)
|
||||
// esp32 (pin and interrupt should be the same thing)
|
||||
|
||||
#define RtcSquareWavePin 19 // Mega2560
|
||||
#define RtcSquareWaveInterrupt 4 // Mega2560
|
||||
|
||||
// marked volatile so interrupt can safely modify them and
|
||||
// other code can safely read and modify them
|
||||
volatile uint16_t interuptCount = 0;
|
||||
volatile bool interuptFlag = false;
|
||||
|
||||
void ISR_ATTR InteruptServiceRoutine()
|
||||
{
|
||||
// since this interupted any other running code,
|
||||
// don't do anything that takes long and especially avoid
|
||||
// any communications calls within this routine
|
||||
interuptCount++;
|
||||
interuptFlag = true;
|
||||
}
|
||||
|
||||
void setup ()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while (!Serial);
|
||||
|
||||
// set the interupt pin to input mode
|
||||
pinMode(RtcSquareWavePin, INPUT_PULLUP); // external pullup maybe required still
|
||||
|
||||
SPI.begin();
|
||||
Rtc.Begin();
|
||||
|
||||
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
|
||||
|
||||
if (!Rtc.IsDateTimeValid())
|
||||
{
|
||||
Serial.println("RTC lost confidence in the DateTime!");
|
||||
Rtc.SetDateTime(compiled);
|
||||
}
|
||||
|
||||
if (!Rtc.GetIsRunning())
|
||||
{
|
||||
Serial.println("RTC was not actively running, starting now");
|
||||
Rtc.SetIsRunning(true);
|
||||
}
|
||||
|
||||
RtcDateTime now = Rtc.GetDateTime();
|
||||
if (now < compiled)
|
||||
{
|
||||
Serial.println("RTC is older than compile time! (Updating DateTime)");
|
||||
Rtc.SetDateTime(compiled);
|
||||
}
|
||||
|
||||
Rtc.Enable32kHzPin(false);
|
||||
Rtc.SetSquareWavePin(DS3234SquareWavePin_ModeAlarmBoth);
|
||||
|
||||
// Alarm 1 set to trigger every day when
|
||||
// the hours, minutes, and seconds match
|
||||
RtcDateTime alarmTime = now + 88; // into the future
|
||||
DS3234AlarmOne alarm1(
|
||||
alarmTime.Day(),
|
||||
alarmTime.Hour(),
|
||||
alarmTime.Minute(),
|
||||
alarmTime.Second(),
|
||||
DS3234AlarmOneControl_HoursMinutesSecondsMatch);
|
||||
Rtc.SetAlarmOne(alarm1);
|
||||
|
||||
// Alarm 2 set to trigger at the top of the minute
|
||||
DS3234AlarmTwo alarm2(
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
DS3234AlarmTwoControl_OncePerMinute);
|
||||
Rtc.SetAlarmTwo(alarm2);
|
||||
|
||||
// throw away any old alarm state before we ran
|
||||
Rtc.LatchAlarmsTriggeredFlags();
|
||||
|
||||
// setup external interupt
|
||||
attachInterrupt(RtcSquareWaveInterrupt, InteruptServiceRoutine, FALLING);
|
||||
}
|
||||
|
||||
void loop ()
|
||||
{
|
||||
if (!Rtc.IsDateTimeValid())
|
||||
{
|
||||
Serial.println("RTC lost confidence in the DateTime!");
|
||||
}
|
||||
|
||||
RtcDateTime now = Rtc.GetDateTime();
|
||||
|
||||
printDateTime(now);
|
||||
Serial.println();
|
||||
|
||||
// we only want to show time every 10 seconds
|
||||
// but we want to show responce to the interupt firing
|
||||
for (int timeCount = 0; timeCount < 20; timeCount++)
|
||||
{
|
||||
if (Alarmed())
|
||||
{
|
||||
Serial.print(">>Interupt Count: ");
|
||||
Serial.print(interuptCount);
|
||||
Serial.println("<<");
|
||||
}
|
||||
delay(500);
|
||||
}
|
||||
}
|
||||
|
||||
bool Alarmed()
|
||||
{
|
||||
bool wasAlarmed = false;
|
||||
if (interuptFlag) // check our flag that gets sets in the interupt
|
||||
{
|
||||
wasAlarmed = true;
|
||||
interuptFlag = false; // reset the flag
|
||||
|
||||
// this gives us which alarms triggered and
|
||||
// then allows for others to trigger again
|
||||
DS3234AlarmFlag flag = Rtc.LatchAlarmsTriggeredFlags();
|
||||
|
||||
if (flag & DS3234AlarmFlag_Alarm1)
|
||||
{
|
||||
Serial.println("alarm one triggered");
|
||||
}
|
||||
if (flag & DS3234AlarmFlag_Alarm2)
|
||||
{
|
||||
Serial.println("alarm two triggered");
|
||||
}
|
||||
}
|
||||
return wasAlarmed;
|
||||
}
|
||||
|
||||
#define countof(a) (sizeof(a) / sizeof(a[0]))
|
||||
|
||||
void printDateTime(const RtcDateTime& dt)
|
||||
{
|
||||
char datestring[20];
|
||||
|
||||
snprintf_P(datestring,
|
||||
countof(datestring),
|
||||
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
|
||||
dt.Month(),
|
||||
dt.Day(),
|
||||
dt.Year(),
|
||||
dt.Hour(),
|
||||
dt.Minute(),
|
||||
dt.Second() );
|
||||
Serial.print(datestring);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,139 @@
|
||||
|
||||
// Reference for connecting SPI see https://www.arduino.cc/en/Reference/SPI
|
||||
// CONNECTIONS:
|
||||
// DS3234 MISO --> MISO
|
||||
// DS3234 MOSI --> MOSI
|
||||
// DS3234 CLK --> CLK (SCK)
|
||||
// DS3234 CS (SS) --> 5 (pin used to select the DS3234 on the SPI)
|
||||
// DS3234 VCC --> 3.3v or 5v
|
||||
// DS3234 GND --> GND
|
||||
|
||||
const uint8_t DS3234_CS_PIN = 5;
|
||||
|
||||
#include <SPI.h>
|
||||
#include <RtcDS3234.h>
|
||||
|
||||
RtcDS3234<SPIClass> Rtc(SPI, DS3234_CS_PIN);
|
||||
|
||||
const char data[] = "what time is it";
|
||||
|
||||
void setup ()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while (!Serial);
|
||||
|
||||
Serial.print("compiled: ");
|
||||
Serial.print(__DATE__);
|
||||
Serial.println(__TIME__);
|
||||
|
||||
SPI.begin();
|
||||
Rtc.Begin();
|
||||
|
||||
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
|
||||
printDateTime(compiled);
|
||||
Serial.println();
|
||||
|
||||
if (!Rtc.IsDateTimeValid())
|
||||
{
|
||||
Serial.println("RTC lost confidence in the DateTime!");
|
||||
Rtc.SetDateTime(compiled);
|
||||
}
|
||||
|
||||
if (!Rtc.GetIsRunning())
|
||||
{
|
||||
Serial.println("RTC was not actively running, starting now");
|
||||
Rtc.SetIsRunning(true);
|
||||
}
|
||||
|
||||
RtcDateTime now = Rtc.GetDateTime();
|
||||
if (now < compiled)
|
||||
{
|
||||
Serial.println("RTC is older than compile time! (Updating DateTime)");
|
||||
Rtc.SetDateTime(compiled);
|
||||
}
|
||||
|
||||
// never assume the Rtc was last configured by you, so
|
||||
// just clear them to your needed state
|
||||
Rtc.SetSquareWavePin(DS3234SquareWavePin_ModeNone);
|
||||
|
||||
/* comment out on a second run to see that the info is stored long term */
|
||||
// Store something in memory on the RTC
|
||||
Rtc.SetMemory(0, 13); // address of a data item
|
||||
uint8_t written = Rtc.SetMemory(13, (const uint8_t*)data, sizeof(data) - 1); // remove the null terminator strings add
|
||||
Rtc.SetMemory(1, written); // size of data time
|
||||
/* end of comment out section */
|
||||
}
|
||||
|
||||
void loop ()
|
||||
{
|
||||
if (!Rtc.IsDateTimeValid())
|
||||
{
|
||||
// Common Causes:
|
||||
// 1) the battery on the device is low or even missing and the power line was disconnected
|
||||
Serial.println("RTC lost confidence in the DateTime!");
|
||||
}
|
||||
|
||||
RtcDateTime now = Rtc.GetDateTime();
|
||||
|
||||
printDateTime(now);
|
||||
Serial.println();
|
||||
|
||||
delay(5000);
|
||||
|
||||
// read data
|
||||
|
||||
// get the offset we stored our data from address zero
|
||||
uint8_t address = Rtc.GetMemory(0);
|
||||
if (address != 13)
|
||||
{
|
||||
Serial.println("address didn't match");
|
||||
}
|
||||
else
|
||||
{
|
||||
// get the size of the data from address 1
|
||||
uint8_t count = Rtc.GetMemory(1);
|
||||
uint8_t buff[20];
|
||||
|
||||
// get our data from the address with the given size
|
||||
uint8_t gotten = Rtc.GetMemory(address, buff, count);
|
||||
|
||||
if (gotten != count ||
|
||||
count != sizeof(data) - 1) // remove the extra null terminator strings add
|
||||
{
|
||||
Serial.print("something didn't match, count = ");
|
||||
Serial.print(count, DEC);
|
||||
Serial.print(", gotten = ");
|
||||
Serial.print(gotten, DEC);
|
||||
Serial.println();
|
||||
}
|
||||
Serial.print("data read (");
|
||||
Serial.print(gotten);
|
||||
Serial.print(") = \"");
|
||||
for (uint8_t ch = 0; ch < gotten; ch++)
|
||||
{
|
||||
Serial.print((char)buff[ch]);
|
||||
}
|
||||
Serial.println("\"");
|
||||
}
|
||||
|
||||
delay(5000);
|
||||
}
|
||||
|
||||
#define countof(a) (sizeof(a) / sizeof(a[0]))
|
||||
|
||||
void printDateTime(const RtcDateTime& dt)
|
||||
{
|
||||
char datestring[20];
|
||||
|
||||
snprintf_P(datestring,
|
||||
countof(datestring),
|
||||
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
|
||||
dt.Month(),
|
||||
dt.Day(),
|
||||
dt.Year(),
|
||||
dt.Hour(),
|
||||
dt.Minute(),
|
||||
dt.Second() );
|
||||
Serial.print(datestring);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
|
||||
// Reference for connecting SPI see https://www.arduino.cc/en/Reference/SPI
|
||||
// CONNECTIONS:
|
||||
// DS3234 MISO --> MISO
|
||||
// DS3234 MOSI --> MOSI
|
||||
// DS3234 CLK --> CLK (SCK)
|
||||
// DS3234 CS (SS) --> 5 (pin used to select the DS3234 on the SPI)
|
||||
// DS3234 VCC --> 3.3v or 5v
|
||||
// DS3234 GND --> GND
|
||||
|
||||
const uint8_t DS3234_CS_PIN = 5;
|
||||
|
||||
#include <SPI.h>
|
||||
#include <RtcDS3234.h>
|
||||
|
||||
RtcDS3234<SPIClass> Rtc(SPI, DS3234_CS_PIN);
|
||||
|
||||
void setup ()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while (!Serial);
|
||||
|
||||
Serial.print("compiled: ");
|
||||
Serial.print(__DATE__);
|
||||
Serial.println(__TIME__);
|
||||
|
||||
SPI.begin();
|
||||
Rtc.Begin();
|
||||
|
||||
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
|
||||
printDateTime(compiled);
|
||||
Serial.println();
|
||||
|
||||
if (!Rtc.IsDateTimeValid())
|
||||
{
|
||||
// Common Causes:
|
||||
// 1) first time you ran and the device wasn't running yet
|
||||
// 2) the battery on the device is low or even missing
|
||||
|
||||
Serial.println("RTC lost confidence in the DateTime!");
|
||||
|
||||
// following line sets the RTC to the date & time this sketch was compiled
|
||||
// it will also reset the valid flag internally unless the Rtc device is
|
||||
// having an issue
|
||||
|
||||
Rtc.SetDateTime(compiled);
|
||||
}
|
||||
|
||||
if (!Rtc.GetIsRunning())
|
||||
{
|
||||
Serial.println("RTC was not actively running, starting now");
|
||||
Rtc.SetIsRunning(true);
|
||||
}
|
||||
|
||||
RtcDateTime now = Rtc.GetDateTime();
|
||||
if (now < compiled)
|
||||
{
|
||||
Serial.println("RTC is older than compile time! (Updating DateTime)");
|
||||
Rtc.SetDateTime(compiled);
|
||||
}
|
||||
else if (now > compiled)
|
||||
{
|
||||
Serial.println("RTC is newer than compile time. (this is expected)");
|
||||
}
|
||||
else if (now == compiled)
|
||||
{
|
||||
Serial.println("RTC is the same as compile time! (not expected but all is fine)");
|
||||
}
|
||||
|
||||
// never assume the Rtc was last configured by you, so
|
||||
// just clear them to your needed state
|
||||
Rtc.Enable32kHzPin(false);
|
||||
Rtc.SetSquareWavePin(DS3234SquareWavePin_ModeNone);
|
||||
}
|
||||
|
||||
void loop ()
|
||||
{
|
||||
if (!Rtc.IsDateTimeValid())
|
||||
{
|
||||
// Common Causes:
|
||||
// 1) the battery on the device is low or even missing and the power line was disconnected
|
||||
Serial.println("RTC lost confidence in the DateTime!");
|
||||
}
|
||||
|
||||
RtcDateTime now = Rtc.GetDateTime();
|
||||
printDateTime(now);
|
||||
Serial.println();
|
||||
|
||||
RtcTemperature temp = Rtc.GetTemperature();
|
||||
temp.Print(Serial);
|
||||
// you may also get the temperature as a float and print it
|
||||
// Serial.print(temp.AsFloatDegC());
|
||||
Serial.println("C");
|
||||
|
||||
delay(10000); // ten seconds
|
||||
}
|
||||
|
||||
#define countof(a) (sizeof(a) / sizeof(a[0]))
|
||||
|
||||
void printDateTime(const RtcDateTime& dt)
|
||||
{
|
||||
char datestring[20];
|
||||
|
||||
snprintf_P(datestring,
|
||||
countof(datestring),
|
||||
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
|
||||
dt.Month(),
|
||||
dt.Day(),
|
||||
dt.Year(),
|
||||
dt.Hour(),
|
||||
dt.Minute(),
|
||||
dt.Second() );
|
||||
Serial.print(datestring);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user