Skip to content

Commit

Permalink
Merge pull request adafruit#5 from zevenwolf/master
Browse files Browse the repository at this point in the history
Did the changes that you asked for. Works with 22 and 1.0
  • Loading branch information
ladyada committed Nov 5, 2011
2 parents 72fc1a4 + 4ce3c8b commit 8e266db
Showing 1 changed file with 66 additions and 5 deletions.
71 changes: 66 additions & 5 deletions RTClib.cpp
Expand Up @@ -4,13 +4,22 @@
#include <Wire.h> #include <Wire.h>
#include <avr/pgmspace.h> #include <avr/pgmspace.h>
#include "RTClib.h" #include "RTClib.h"
#include <WProgram.h>


#define DS1307_ADDRESS 0x68 #define DS1307_ADDRESS 0x68
#define SECONDS_PER_DAY 86400L #define SECONDS_PER_DAY 86400L


#define SECONDS_FROM_1970_TO_2000 946684800 #define SECONDS_FROM_1970_TO_2000 946684800


#if ARDUINO > 22
#include <arduino.h>
#endif

#if ARDUINO <= 22
#include <WProgram.h>
#endif


int i = 0; //The new wire library needs to take an int when you are sending for the zero register
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// utility code, some of this could be exposed in the DateTime API if needed // utility code, some of this could be exposed in the DateTime API if needed


Expand Down Expand Up @@ -128,9 +137,58 @@ uint8_t RTC_DS1307::begin(void) {
return 1; return 1;
} }



#if ARDUINO > 22


uint8_t RTC_DS1307::isrunning(void) {
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(i);
Wire.endTransmission();

Wire.requestFrom(DS1307_ADDRESS, 1);
uint8_t ss = Wire.read();
return !(ss>>7);
}

void RTC_DS1307::adjust(const DateTime& dt) {
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(i);
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(i);
Wire.endTransmission();
}

DateTime RTC_DS1307::now() {
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(i);
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;

return DateTime (y, m, d, hh, mm, ss);
}

#endif

#if ARDUINO <= 22

uint8_t RTC_DS1307::isrunning(void) { uint8_t RTC_DS1307::isrunning(void) {
Wire.beginTransmission(DS1307_ADDRESS); Wire.beginTransmission(DS1307_ADDRESS);
Wire.send(0); Wire.send(i);
Wire.endTransmission(); Wire.endTransmission();


Wire.requestFrom(DS1307_ADDRESS, 1); Wire.requestFrom(DS1307_ADDRESS, 1);
Expand All @@ -140,21 +198,21 @@ uint8_t RTC_DS1307::isrunning(void) {


void RTC_DS1307::adjust(const DateTime& dt) { void RTC_DS1307::adjust(const DateTime& dt) {
Wire.beginTransmission(DS1307_ADDRESS); Wire.beginTransmission(DS1307_ADDRESS);
Wire.send(0); Wire.send(i);
Wire.send(bin2bcd(dt.second())); Wire.send(bin2bcd(dt.second()));
Wire.send(bin2bcd(dt.minute())); Wire.send(bin2bcd(dt.minute()));
Wire.send(bin2bcd(dt.hour())); Wire.send(bin2bcd(dt.hour()));
Wire.send(bin2bcd(0)); Wire.send(bin2bcd(0));
Wire.send(bin2bcd(dt.day())); Wire.send(bin2bcd(dt.day()));
Wire.send(bin2bcd(dt.month())); Wire.send(bin2bcd(dt.month()));
Wire.send(bin2bcd(dt.year() - 2000)); Wire.send(bin2bcd(dt.year() - 2000));
Wire.send(0); Wire.send(i);
Wire.endTransmission(); Wire.endTransmission();
} }


DateTime RTC_DS1307::now() { DateTime RTC_DS1307::now() {
Wire.beginTransmission(DS1307_ADDRESS); Wire.beginTransmission(DS1307_ADDRESS);
Wire.send(0); Wire.send(i);
Wire.endTransmission(); Wire.endTransmission();


Wire.requestFrom(DS1307_ADDRESS, 7); Wire.requestFrom(DS1307_ADDRESS, 7);
Expand All @@ -169,6 +227,9 @@ DateTime RTC_DS1307::now() {
return DateTime (y, m, d, hh, mm, ss); return DateTime (y, m, d, hh, mm, ss);
} }


#endif


//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// RTC_Millis implementation // RTC_Millis implementation


Expand Down

0 comments on commit 8e266db

Please sign in to comment.