Skip to content

Commit

Permalink
now with zip file
Browse files Browse the repository at this point in the history
  • Loading branch information
ladyada committed May 15, 2010
1 parent f8a1ab0 commit 1d04f0f
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 30 deletions.
61 changes: 40 additions & 21 deletions RTClib.cpp
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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

Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

////////////////////////////////////////////////////////////////////////////////
Expand All @@ -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) {
Expand All @@ -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);
}

////////////////////////////////////////////////////////////////////////////////
Expand All @@ -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);
}

////////////////////////////////////////////////////////////////////////////////
11 changes: 8 additions & 3 deletions RTClib.h
Original file line number Diff line number Diff line change
@@ -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; }
Expand All @@ -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;
Expand All @@ -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();
};

Expand Down
Binary file added RTClib.zip
Binary file not shown.
15 changes: 9 additions & 6 deletions examples/ds1307/ds1307.pde
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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);
Expand Down

0 comments on commit 1d04f0f

Please sign in to comment.