Hopefully this is the final modification to the library DS1307RTC supplied with the Time.h library found on this site.
I have added the NVSRAM access functions created by Matt Joyce. There were only minor modifications to make them compatible with the DS1307RTC lib and the Time.h lib.
These routines are based on the work developed by Matt Joyce -- so if they work give him credit -- if not blame me.
Included in this library is the Keywords, the .h file and the .cpp file
First the Keywords...
#######################################
# Syntax Coloring Map For DS1307RTC
#######################################
#######################################
# Datatypes (KEYWORD1)
#######################################
#######################################
# Methods and Functions (KEYWORD2)
#######################################
get KEYWORD2
set KEYWORD2
read KEYWORD2
write KEYWORD2
set2 KEYWORD2
set_sram_data KEYWORD2
get_sram_data KEYWORD2
get_sram_byte KEYWORD2
set_sram_byte KEYWORD2
#######################################
# Instances (KEYWORD2)
#######################################
RTC
#######################################
# Constants (LITERAL1)
#######################################
The .h file
/*
* DS1307RTC.h - library for DS1307 RTC
* This library is intended to be used with Arduino Time.h library functions
* 11 Feb 2011 Modified by D. Robinson // added the NVSRAM functions for 56 bytes starting at Location 8
* Feb 10, 2011 by D. Robinson. //set2 added
* set2 is for Square Wave Oscillator control
* Matt Joyce originally wrote the NVSRAM functions I modified the slightly
* for compatibility with the Time.h library
*/
#ifndef DS1307RTC_h
#define DS1307RTC_h
#include <Time.h>
#define DS1307_DATASTART 0x08 //added dwr feb 11, 2011
// library interface description
class DS1307RTC
{
// user-accessible "public" interface
public:
DS1307RTC();
static time_t get();
static void set(time_t t);
static void set2(time_t t, uint8_t sq); //added for sq wave dwr feb 10, 2011
static void read(tmElements_t &tm);
static void write(tmElements_t &tm);
void get_sram_data(uint8_t *);
void set_sram_data(uint8_t *);
uint8_t get_sram_byte(int);
void set_sram_byte(uint8_t, int);
// library-accessible "private" interface
private:
static uint8_t dec2bcd(uint8_t num);
static uint8_t bcd2dec(uint8_t num);
};
extern DS1307RTC RTC;
#endif
finally the .cpp file
/*
* DS1307RTC.cpp - library for DS1307 RTC
Copyright (c) Michael Margolis 2009
This library is intended to be uses 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
11 Feb 2011 Modified by D. Robinson // added the NVSRAM functions for 56 bytes starting at Location 8
10 Feb 2011 Modified by D. Robinson // added set2 function to controll square wave
setting the SQ byte as 90, 91,92 or 93 HEX gives 1Hz, 4096HZ, 8192HZ or 32768Hz at thew SQ pin
Assuming you added the required Pull up resistor (3.9K for example) to 5V
30 Dec 2009 - Initial release
*/
#include <Wire.h>
#include "DS1307RTC.h"
#define DS1307_CTRL_ID 0x68
DS1307RTC::DS1307RTC()
{
Wire.begin();
}
// PUBLIC FUNCTIONS
time_t DS1307RTC::get() // Aquire data from buffer and convert to time_t
{
tmElements_t tm;
read(tm);
return(makeTime(tm));
}
void DS1307RTC::set(time_t t)
{
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 DS1307RTC::read( tmElements_t &tm)
{
Wire.beginTransmission(DS1307_CTRL_ID);
Wire.send(0x00);
Wire.endTransmission();
// request the 7 data fields (secs, min, hr, dow, date, mth, yr)
Wire.requestFrom(DS1307_CTRL_ID, tmNbrFields);
tm.Second = bcd2dec(Wire.receive() & 0x7f);
tm.Minute = bcd2dec(Wire.receive() );
tm.Hour = bcd2dec(Wire.receive() & 0x3f); // mask assumes 24hr clock
tm.Wday = bcd2dec(Wire.receive() );
tm.Day = bcd2dec(Wire.receive() );
tm.Month = bcd2dec(Wire.receive() );
tm.Year = y2kYearToTm((bcd2dec(Wire.receive())));
tm.SQControl = (Wire.receive() );
}
void DS1307RTC::write(tmElements_t &tm)
{
Wire.beginTransmission(DS1307_CTRL_ID);
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.send(tm.SQControl);
Wire.endTransmission();
}
void DS1307RTC::set2(time_t t, uint8_t sq)
{
tmElements_t tm;
breakTime(t, tm);
tm.SQControl = sq;
tm.Second |= 0x80; // stop the clock
write(tm);
tm.Second &= 0x7f; // start the clock
write(tm);
}
void DS1307RTC::get_sram_data(uint8_t *sram_data)
{
// set the register to the sram area and read 56 bytes
Wire.beginTransmission(DS1307_CTRL_ID);
Wire.send(DS1307_DATASTART);
Wire.endTransmission();
for(int i=0;i<56;i++)
{
Wire.requestFrom(DS1307_CTRL_ID, 56);
sram_data[i]=Wire.receive();
}
}
void DS1307RTC::set_sram_data(uint8_t *sram_data)
{
// set the register to the sram area and save 56 bytes
Wire.beginTransmission(DS1307_CTRL_ID);
Wire.send(DS1307_DATASTART);
for(int i=0;i<56;i++)
{
Wire.send(sram_data[i]);
}
Wire.endTransmission();
}
uint8_t DS1307RTC::get_sram_byte(int p)
{
// set the register to a specific the sram location and read a single byte
Wire.beginTransmission(DS1307_CTRL_ID);
Wire.send(DS1307_DATASTART+p);
Wire.endTransmission();
Wire.requestFrom(DS1307_CTRL_ID, 1);
return Wire.receive();
}
void DS1307RTC::set_sram_byte(uint8_t b, int p)
{
// set the register to a specific the sram location and save a single byte
Wire.beginTransmission(DS1307_CTRL_ID);
Wire.send(DS1307_DATASTART+p);
Wire.send(b);
Wire.endTransmission();
}
// PRIVATE FUNCTIONS
// Convert Decimal to Binary Coded Decimal (BCD)
uint8_t DS1307RTC::dec2bcd(uint8_t num)
{
return ((num/10 * 16) + (num % 10));
}
// Convert Binary Coded Decimal (BCD) to Decimal
uint8_t DS1307RTC::bcd2dec(uint8_t num)
{
return ((num/16 * 10) + (num % 16));
}
DS1307RTC RTC = DS1307RTC(); // create an instance for the user
If anyone has problems just post here I will try to sort it out.