初始化提交
This commit is contained in:
@@ -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