Update librairies + optimizations
loop time and memory optimization
This commit is contained in:
8
libraries/RTClib/README.txt
Normal file
8
libraries/RTClib/README.txt
Normal file
@@ -0,0 +1,8 @@
|
||||
This is a fork of JeeLab's fantastic real time clock library for Arduino.
|
||||
|
||||
For details on using this library with an RTC module like the DS1307, see the guide at: https://learn.adafruit.com/ds1307-real-time-clock-breakout-board-kit/overview
|
||||
|
||||
To download. click the DOWNLOADS button to the right, and rename the uncompressed folder RTClib.
|
||||
|
||||
Place the RTClib folder in your *arduinosketchfolder*/libraries/ folder.
|
||||
You may need to create the libraries subfolder if its your first library. Restart the IDE.
|
||||
@@ -12,15 +12,22 @@
|
||||
#define WIRE Wire1
|
||||
#endif
|
||||
|
||||
#define DS1307_ADDRESS 0x68
|
||||
#define DS1307_ADDRESS 0x68
|
||||
#define DS1307_CONTROL 0x07
|
||||
#define DS1307_NVRAM 0x08
|
||||
#define SECONDS_PER_DAY 86400L
|
||||
|
||||
#define SECONDS_FROM_1970_TO_2000 946684800
|
||||
|
||||
#if (ARDUINO >= 100)
|
||||
#include <Arduino.h> // capital A so it is error prone on case-sensitive filesystems
|
||||
// Macro to deal with the difference in I2C write functions from old and new Arduino versions.
|
||||
#define _I2C_WRITE write
|
||||
#define _I2C_READ read
|
||||
#else
|
||||
#include <WProgram.h>
|
||||
#define _I2C_WRITE send
|
||||
#define _I2C_READ receive
|
||||
#endif
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -86,6 +93,15 @@ DateTime::DateTime (uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uin
|
||||
ss = sec;
|
||||
}
|
||||
|
||||
DateTime::DateTime (const DateTime& copy):
|
||||
yOff(copy.yOff),
|
||||
m(copy.m),
|
||||
d(copy.d),
|
||||
hh(copy.hh),
|
||||
mm(copy.mm),
|
||||
ss(copy.ss)
|
||||
{}
|
||||
|
||||
static uint8_t conv2d(const char* p) {
|
||||
uint8_t v = 0;
|
||||
if ('0' <= *p && *p <= '9')
|
||||
@@ -95,7 +111,7 @@ static uint8_t conv2d(const char* p) {
|
||||
|
||||
// A convenient constructor for using "the compiler's time":
|
||||
// DateTime now (__DATE__, __TIME__);
|
||||
// NOTE: using PSTR would further reduce the RAM footprint
|
||||
// NOTE: using F() would further reduce the RAM footprint, see below.
|
||||
DateTime::DateTime (const char* date, const char* time) {
|
||||
// sample input: date = "Dec 26 2009", time = "12:34:56"
|
||||
yOff = conv2d(date + 9);
|
||||
@@ -116,6 +132,32 @@ DateTime::DateTime (const char* date, const char* time) {
|
||||
ss = conv2d(time + 6);
|
||||
}
|
||||
|
||||
// A convenient constructor for using "the compiler's time":
|
||||
// This version will save RAM by using PROGMEM to store it by using the F macro.
|
||||
// DateTime now (F(__DATE__), F(__TIME__));
|
||||
DateTime::DateTime (const __FlashStringHelper* date, const __FlashStringHelper* time) {
|
||||
// sample input: date = "Dec 26 2009", time = "12:34:56"
|
||||
char buff[11];
|
||||
memcpy_P(buff, date, 11);
|
||||
yOff = conv2d(buff + 9);
|
||||
// Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
|
||||
switch (buff[0]) {
|
||||
case 'J': m = buff[1] == 'a' ? 1 : m = buff[2] == 'n' ? 6 : 7; break;
|
||||
case 'F': m = 2; break;
|
||||
case 'A': m = buff[2] == 'r' ? 4 : 8; break;
|
||||
case 'M': m = buff[2] == 'r' ? 3 : 5; break;
|
||||
case 'S': m = 9; break;
|
||||
case 'O': m = 10; break;
|
||||
case 'N': m = 11; break;
|
||||
case 'D': m = 12; break;
|
||||
}
|
||||
d = conv2d(buff + 4);
|
||||
memcpy_P(buff, time, 8);
|
||||
hh = conv2d(buff);
|
||||
mm = conv2d(buff + 3);
|
||||
ss = conv2d(buff + 6);
|
||||
}
|
||||
|
||||
uint8_t DateTime::dayOfWeek() const {
|
||||
uint16_t day = date2days(yOff, m, d);
|
||||
return (day + 6) % 7; // Jan 1, 2000 is a Saturday, i.e. returns 6
|
||||
@@ -130,6 +172,48 @@ uint32_t DateTime::unixtime(void) const {
|
||||
return t;
|
||||
}
|
||||
|
||||
long DateTime::secondstime(void) const {
|
||||
long t;
|
||||
uint16_t days = date2days(yOff, m, d);
|
||||
t = time2long(days, hh, mm, ss);
|
||||
return t;
|
||||
}
|
||||
|
||||
DateTime DateTime::operator+(const TimeSpan& span) {
|
||||
return DateTime(unixtime()+span.totalseconds());
|
||||
}
|
||||
|
||||
DateTime DateTime::operator-(const TimeSpan& span) {
|
||||
return DateTime(unixtime()-span.totalseconds());
|
||||
}
|
||||
|
||||
TimeSpan DateTime::operator-(const DateTime& right) {
|
||||
return TimeSpan(unixtime()-right.unixtime());
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// TimeSpan implementation
|
||||
|
||||
TimeSpan::TimeSpan (int32_t seconds):
|
||||
_seconds(seconds)
|
||||
{}
|
||||
|
||||
TimeSpan::TimeSpan (int16_t days, int8_t hours, int8_t minutes, int8_t seconds):
|
||||
_seconds(days*86400L + hours*3600 + minutes*60 + seconds)
|
||||
{}
|
||||
|
||||
TimeSpan::TimeSpan (const TimeSpan& copy):
|
||||
_seconds(copy._seconds)
|
||||
{}
|
||||
|
||||
TimeSpan TimeSpan::operator+(const TimeSpan& right) {
|
||||
return TimeSpan(_seconds+right._seconds);
|
||||
}
|
||||
|
||||
TimeSpan TimeSpan::operator-(const TimeSpan& right) {
|
||||
return TimeSpan(_seconds-right._seconds);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// RTC_DS1307 implementation
|
||||
|
||||
@@ -140,95 +224,99 @@ uint8_t RTC_DS1307::begin(void) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
#if (ARDUINO >= 100)
|
||||
|
||||
uint8_t RTC_DS1307::isrunning(void) {
|
||||
WIRE.beginTransmission(DS1307_ADDRESS);
|
||||
WIRE.write(0);
|
||||
WIRE._I2C_WRITE(0);
|
||||
WIRE.endTransmission();
|
||||
|
||||
WIRE.requestFrom(DS1307_ADDRESS, 1);
|
||||
uint8_t ss = WIRE.read();
|
||||
uint8_t ss = WIRE._I2C_READ();
|
||||
return !(ss>>7);
|
||||
}
|
||||
|
||||
void RTC_DS1307::adjust(const DateTime& dt) {
|
||||
WIRE.beginTransmission(DS1307_ADDRESS);
|
||||
WIRE.write(0);
|
||||
WIRE.write(bin2bcd(dt.second()));
|
||||
WIRE.write(bin2bcd(dt.minute()));
|
||||
WIRE.write(bin2bcd(dt.hour()));
|
||||
WIRE.write(bin2bcd(0));
|
||||
WIRE.write(bin2bcd(dt.day()));
|
||||
WIRE.write(bin2bcd(dt.month()));
|
||||
WIRE.write(bin2bcd(dt.year() - 2000));
|
||||
WIRE.write(0);
|
||||
WIRE.endTransmission();
|
||||
WIRE.beginTransmission(DS1307_ADDRESS);
|
||||
WIRE._I2C_WRITE(0);
|
||||
WIRE._I2C_WRITE(bin2bcd(dt.second()));
|
||||
WIRE._I2C_WRITE(bin2bcd(dt.minute()));
|
||||
WIRE._I2C_WRITE(bin2bcd(dt.hour()));
|
||||
WIRE._I2C_WRITE(bin2bcd(0));
|
||||
WIRE._I2C_WRITE(bin2bcd(dt.day()));
|
||||
WIRE._I2C_WRITE(bin2bcd(dt.month()));
|
||||
WIRE._I2C_WRITE(bin2bcd(dt.year() - 2000));
|
||||
WIRE._I2C_WRITE(0);
|
||||
WIRE.endTransmission();
|
||||
}
|
||||
|
||||
DateTime RTC_DS1307::now() {
|
||||
WIRE.beginTransmission(DS1307_ADDRESS);
|
||||
WIRE.write(0);
|
||||
WIRE._I2C_WRITE(0);
|
||||
WIRE.endTransmission();
|
||||
|
||||
WIRE.requestFrom(DS1307_ADDRESS, 7);
|
||||
uint8_t ss = bcd2bin(WIRE.read() & 0x7F);
|
||||
uint8_t mm = bcd2bin(WIRE.read());
|
||||
uint8_t hh = bcd2bin(WIRE.read());
|
||||
WIRE.read();
|
||||
uint8_t d = bcd2bin(WIRE.read());
|
||||
uint8_t m = bcd2bin(WIRE.read());
|
||||
uint16_t y = bcd2bin(WIRE.read()) + 2000;
|
||||
uint8_t ss = bcd2bin(WIRE._I2C_READ() & 0x7F);
|
||||
uint8_t mm = bcd2bin(WIRE._I2C_READ());
|
||||
uint8_t hh = bcd2bin(WIRE._I2C_READ());
|
||||
WIRE._I2C_READ();
|
||||
uint8_t d = bcd2bin(WIRE._I2C_READ());
|
||||
uint8_t m = bcd2bin(WIRE._I2C_READ());
|
||||
uint16_t y = bcd2bin(WIRE._I2C_READ()) + 2000;
|
||||
|
||||
return DateTime (y, m, d, hh, mm, ss);
|
||||
}
|
||||
|
||||
#else
|
||||
Ds1307SqwPinMode RTC_DS1307::readSqwPinMode() {
|
||||
int mode;
|
||||
|
||||
uint8_t RTC_DS1307::isrunning(void) {
|
||||
WIRE.beginTransmission(DS1307_ADDRESS);
|
||||
WIRE.send(0);
|
||||
WIRE.endTransmission();
|
||||
|
||||
WIRE.requestFrom(DS1307_ADDRESS, 1);
|
||||
uint8_t ss = WIRE.receive();
|
||||
return !(ss>>7);
|
||||
}
|
||||
|
||||
void RTC_DS1307::adjust(const DateTime& dt) {
|
||||
WIRE.beginTransmission(DS1307_ADDRESS);
|
||||
WIRE.send(0);
|
||||
WIRE.send(bin2bcd(dt.second()));
|
||||
WIRE.send(bin2bcd(dt.minute()));
|
||||
WIRE.send(bin2bcd(dt.hour()));
|
||||
WIRE.send(bin2bcd(0));
|
||||
WIRE.send(bin2bcd(dt.day()));
|
||||
WIRE.send(bin2bcd(dt.month()));
|
||||
WIRE.send(bin2bcd(dt.year() - 2000));
|
||||
WIRE.send(0);
|
||||
WIRE.endTransmission();
|
||||
}
|
||||
|
||||
DateTime RTC_DS1307::now() {
|
||||
WIRE.beginTransmission(DS1307_ADDRESS);
|
||||
WIRE.send(0);
|
||||
WIRE._I2C_WRITE(DS1307_CONTROL);
|
||||
WIRE.endTransmission();
|
||||
|
||||
WIRE.requestFrom(DS1307_ADDRESS, 7);
|
||||
uint8_t ss = bcd2bin(WIRE.receive() & 0x7F);
|
||||
uint8_t mm = bcd2bin(WIRE.receive());
|
||||
uint8_t hh = bcd2bin(WIRE.receive());
|
||||
WIRE.receive();
|
||||
uint8_t d = bcd2bin(WIRE.receive());
|
||||
uint8_t m = bcd2bin(WIRE.receive());
|
||||
uint16_t y = bcd2bin(WIRE.receive()) + 2000;
|
||||
|
||||
return DateTime (y, m, d, hh, mm, ss);
|
||||
WIRE.requestFrom((uint8_t)DS1307_ADDRESS, (uint8_t)1);
|
||||
mode = WIRE._I2C_READ();
|
||||
|
||||
mode &= 0x93;
|
||||
return static_cast<Ds1307SqwPinMode>(mode);
|
||||
}
|
||||
|
||||
#endif
|
||||
void RTC_DS1307::writeSqwPinMode(Ds1307SqwPinMode mode) {
|
||||
WIRE.beginTransmission(DS1307_ADDRESS);
|
||||
WIRE._I2C_WRITE(DS1307_CONTROL);
|
||||
WIRE._I2C_WRITE(mode);
|
||||
WIRE.endTransmission();
|
||||
}
|
||||
|
||||
void RTC_DS1307::readnvram(uint8_t* buf, uint8_t size, uint8_t address) {
|
||||
int addrByte = DS1307_NVRAM + address;
|
||||
WIRE.beginTransmission(DS1307_ADDRESS);
|
||||
WIRE._I2C_WRITE(addrByte);
|
||||
WIRE.endTransmission();
|
||||
|
||||
WIRE.requestFrom((uint8_t) DS1307_ADDRESS, size);
|
||||
for (uint8_t pos = 0; pos < size; ++pos) {
|
||||
buf[pos] = WIRE._I2C_READ();
|
||||
}
|
||||
}
|
||||
|
||||
void RTC_DS1307::writenvram(uint8_t address, uint8_t* buf, uint8_t size) {
|
||||
int addrByte = DS1307_NVRAM + address;
|
||||
WIRE.beginTransmission(DS1307_ADDRESS);
|
||||
WIRE._I2C_WRITE(addrByte);
|
||||
for (uint8_t pos = 0; pos < size; ++pos) {
|
||||
WIRE._I2C_WRITE(buf[pos]);
|
||||
}
|
||||
WIRE.endTransmission();
|
||||
}
|
||||
|
||||
uint8_t RTC_DS1307::readnvram(uint8_t address) {
|
||||
uint8_t data;
|
||||
readnvram(&data, 1, address);
|
||||
return data;
|
||||
}
|
||||
|
||||
void RTC_DS1307::writenvram(uint8_t address, uint8_t data) {
|
||||
writenvram(address, &data, 1);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// RTC_Millis implementation
|
||||
|
||||
@@ -4,13 +4,17 @@
|
||||
#ifndef _RTCLIB_H_
|
||||
#define _RTCLIB_H_
|
||||
|
||||
class TimeSpan;
|
||||
|
||||
// Simple general-purpose date/time class (no TZ / DST / leap second handling!)
|
||||
class DateTime {
|
||||
public:
|
||||
DateTime (uint32_t t =0);
|
||||
DateTime (uint16_t year, uint8_t month, uint8_t day,
|
||||
uint8_t hour =0, uint8_t min =0, uint8_t sec =0);
|
||||
DateTime (const DateTime& copy);
|
||||
DateTime (const char* date, const char* time);
|
||||
DateTime (const __FlashStringHelper* date, const __FlashStringHelper* time);
|
||||
uint16_t year() const { return 2000 + yOff; }
|
||||
uint8_t month() const { return m; }
|
||||
uint8_t day() const { return d; }
|
||||
@@ -24,17 +28,48 @@ public:
|
||||
// 32-bit times as seconds since 1/1/1970
|
||||
uint32_t unixtime(void) const;
|
||||
|
||||
DateTime operator+(const TimeSpan& span);
|
||||
DateTime operator-(const TimeSpan& span);
|
||||
TimeSpan operator-(const DateTime& right);
|
||||
|
||||
protected:
|
||||
uint8_t yOff, m, d, hh, mm, ss;
|
||||
};
|
||||
|
||||
// Timespan which can represent changes in time with seconds accuracy.
|
||||
class TimeSpan {
|
||||
public:
|
||||
TimeSpan (int32_t seconds = 0);
|
||||
TimeSpan (int16_t days, int8_t hours, int8_t minutes, int8_t seconds);
|
||||
TimeSpan (const TimeSpan& copy);
|
||||
int16_t days() const { return _seconds / 86400L; }
|
||||
int8_t hours() const { return _seconds / 3600 % 24; }
|
||||
int8_t minutes() const { return _seconds / 60 % 60; }
|
||||
int8_t seconds() const { return _seconds % 60; }
|
||||
int32_t totalseconds() const { return _seconds; }
|
||||
|
||||
TimeSpan operator+(const TimeSpan& right);
|
||||
TimeSpan operator-(const TimeSpan& right);
|
||||
|
||||
protected:
|
||||
int32_t _seconds;
|
||||
};
|
||||
|
||||
// RTC based on the DS1307 chip connected via I2C and the Wire library
|
||||
enum Ds1307SqwPinMode { OFF = 0x00, ON = 0x80, SquareWave1HZ = 0x10, SquareWave4kHz = 0x11, SquareWave8kHz = 0x12, SquareWave32kHz = 0x13 };
|
||||
|
||||
class RTC_DS1307 {
|
||||
public:
|
||||
static uint8_t begin(void);
|
||||
static void adjust(const DateTime& dt);
|
||||
uint8_t isrunning(void);
|
||||
static DateTime now();
|
||||
static Ds1307SqwPinMode readSqwPinMode();
|
||||
static void writeSqwPinMode(Ds1307SqwPinMode mode);
|
||||
uint8_t readnvram(uint8_t address);
|
||||
void readnvram(uint8_t* buf, uint8_t size, uint8_t address);
|
||||
void writenvram(uint8_t address, uint8_t data);
|
||||
void writenvram(uint8_t address, uint8_t* buf, uint8_t size);
|
||||
};
|
||||
|
||||
// RTC using the internal millis() clock, has to be initialized before use
|
||||
|
||||
102
libraries/RTClib/examples/datecalc/datecalc.ino
Normal file
102
libraries/RTClib/examples/datecalc/datecalc.ino
Normal file
@@ -0,0 +1,102 @@
|
||||
// Simple date conversions and calculations
|
||||
|
||||
#include <Wire.h>
|
||||
#include "RTClib.h"
|
||||
|
||||
void showDate(const char* txt, const DateTime& dt) {
|
||||
Serial.print(txt);
|
||||
Serial.print(' ');
|
||||
Serial.print(dt.year(), DEC);
|
||||
Serial.print('/');
|
||||
Serial.print(dt.month(), DEC);
|
||||
Serial.print('/');
|
||||
Serial.print(dt.day(), DEC);
|
||||
Serial.print(' ');
|
||||
Serial.print(dt.hour(), DEC);
|
||||
Serial.print(':');
|
||||
Serial.print(dt.minute(), DEC);
|
||||
Serial.print(':');
|
||||
Serial.print(dt.second(), DEC);
|
||||
|
||||
Serial.print(" = ");
|
||||
Serial.print(dt.unixtime());
|
||||
Serial.print("s / ");
|
||||
Serial.print(dt.unixtime() / 86400L);
|
||||
Serial.print("d since 1970");
|
||||
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void showTimeSpan(const char* txt, const TimeSpan& ts) {
|
||||
Serial.print(txt);
|
||||
Serial.print(" ");
|
||||
Serial.print(ts.days(), DEC);
|
||||
Serial.print(" days ");
|
||||
Serial.print(ts.hours(), DEC);
|
||||
Serial.print(" hours ");
|
||||
Serial.print(ts.minutes(), DEC);
|
||||
Serial.print(" minutes ");
|
||||
Serial.print(ts.seconds(), DEC);
|
||||
Serial.print(" seconds (");
|
||||
Serial.print(ts.totalseconds(), DEC);
|
||||
Serial.print(" total seconds)");
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void setup () {
|
||||
Serial.begin(57600);
|
||||
|
||||
DateTime dt0 (0, 1, 1, 0, 0, 0);
|
||||
showDate("dt0", dt0);
|
||||
|
||||
DateTime dt1 (1, 1, 1, 0, 0, 0);
|
||||
showDate("dt1", dt1);
|
||||
|
||||
DateTime dt2 (2009, 1, 1, 0, 0, 0);
|
||||
showDate("dt2", dt2);
|
||||
|
||||
DateTime dt3 (2009, 1, 2, 0, 0, 0);
|
||||
showDate("dt3", dt3);
|
||||
|
||||
DateTime dt4 (2009, 1, 27, 0, 0, 0);
|
||||
showDate("dt4", dt4);
|
||||
|
||||
DateTime dt5 (2009, 2, 27, 0, 0, 0);
|
||||
showDate("dt5", dt5);
|
||||
|
||||
DateTime dt6 (2009, 12, 27, 0, 0, 0);
|
||||
showDate("dt6", dt6);
|
||||
|
||||
DateTime dt7 (dt6.unixtime() + 3600); // One hour later.
|
||||
showDate("dt7", dt7);
|
||||
|
||||
DateTime dt75 = dt6 + TimeSpan(0, 1, 0, 0); // One hour later with TimeSpan addition.
|
||||
showDate("dt7.5", dt75);
|
||||
|
||||
DateTime dt8 (dt6.unixtime() + 86400L); // One day later.
|
||||
showDate("dt8", dt8);
|
||||
|
||||
DateTime dt85 = dt6 + TimeSpan(1, 0, 0, 0); // One day later with TimeSpan addition.
|
||||
showDate("dt8.5", dt85);
|
||||
|
||||
DateTime dt9 (dt6.unixtime() + 7 * 86400L); // One week later.
|
||||
showDate("dt9", dt9);
|
||||
|
||||
DateTime dt95 = dt6 + TimeSpan(7, 0, 0, 0); // One week later with TimeSpan addition.
|
||||
showDate("dt9.5", dt95);
|
||||
|
||||
DateTime dt10 = dt6 + TimeSpan(0, 0, 42, 42); // Fourty two minutes and fourty two seconds later.
|
||||
showDate("dt10", dt10);
|
||||
|
||||
DateTime dt11 = dt6 - TimeSpan(7, 0, 0, 0); // One week ago.
|
||||
showDate("dt11", dt11);
|
||||
|
||||
TimeSpan ts1 = dt6 - dt5;
|
||||
showTimeSpan("dt6-dt5", ts1);
|
||||
|
||||
TimeSpan ts2 = dt10 - dt6;
|
||||
showTimeSpan("dt10-dt6", ts2);
|
||||
}
|
||||
|
||||
void loop () {
|
||||
}
|
||||
68
libraries/RTClib/examples/ds1307/ds1307.ino
Normal file
68
libraries/RTClib/examples/ds1307/ds1307.ino
Normal file
@@ -0,0 +1,68 @@
|
||||
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
|
||||
|
||||
#include <Wire.h>
|
||||
#include "RTClib.h"
|
||||
|
||||
RTC_DS1307 rtc;
|
||||
|
||||
void setup () {
|
||||
Serial.begin(57600);
|
||||
#ifdef AVR
|
||||
Wire.begin();
|
||||
#else
|
||||
Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
|
||||
#endif
|
||||
rtc.begin();
|
||||
|
||||
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(F(__DATE__), F(__TIME__)));
|
||||
// This line sets the RTC with an explicit date & time, for example to set
|
||||
// January 21, 2014 at 3am you would call:
|
||||
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
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.print(future.year(), DEC);
|
||||
Serial.print('/');
|
||||
Serial.print(future.month(), DEC);
|
||||
Serial.print('/');
|
||||
Serial.print(future.day(), DEC);
|
||||
Serial.print(' ');
|
||||
Serial.print(future.hour(), DEC);
|
||||
Serial.print(':');
|
||||
Serial.print(future.minute(), DEC);
|
||||
Serial.print(':');
|
||||
Serial.print(future.second(), DEC);
|
||||
Serial.println();
|
||||
|
||||
Serial.println();
|
||||
delay(3000);
|
||||
}
|
||||
56
libraries/RTClib/examples/ds1307SqwPin/ds1307SqwPin.ino
Normal file
56
libraries/RTClib/examples/ds1307SqwPin/ds1307SqwPin.ino
Normal file
@@ -0,0 +1,56 @@
|
||||
// SQW/OUT pin mode using a DS1307 RTC connected via I2C.
|
||||
//
|
||||
// According to the data sheet (http://datasheets.maxim-ic.com/en/ds/DS1307.pdf), the
|
||||
// DS1307's SQW/OUT pin can be set to low, high, 1Hz, 4.096kHz, 8.192kHz, or 32.768kHz.
|
||||
//
|
||||
// This sketch reads the state of the pin, then iterates through the possible values at
|
||||
// 5 second intervals.
|
||||
//
|
||||
|
||||
// NOTE:
|
||||
// You must connect a pull up resistor (~10kohm) from the SQW pin up to VCC. Without
|
||||
// this pull up the wave output will not work!
|
||||
|
||||
#include <Wire.h>
|
||||
#include "RTClib.h"
|
||||
|
||||
RTC_DS1307 rtc;
|
||||
|
||||
int mode_index = 0;
|
||||
|
||||
Ds1307SqwPinMode modes[] = {OFF, ON, SquareWave1HZ, SquareWave4kHz, SquareWave8kHz, SquareWave32kHz};
|
||||
|
||||
|
||||
void print_mode() {
|
||||
Ds1307SqwPinMode mode = rtc.readSqwPinMode();
|
||||
|
||||
Serial.print("Sqw Pin Mode: ");
|
||||
switch(mode) {
|
||||
case OFF: Serial.println("OFF"); break;
|
||||
case ON: Serial.println("ON"); break;
|
||||
case SquareWave1HZ: Serial.println("1Hz"); break;
|
||||
case SquareWave4kHz: Serial.println("4.096kHz"); break;
|
||||
case SquareWave8kHz: Serial.println("8.192kHz"); break;
|
||||
case SquareWave32kHz: Serial.println("32.768kHz"); break;
|
||||
default: Serial.println("UNKNOWN"); break;
|
||||
}
|
||||
}
|
||||
|
||||
void setup () {
|
||||
Serial.begin(57600);
|
||||
Wire.begin();
|
||||
rtc.begin();
|
||||
|
||||
print_mode();
|
||||
}
|
||||
|
||||
void loop () {
|
||||
rtc.writeSqwPinMode(modes[mode_index++]);
|
||||
print_mode();
|
||||
|
||||
if (mode_index > 5) {
|
||||
mode_index = 0;
|
||||
}
|
||||
|
||||
delay(5000);
|
||||
}
|
||||
59
libraries/RTClib/examples/ds1307nvram/ds1307nvram.ino
Normal file
59
libraries/RTClib/examples/ds1307nvram/ds1307nvram.ino
Normal file
@@ -0,0 +1,59 @@
|
||||
// Example of using the non-volatile RAM storage on the DS1307.
|
||||
// You can write up to 56 bytes from address 0 to 55.
|
||||
// Data will be persisted as long as the DS1307 has battery power.
|
||||
|
||||
#include <Wire.h>
|
||||
#include "RTClib.h"
|
||||
|
||||
RTC_DS1307 rtc;
|
||||
|
||||
void printnvram(uint8_t address) {
|
||||
Serial.print("Address 0x");
|
||||
Serial.print(address, HEX);
|
||||
Serial.print(" = 0x");
|
||||
Serial.println(rtc.readnvram(address), HEX);
|
||||
}
|
||||
|
||||
void setup () {
|
||||
Serial.begin(57600);
|
||||
#ifdef AVR
|
||||
Wire.begin();
|
||||
#else
|
||||
Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
|
||||
#endif
|
||||
rtc.begin();
|
||||
|
||||
// Print old RAM contents on startup.
|
||||
Serial.println("Current NVRAM values:");
|
||||
for (int i = 0; i < 6; ++i) {
|
||||
printnvram(i);
|
||||
}
|
||||
|
||||
// Write some bytes to non-volatile RAM storage.
|
||||
// NOTE: You can only read and write from addresses 0 to 55 (i.e. 56 byte values).
|
||||
Serial.println("Writing NVRAM values.");
|
||||
// Example writing one byte at a time:
|
||||
rtc.writenvram(0, 0xFE);
|
||||
rtc.writenvram(1, 0xED);
|
||||
// Example writing multiple bytes:
|
||||
uint8_t writeData[4] = { 0xBE, 0xEF, 0x01, 0x02 };
|
||||
rtc.writenvram(2, writeData, 4);
|
||||
|
||||
// Read bytes from non-volatile RAM storage.
|
||||
Serial.println("Reading NVRAM values:");
|
||||
// Example reading one byte at a time.
|
||||
Serial.println(rtc.readnvram(0), HEX);
|
||||
Serial.println(rtc.readnvram(1), HEX);
|
||||
// Example reading multiple bytes:
|
||||
uint8_t readData[4] = {0};
|
||||
rtc.readnvram(readData, 4, 2);
|
||||
Serial.println(readData[0], HEX);
|
||||
Serial.println(readData[1], HEX);
|
||||
Serial.println(readData[2], HEX);
|
||||
Serial.println(readData[3], HEX);
|
||||
|
||||
}
|
||||
|
||||
void loop () {
|
||||
// Do nothing in the loop.
|
||||
}
|
||||
55
libraries/RTClib/examples/softrtc/softrtc.ino
Normal file
55
libraries/RTClib/examples/softrtc/softrtc.ino
Normal file
@@ -0,0 +1,55 @@
|
||||
// Date and time functions using just software, based on millis() & timer
|
||||
|
||||
#include <Wire.h>
|
||||
#include "RTClib.h"
|
||||
|
||||
RTC_Millis rtc;
|
||||
|
||||
void setup () {
|
||||
Serial.begin(57600);
|
||||
// following line sets the RTC to the date & time this sketch was compiled
|
||||
rtc.begin(DateTime(F(__DATE__), F(__TIME__)));
|
||||
// This line sets the RTC with an explicit date & time, for example to set
|
||||
// January 21, 2014 at 3am you would call:
|
||||
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
Serial.print(" seconds since 1970: ");
|
||||
Serial.println(now.unixtime());
|
||||
|
||||
// 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.print(future.year(), DEC);
|
||||
Serial.print('/');
|
||||
Serial.print(future.month(), DEC);
|
||||
Serial.print('/');
|
||||
Serial.print(future.day(), DEC);
|
||||
Serial.print(' ');
|
||||
Serial.print(future.hour(), DEC);
|
||||
Serial.print(':');
|
||||
Serial.print(future.minute(), DEC);
|
||||
Serial.print(':');
|
||||
Serial.print(future.second(), DEC);
|
||||
Serial.println();
|
||||
|
||||
Serial.println();
|
||||
delay(3000);
|
||||
}
|
||||
@@ -9,6 +9,7 @@
|
||||
DateTime KEYWORD1
|
||||
RTC_DS1307 KEYWORD1
|
||||
RTC_Millis KEYWORD1
|
||||
Ds1307SqwPinMode KEYWORD1
|
||||
|
||||
#######################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
@@ -27,6 +28,8 @@ begin KEYWORD2
|
||||
adjust KEYWORD2
|
||||
isrunning KEYWORD2
|
||||
now KEYWORD2
|
||||
readSqwPinMode KEYWORD2
|
||||
writeSqwPinMode KEYWORD2
|
||||
|
||||
#######################################
|
||||
# Constants (LITERAL1)
|
||||
|
||||
Reference in New Issue
Block a user