Skip to content
Browse files

Incorporated RAM memory Read/Write from dmalec

  • Loading branch information...
1 parent 5d9bc27 commit ecbe1eda184272757fbb0f546ab212a68ed6e767 @MrAlvin committed
Showing with 123 additions and 4 deletions.
  1. +35 −0 RTClib.cpp
  2. +7 −4 RTClib.h
  3. +81 −0 examples/ds1307Memory/ds1307Memory.pde
View
35 RTClib.cpp
@@ -20,6 +20,8 @@
#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
@@ -225,6 +227,39 @@ DateTime RTC_DS1307::now() {
return DateTime (y, m, d, hh, mm, ss);
}
+
+uint8_t RTC_DS1307::readMemory(uint8_t offset, uint8_t* data, uint8_t length) {
+ uint8_t bytes_read = 0;
+
+ Wire.beginTransmission(DS1307_ADDRESS);
+ WW_(0x08 + offset);
+ Wire.endTransmission();
+
+ Wire.requestFrom((uint8_t)DS1307_ADDRESS, (uint8_t)length);
+ while (Wire.available() > 0 && bytes_read < length) {
+ data[bytes_read] = WR_();
+ bytes_read++;
+ }
+
+ return bytes_read;
+}
+
+uint8_t RTC_DS1307::writeMemory(uint8_t offset, uint8_t* data, uint8_t length) {
+ uint8_t bytes_written;
+
+ Wire.beginTransmission(DS1307_ADDRESS);
+ WW_(0x08 + offset);
+#if (ARDUINO >= 100)
+ bytes_written = WW_(data, length);
+ Wire.endTransmission();
+ return bytes_written;
+#else
+ WW_(data, length);
+ Wire.endTransmission();
+ return length;
+#endif
+}
+
////////////////////////////////////////////////////////////////////////////////
// RTC_DS3231 implementation
View
11 RTClib.h
@@ -1,11 +1,12 @@
// Code by JeeLabs http://news.jeelabs.org/code/
// Released to the public domain! Enjoy!
+
+// Merged read/write RAM memory functions from: github.com/dmalec/RTClib by MrAlvin 2012-02-27
+// Merged DS3231 & DS3234 functions from: github/coobro/RTClib by MrAlvin 2012-02-27
-// Additions by MrAlvin.
-// Merged text from: github/coobro/RTClib
-// Alarm code for DS3231 (Chronodot) heavily used/modified from Eric Ayars DS3231 library
-// His code is located at: http://hacks.ayars.org/2011/04/ds3231-real-time-clock.html
+// Alarm code for DS3231 (Chronodot) heavily used/modified from Eric Ayars DS3231 library by Coobro
+// Eric Ayars code is located at: http://hacks.ayars.org/2011/04/ds3231-real-time-clock.html
#ifndef __RTCLIB_H__
#define __RTCLIB_H__
@@ -50,6 +51,8 @@ class RTC_DS1307 {
static void adjust(const DateTime& dt);
uint8_t isrunning(void);
static DateTime now();
+ static uint8_t readMemory(uint8_t offset, uint8_t* data, uint8_t length);
+ static uint8_t writeMemory(uint8_t offset, uint8_t* data, uint8_t length);
};
View
81 examples/ds1307Memory/ds1307Memory.pde
@@ -0,0 +1,81 @@
+// Battery backed memory functions using a DS1307 RTC connected via I2C.
+//
+// According to the data sheet (http://datasheets.maxim-ic.com/en/ds/DS1307.pdf), the
+// DS1307 has 56 bytes of general purpose RAM with unlimited writes. The RAM registers
+// are located at 0x08 - 0x3F. During read/write, if the data would go past 0x3F, it
+// will wrap around to 0x00 and read/write to the clock registers.
+//
+// The readMemory and writeMemory functions assume the caller is intending to write into
+// the RAM above the clock registers and so 8 is added to the passed in offset.
+//
+// This sketch writes a character array and a long into the memory of the DS1307 during
+// setup and then reads them back on each iteration of the loop. The sketch demonstrates
+// how the RAM could be used to store the local UTC offset once and then used on each read
+// to adjust the local results of calling unixtime to UTC unixtime.
+//
+// To see the battery backed feature in action:
+// 1. Upload the sketch as-is
+// 2. Comment out the section in setup that writes to memory and upload the sketch again
+// 3. You can now disconnect power, reconnect power and see the values being read and printed
+
+#include <Wire.h>
+#include <SPI.h> // not used here, but needed to prevent a RTClib compile error
+#include "RTClib.h"
+
+RTC_DS1307 RTC;
+
+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__));
+ }
+
+ // Write a char array
+ char message[12] = "Hello World";
+ RTC.writeMemory(0, (uint8_t *)message, 11);
+ Serial.print("storing message to memory : ");
+ Serial.println(message);
+
+ // Write a long
+ uint8_t buffer[4];
+ long utc_offset = (-5 * 60 * 60); // UTC-5
+ buffer[0] = (utc_offset >> 24) & 0x000000FF;
+ buffer[1] = (utc_offset >> 16) & 0x000000FF;
+ buffer[2] = (utc_offset >> 8) & 0x000000FF;
+ buffer[3] = utc_offset & 0x000000FF;
+ RTC.writeMemory(12, buffer, 4);
+ Serial.print("storing long (UTC offset) to memory : ");
+ Serial.println(utc_offset, DEC);
+}
+
+void loop () {
+ // Read a char array
+ char message[12];
+ RTC.readMemory(0x00, (uint8_t *)message, 11);
+ message[11] = '\0';
+ Serial.print("reading message from memory : ");
+ Serial.println(message);
+
+ // Read a long
+ uint8_t buffer[4];
+ RTC.readMemory(12, buffer, 4);
+ long utc_offset = ((long)buffer[0] << 24) + ((long)buffer[1] << 16) + ((long)buffer[2] << 8) + ((long)buffer[3]);
+ Serial.print("reading long (UTC offset) from memory : ");
+ Serial.println(utc_offset, DEC);
+
+ // Print the unaltered unixtime
+ DateTime now = RTC.now();
+ Serial.print("time since midnight 1/1/1970 = ");
+ Serial.println(now.unixtime());
+
+ // Print the unixtime less the UTC offset
+ Serial.print("time since midnight 1/1/1970 (adjusted to UTC) = ");
+ Serial.println(now.unixtime() - utc_offset);
+
+ delay(3000);
+}

0 comments on commit ecbe1ed

Please sign in to comment.
Something went wrong with that request. Please try again.