Skip to content
Browse files

now with zip file

  • Loading branch information...
1 parent f8a1ab0 commit 1d04f0fdedc365d164bb11fae6cbf8b653bca8eb @ladyada ladyada committed May 14, 2010
Showing with 57 additions and 30 deletions.
  1. +40 −21 RTClib.cpp
  2. +8 −3 RTClib.h
  3. BIN RTClib.zip
  4. +9 −6 examples/ds1307/ds1307.pde
View
61 RTClib.cpp
@@ -1,3 +1,6 @@
+// Code by JeeLabs http://news.jeelabs.org/code/
+// Released to the public domain! Enjoy!
+
#include <Wire.h>
#include <avr/pgmspace.h>
#include "RTClib.h"
@@ -6,6 +9,8 @@
#define DS1307_ADDRESS 0x68
#define SECONDS_PER_DAY 86400L
+#define SECONDS_FROM_1970_TO_2000 946684800
+
////////////////////////////////////////////////////////////////////////////////
// utility code, some of this could be exposed in the DateTime API if needed
@@ -31,7 +36,9 @@ static long time2long(uint16_t days, uint8_t h, uint8_t m, uint8_t s) {
// DateTime implementation - ignores time zones and DST changes
// NOTE: also ignores leap seconds, see http://en.wikipedia.org/wiki/Leap_second
-DateTime::DateTime (long t) {
+DateTime::DateTime (uint32_t t) {
+ t -= SECONDS_FROM_1970_TO_2000; // bring to 2000 timestamp from 1970
+
ss = t % 60;
t /= 60;
mm = t % 60;
@@ -98,13 +105,17 @@ DateTime::DateTime (const char* date, const char* time) {
}
uint8_t DateTime::dayOfWeek() const {
- uint16_t day = get() / SECONDS_PER_DAY;
+ uint16_t day = secondstime() / SECONDS_PER_DAY;
return (day + 6) % 7; // Jan 1, 2000 is a Saturday, i.e. returns 6
}
-long DateTime::get() const {
- uint16_t days = date2days(yOff, m, d);
- return time2long(days, hh, mm, ss);
+uint32_t DateTime::unixtime(void) const {
+ uint32_t t;
+ uint16_t days = date2days(yOff, m, d);
+ t = time2long(days, hh, mm, ss);
+ t += SECONDS_FROM_1970_TO_2000; // seconds from 1970 to 2000
+
+ return t;
}
////////////////////////////////////////////////////////////////////////////////
@@ -115,8 +126,16 @@ static uint8_t bin2bcd (uint8_t val) { return val + 6 * (val / 10); }
uint8_t RTC_DS1307::begin(void) {
return 1;
+}
+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) {
@@ -134,20 +153,20 @@ void RTC_DS1307::adjust(const DateTime& dt) {
}
DateTime RTC_DS1307::now() {
- Wire.beginTransmission(DS1307_ADDRESS);
- Wire.send(0);
- Wire.endTransmission();
-
- Wire.requestFrom(DS1307_ADDRESS, 7);
- uint8_t ss = bcd2bin(Wire.receive());
- 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.beginTransmission(DS1307_ADDRESS);
+ Wire.send(0);
+ 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);
}
////////////////////////////////////////////////////////////////////////////////
@@ -156,11 +175,11 @@ DateTime RTC_DS1307::now() {
long RTC_Millis::offset = 0;
void RTC_Millis::adjust(const DateTime& dt) {
- offset = dt.get() - millis() / 1000;
+ offset = dt.secondstime() - millis() / 1000;
}
DateTime RTC_Millis::now() {
- return offset + millis() / 1000;
+ return (uint32_t)(offset + millis() / 1000);
}
////////////////////////////////////////////////////////////////////////////////
View
11 RTClib.h
@@ -1,11 +1,13 @@
+// Code by JeeLabs http://news.jeelabs.org/code/
+// Released to the public domain! Enjoy!
+
// Simple general-purpose date/time class (no TZ / DST / leap second handling!)
class DateTime {
public:
- DateTime (long t =0);
+ 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 char* date, const char* time);
-
uint16_t year() const { return 2000 + yOff; }
uint8_t month() const { return m; }
uint8_t day() const { return d; }
@@ -15,7 +17,9 @@ class DateTime {
uint8_t dayOfWeek() const;
// 32-bit times as seconds since 1/1/2000
- long get() const;
+ long secondstime() const;
+ // 32-bit times as seconds since 1/1/1970
+ uint32_t unixtime(void) const;
protected:
uint8_t yOff, m, d, hh, mm, ss;
@@ -26,6 +30,7 @@ class RTC_DS1307 {
public:
static uint8_t begin(void);
static void adjust(const DateTime& dt);
+ uint8_t isrunning(void);
static DateTime now();
};
View
BIN RTClib.zip
Binary file not shown.
View
15 examples/ds1307/ds1307.pde
@@ -9,9 +9,12 @@ void setup () {
Serial.begin(57600);
Wire.begin();
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(__DATE__, __TIME__));
+ RTC.adjust(DateTime(__DATE__, __TIME__));
+ }
}
void loop () {
@@ -30,14 +33,14 @@ void loop () {
Serial.print(now.second(), DEC);
Serial.println();
- Serial.print(" since 2000 = ");
- Serial.print(now.get());
+ Serial.print(" since midnight 1/1/1970 = ");
+ Serial.print(now.unixtime());
Serial.print("s = ");
- Serial.print(now.get() / 86400L);
+ Serial.print(now.unixtime() / 86400L);
Serial.println("d");
// calculate a date which is 7 days and 30 seconds into the future
- DateTime future (now.get() + 7 * 86400L + 30);
+ DateTime future (now.unixtime() + 7 * 86400L + 30);
Serial.print(" now + 7d + 30s: ");
Serial.print(future.year(), DEC);

0 comments on commit 1d04f0f

Please sign in to comment.
Something went wrong with that request. Please try again.