summaryrefslogtreecommitdiff
path: root/DS1307RTC
diff options
context:
space:
mode:
authorMike Beattie <mike@ethernal.org>2012-03-20 23:25:17 +1300
committerMike Beattie <mike@ethernal.org>2012-03-20 23:25:17 +1300
commitcb7bd533cb2b505441ca9a35c9897db358a30b47 (patch)
treefd20d304a519730798433e9835ce3e8e12f703fc /DS1307RTC
Add Time library
Signed-off-by: Mike Beattie <mike@ethernal.org>
Diffstat (limited to 'DS1307RTC')
-rw-r--r--DS1307RTC/DS1307RTC.cpp124
-rw-r--r--DS1307RTC/DS1307RTC.h31
-rw-r--r--DS1307RTC/keywords.txt22
-rw-r--r--DS1307RTC/readme.txt7
4 files changed, 184 insertions, 0 deletions
diff --git a/DS1307RTC/DS1307RTC.cpp b/DS1307RTC/DS1307RTC.cpp
new file mode 100644
index 0000000..bf0b46e
--- /dev/null
+++ b/DS1307RTC/DS1307RTC.cpp
@@ -0,0 +1,124 @@
+/*
+ * DS1307RTC.h - 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
+
+ 30 Dec 2009 - Initial release
+ 5 Sep 2011 updated for Arduino 1.0
+ */
+
+#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);
+#if ARDUINO >= 100
+ Wire.write((uint8_t)0x00);
+#else
+ Wire.send(0x00);
+#endif
+ Wire.endTransmission();
+
+ // request the 7 data fields (secs, min, hr, dow, date, mth, yr)
+ Wire.requestFrom(DS1307_CTRL_ID, tmNbrFields);
+#if ARDUINO >= 100
+ tm.Second = bcd2dec(Wire.read() & 0x7f);
+ tm.Minute = bcd2dec(Wire.read() );
+ tm.Hour = bcd2dec(Wire.read() & 0x3f); // mask assumes 24hr clock
+ tm.Wday = bcd2dec(Wire.read() );
+ tm.Day = bcd2dec(Wire.read() );
+ tm.Month = bcd2dec(Wire.read() );
+ tm.Year = y2kYearToTm((bcd2dec(Wire.read())));
+#else
+ 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())));
+#endif
+}
+
+void DS1307RTC::write(tmElements_t &tm)
+{
+ Wire.beginTransmission(DS1307_CTRL_ID);
+#if ARDUINO >= 100
+ Wire.write((uint8_t)0x00); // reset register pointer
+ Wire.write(dec2bcd(tm.Second)) ;
+ Wire.write(dec2bcd(tm.Minute));
+ Wire.write(dec2bcd(tm.Hour)); // sets 24 hour format
+ Wire.write(dec2bcd(tm.Wday));
+ Wire.write(dec2bcd(tm.Day));
+ Wire.write(dec2bcd(tm.Month));
+ Wire.write(dec2bcd(tmYearToY2k(tm.Year)));
+#else
+ 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)));
+#endif
+ 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
+
diff --git a/DS1307RTC/DS1307RTC.h b/DS1307RTC/DS1307RTC.h
new file mode 100644
index 0000000..e3a1bc1
--- /dev/null
+++ b/DS1307RTC/DS1307RTC.h
@@ -0,0 +1,31 @@
+/*
+ * DS1307RTC.h - library for DS1307 RTC
+ * This library is intended to be uses with Arduino Time.h library functions
+ */
+
+#ifndef DS1307RTC_h
+#define DS1307RTC_h
+
+#include <Time.h>
+
+// library interface description
+class DS1307RTC
+{
+ // user-accessible "public" interface
+ public:
+ DS1307RTC();
+ static time_t get();
+ static void set(time_t t);
+ static void read(tmElements_t &tm);
+ static void write(tmElements_t &tm);
+
+ private:
+ static uint8_t dec2bcd(uint8_t num);
+ static uint8_t bcd2dec(uint8_t num);
+};
+
+extern DS1307RTC RTC;
+
+#endif
+
+
diff --git a/DS1307RTC/keywords.txt b/DS1307RTC/keywords.txt
new file mode 100644
index 0000000..be96f1e
--- /dev/null
+++ b/DS1307RTC/keywords.txt
@@ -0,0 +1,22 @@
+#######################################
+# Syntax Coloring Map For DS1307RTC
+#######################################
+
+#######################################
+# Datatypes (KEYWORD1)
+#######################################
+
+#######################################
+# Methods and Functions (KEYWORD2)
+#######################################
+get KEYWORD2
+set KEYWORD2
+read KEYWORD2
+write KEYWORD2
+#######################################
+# Instances (KEYWORD2)
+#######################################
+RTC
+#######################################
+# Constants (LITERAL1)
+#######################################
diff --git a/DS1307RTC/readme.txt b/DS1307RTC/readme.txt
new file mode 100644
index 0000000..5978295
--- /dev/null
+++ b/DS1307RTC/readme.txt
@@ -0,0 +1,7 @@
+Readme file for DS1307RTC Library
+
+The DS1307RTC library is provided to demonstrate the Arduino Time library.
+
+See the TimeRTC example sketches privided with the Time library download for usage
+
+