The Stalker 2 RTC is not compatible with the DS1307 so the RTC1307 code in the Time download wont work.
Here is an updated version that autodetects the RTC type.
Save the files as RTC.cpp and RTC.h. Its used in the sketch like the old library but the name is now RTC
/*
* RTC.cpp - library for DS1307 and RX8025 RTC chips
Copyright (c) Michael Margolis 2009, 2010
This library is intended to be used with Arduino Time.h library functions
The library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 Dec 2009 - Initial release for DS1307
20 Nov 2010 - renamed to RTC and Added support for DX8025
*/
#include <WProgram.h>
#include <Wire.h>
#include "RTC.h"
//#include <HardwareSerial.h> //only needed for diagnostics
const uint8_t DS1307_ADDRESS = 0x68;
const uint8_t RX8025_ADDRESS = 0x32;
const uint8_t RX8025_CTL1 = 0xE0;
const uint8_t RX8025_CTL2 = 0xf0;
const uint8_t RX8025_24HR_MODE = 0x20; // set bit 5 for 24 hr mode
const uint8_t RX8025_NO_INTR = 0x0; // interupts off, INTA hi-Z
const uint8_t RX8025_INTR_PER_SEC = 0x4; // INTA occurs once per second
const uint8_t RX8025_INTR_PER_MIN = 0x5; // INTA occurs once per minute
const uint8_t RX8025_INTR_PER_HR = 0x6; // INTA occurs once per hour
const uint8_t RX8025_INTR_PER_MON = 0x7; // INTA occurs once per month
// PUBLIC FUNCTIONS
time_t RTCget() // static function that can be used as a callback
{
RTC.get();
}
rtcClass::rtcClass()
{
}
// returns true if selection is valid or autodetect succeeds in finding a device
bool rtcClass::begin(RTCDevice_t selection)
{
device = rtcUnknown; // default to indicate that no device is selected
Wire.begin();
if( selection == rtcUnknown)
{
// discover RTC device
if(isAddressValid( RX8025_ADDRESS))
selection = rtcRX8025;
else if(isAddressValid( DS1307_ADDRESS))
selection = rtcDS1307;
}
if(selection == rtcDS1307)
{
address = DS1307_ADDRESS;
device = rtcDS1307;
}
else if(selection == rtcRX8025)
{
address = RX8025_ADDRESS;
device = rtcRX8025;
writeRegister(RX8025_CTL1,RX8025_24HR_MODE); // RX8025 defaults to 12 hr mode so change to 24hrs
writeRegister(RX8025_CTL2,0); // reset power on reset flag in control register 2
}
//Serial.print("device = "); Serial.println(device, DEC);
//Serial.print("address = 0x"); Serial.println(address, HEX);
return (device != rtcUnknown); // returns true if a valid device selected
}
time_t rtcClass::get() // Aquire data from RTC and convert to time_t
{
tmElements_t tm;
read(tm);
return(makeTime(tm));
}
void rtcClass::set(time_t t) // set RTC to given time
{
tmElements_t tm;
breakTime(t, tm);
tm.Second |= 0x80; // stop the clock
write(tm);
tm.Second &= 0x7f; // start the clock
write(tm);
}
// Aquire data from the RTC chip in BCD format
void rtcClass::read( tmElements_t &tm)
{
Wire.beginTransmission(address);
Wire.send(0x00);
Wire.endTransmission();
// request the data fields (secs, min, hr, dow, date, mth, yr)
if( device == rtcRX8025)
{
Wire.requestFrom((int)address, 8);
Wire.receive(); // RX8025 gets an extra byte that is ignored
}
else
Wire.requestFrom((int)address, tmNbrFields);
tm.Second = bcd2dec(Wire.receive() & 0x7f);
tm.Minute = bcd2dec(Wire.receive() & 0x7f); // mask added for RX8025
tm.Hour = bcd2dec(Wire.receive() & 0x3f); // mask assumes 24hr clock
tm.Wday = bcd2dec(Wire.receive() & 0x07); // mask added for RX8025
tm.Day = bcd2dec(Wire.receive() & 0x3f); // mask added for RX8025
tm.Month = bcd2dec(Wire.receive() & 0x1f); // mask added for RX8025
tm.Year = y2kYearToTm((bcd2dec(Wire.receive())));
}
void rtcClass::write(tmElements_t &tm)
{
Wire.beginTransmission((int)address);
Wire.send(0x00); // reset register pointer
Wire.send(dec2bcd(tm.Second)) ;
Wire.send(dec2bcd(tm.Minute));
Wire.send(dec2bcd(tm.Hour)); // sets 24 hour format
Wire.send(dec2bcd(tm.Wday));
Wire.send(dec2bcd(tm.Day));
Wire.send(dec2bcd(tm.Month));
Wire.send(dec2bcd(tmYearToY2k(tm.Year)));
Wire.endTransmission();
}
// this method sets the RX8025 periodic interrupt to the given period
// the INTA pin is set low at the selected interval if the interrupt is enabled
void rtcClass::setPeriodicInterrupt( RTCIntrInterval_t interval)
{
if( device != rtcRX8025)
return; // only allow this method if using RX8025
switch(interval) {
case rtcIntrDisable : writeRegister(RX8025_CTL1, RX8025_24HR_MODE | RX8025_NO_INTR); break;
case rtcIntrEachSecond : writeRegister(RX8025_CTL1, RX8025_24HR_MODE | RX8025_INTR_PER_SEC ); break;
case rtcIntrEachMinute : writeRegister(RX8025_CTL1, RX8025_24HR_MODE | RX8025_INTR_PER_MIN ); break;
case rtcIntrEachHour : writeRegister(RX8025_CTL1, RX8025_24HR_MODE | RX8025_INTR_PER_HR ); break;
case rtcIntrEachMonth : writeRegister(RX8025_CTL1, RX8025_24HR_MODE | RX8025_INTR_PER_MON ); break;
}
}
// this method sets the INTA pin high impedence ready for next interrupt
// call this after every interrupt
void rtcClass::resetPeriodicInterrupt( )
{
if( device == rtcRX8025)
writeRegister(RX8025_CTL2, 0); // clear the control2 register
}
// PRIVATE FUNCTIONS
/// returns true if there is an i2c device at the given address
bool rtcClass::isAddressValid(uint8_t address)
{
Wire.beginTransmission(address);
Wire.send(0x00);
Wire.endTransmission();
Wire.requestFrom((int)address,1);
delay(10);
if (Wire.available() > 0 )
{
Wire.receive();
//Serial.print("check for addr "); Serial.print(address,HEX), Serial.println(" returned true");
return true;
}
//Serial.print("check for addr "); Serial.print(address,HEX), Serial.println(" returned false");
return false;
}
// sets the given register to the given value
void rtcClass::writeRegister( uint8_t reg, uint8_t value)
{
Wire.beginTransmission((int)address);
Wire.send(reg);
Wire.send(value);
Wire.endTransmission();
}
// Convert Decimal to Binary Coded Decimal (BCD)
uint8_t rtcClass::dec2bcd(uint8_t num)
{
return ((num/10 * 16) + (num % 10));
}
// Convert Binary Coded Decimal (BCD) to Decimal
uint8_t rtcClass::bcd2dec(uint8_t num)
{
return ((num/16 * 10) + (num % 16));
}
rtcClass RTC = rtcClass(); // create an instance for the user
/*
* RTC.h - library for DS1307 RTC
* This library is intended to be uses with Arduino Time.h library functions
*/
#ifndef rtcClass_h
#define rtcClass_h
#include <Time.h> // download from http://www.arduino.cc/playground/Code/Time
// the list of supported RTC devices
enum RTCDevice_t {rtcUnknown, rtcDS1307, rtcRX8025};
// the list of interrupt intervals supported by the RX8025
enum RTCIntrInterval_t {rtcIntrDisable, rtcIntrEachSecond, rtcIntrEachMinute,rtcIntrEachHour,rtcIntrEachMonth} ;
extern time_t RTCget(); // function that can be used as a sync provider callback
// library interface description
class rtcClass
{
// user-accessible "public" interface
public:
rtcClass();
bool begin(RTCDevice_t device = rtcUnknown);
//getExternalTime get; // pointer to the get time method
time_t get();
void set(time_t t);
void read(tmElements_t &tm);
void write(tmElements_t &tm);
void setPeriodicInterrupt( RTCIntrInterval_t interval); // RX8025 only, set the periodic interrupt
void resetPeriodicInterrupt(); // call after each interrupt to reset the pin.
RTCDevice_t device; // the active device
private:
uint8_t address;
bool isAddressValid(uint8_t address);
uint8_t dec2bcd(uint8_t num);
uint8_t bcd2dec(uint8_t num);
void writeRegister( uint8_t register, uint8_t value);
};
extern rtcClass RTC;
#endif