This is a port of the Adafruit driver and DateTime library for the DS1307 RTC controller

Dependents:   espresso-machine-control SAT_OLED Mbedesclava Mbedmaestra ... more

Import libraryAdafruit_RTCLib

This is a port of the Adafruit driver and DateTime library for the DS1307 RTC controller

This library was ported from Adafruit's RTCLib and then modified by splitting the out the driver separate from the DateTime class.

Also add support for access to the 56 bytes of user accessible battery backed ram.

I did not port, but left the RTC_Millis class in, and commented it out.

DS1307 RTC Breakout

Example

/*
 *  Copyright (c) 2012 Neal Horman - http://www.wanlink.com
 *  
 *  License: MIT open source (http://opensource.org/licenses/MIT)
 *      Summary;
 *      Use / modify / distribute / publish it how you want and 
 *      if you use it, or don't, you can't hold me liable for how
 *      it does or doesn't work.
 *      If it doesn't work how you want, don't use it, or change
 *      it so that it does work.
 */

#include "mbed.h"
#include "DS1307.h"
 
class I2C2 : public I2C
{
public:
    I2C2 ( PinName sda, PinName scl, int hz, const char *name = NULL) : I2C ( sda, scl, name) { frequency(hz * 100); };
};
 
I2C2 gI2c ( p28, p27, 100 );
RtcDs1307 gRtc ( gI2c );
Serial gSerial(USBTX, USBRX);
 
void printDT(char *pre, DateTime &dt)
{
    gSerial.printf("%s - %u/%u/%02u %2u:%02u:%02u\r\n"
                ,pre
                ,dt.month(),dt.day(),dt.year()
                ,dt.hour(),dt.minute(),dt.second()
                );
}
 
bool rtcUpdate(RtcDs1307 &rtc, int32_t bias) // this must be signed
{   bool bUpdated = false;
 
    // Use the compiled date/time as a basis for setting the clock.
    // We assign it to a signed integer so that negative biases work correctly
    int64_t compiledTime = DateTime(__DATE__,__TIME__).unixtime();
 
    // This assumes that the program is run VERY soon after the initial compile.
    time_t localt = DateTime(compiledTime + bias).unixtime(); // offset by bias
    
    // If the stored static time stamp does not equal the compiled time stamp,
    // then we need to update the RTC clock and the stored time stamp
    if(*((time_t *)&rtc[0]) != localt)
    {
        // Update the RTC time as local time, not GMT/UTC
        rtc.adjust(localt);
        // Store the new compiled time statically in the object ram image
        *((time_t *)&rtc[0]) = localt;
        // Push the object ram image to the RTC ram image
        bUpdated = rtc.commit();
    }
    
    return bUpdated;
}
 
int main()
{   time_t tick = 0;
 
    if(rtcUpdate(gRtc, -(5*60*60) )) // CDT
        gSerial.printf("Updated RTC to compiled time\r\n");
    gSerial.printf("compiled %s %s\r\n",__DATE__,__TIME__);
    DateTime timeFlashed(*((time_t *)&gRtc[0]));
    printDT("last flashed on",timeFlashed);
    DateTime dt = gRtc.now();
    printDT("reset",dt);
 
    gSerial.printf("rtc clock is %s\r\n", (gRtc.isRunning() ? "running" : "halted"));
 
    while(1)
    {
        if(time(NULL) >= tick)
        {
            dt = gRtc.now();
            gSerial.printf("%u/%u/%02u %2u:%02u:%02u          \r"
                ,dt.month(),dt.day(),dt.year()
                ,dt.hour(),dt.minute(),dt.second()
                );
            tick = time(NULL)+1;
        }
    }
}

Committer:
nkhorman
Date:
Wed Jul 18 01:28:21 2012 +0000
Revision:
1:2c4e81ecda67
Parent:
0:7f90c8e04249
fix / add comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nkhorman 0:7f90c8e04249 1 // Code by JeeLabs http://news.jeelabs.org/code/
nkhorman 0:7f90c8e04249 2 // Released to the public domain! Enjoy!
nkhorman 0:7f90c8e04249 3
nkhorman 0:7f90c8e04249 4 /*
nkhorman 0:7f90c8e04249 5 * Taken from https://github.com/adafruit/RTClib
nkhorman 0:7f90c8e04249 6 * and modified for LPC1768 by Neal Horman July 2012
nkhorman 1:2c4e81ecda67 7 * Also add support for access to the 56 bytes of
nkhorman 1:2c4e81ecda67 8 * user accessible battery backed ram.
nkhorman 0:7f90c8e04249 9 */
nkhorman 0:7f90c8e04249 10
nkhorman 0:7f90c8e04249 11 #include "mbed.h"
nkhorman 0:7f90c8e04249 12 #include "DS1307.h"
nkhorman 0:7f90c8e04249 13
nkhorman 0:7f90c8e04249 14 #define DS1307_ADDRESS 0xD0
nkhorman 0:7f90c8e04249 15
nkhorman 0:7f90c8e04249 16 ////////////////////////////////////////////////////////////////////////////////
nkhorman 0:7f90c8e04249 17 // RtcDs1307 implementation
nkhorman 0:7f90c8e04249 18
nkhorman 0:7f90c8e04249 19 static uint8_t bcd2bin (uint8_t val) { return val - 6 * (val >> 4); }
nkhorman 0:7f90c8e04249 20 static uint8_t bin2bcd (uint8_t val) { return val + 6 * (val / 10); }
nkhorman 0:7f90c8e04249 21
nkhorman 0:7f90c8e04249 22 RtcDs1307::RtcDs1307(I2C &i2c) : mI2c(i2c)
nkhorman 0:7f90c8e04249 23 {
nkhorman 1:2c4e81ecda67 24 // Read the RTC ram into the object RAM image
nkhorman 0:7f90c8e04249 25 mRam[0]=8;
nkhorman 0:7f90c8e04249 26 if(mI2c.write(DS1307_ADDRESS,(const char *)&mRam[0],1,true) == 0)
nkhorman 0:7f90c8e04249 27 mI2c.read(DS1307_ADDRESS,(char *)&mRam[1],sizeof(mRam)-1);
nkhorman 0:7f90c8e04249 28 else
nkhorman 0:7f90c8e04249 29 memset(mRam,0,sizeof(mRam));
nkhorman 0:7f90c8e04249 30 }
nkhorman 0:7f90c8e04249 31
nkhorman 0:7f90c8e04249 32 bool RtcDs1307::commit()
nkhorman 0:7f90c8e04249 33 {
nkhorman 0:7f90c8e04249 34 mRam[0] = 8; // device register address
nkhorman 0:7f90c8e04249 35 return (mI2c.write(DS1307_ADDRESS,(const char *)mRam,sizeof(mRam)) == 0);
nkhorman 0:7f90c8e04249 36 }
nkhorman 0:7f90c8e04249 37
nkhorman 0:7f90c8e04249 38 bool RtcDs1307::isRunning()
nkhorman 0:7f90c8e04249 39 { uint8_t i = 0;
nkhorman 0:7f90c8e04249 40
nkhorman 0:7f90c8e04249 41 return (mI2c.write(DS1307_ADDRESS,(const char *)&i,sizeof(i),true) == 0
nkhorman 0:7f90c8e04249 42 && mI2c.read(DS1307_ADDRESS,(char *)&i,sizeof(i)) == 0
nkhorman 0:7f90c8e04249 43 && (i&0x80) == 0
nkhorman 0:7f90c8e04249 44 );
nkhorman 0:7f90c8e04249 45 }
nkhorman 0:7f90c8e04249 46
nkhorman 0:7f90c8e04249 47 bool RtcDs1307::adjust(const DateTime& dt)
nkhorman 0:7f90c8e04249 48 { uint8_t buf[9] =
nkhorman 0:7f90c8e04249 49 {
nkhorman 0:7f90c8e04249 50 0 // device register address
nkhorman 0:7f90c8e04249 51 ,bin2bcd(dt.second()&0x7F) // make sure bit 7 (CH - Clock Halt) is off or the clock will be stopped
nkhorman 0:7f90c8e04249 52 ,bin2bcd(dt.minute())
nkhorman 0:7f90c8e04249 53 ,bin2bcd(dt.hour()&0x3F) // force 24h mode
nkhorman 0:7f90c8e04249 54 ,bin2bcd(0)
nkhorman 0:7f90c8e04249 55 ,bin2bcd(dt.day())
nkhorman 0:7f90c8e04249 56 ,bin2bcd(dt.month())
nkhorman 0:7f90c8e04249 57 ,bin2bcd(dt.year() - 2000)
nkhorman 0:7f90c8e04249 58 ,0 // turn off SQWO
nkhorman 0:7f90c8e04249 59 };
nkhorman 0:7f90c8e04249 60
nkhorman 0:7f90c8e04249 61 return (mI2c.write(DS1307_ADDRESS,(const char *)buf,sizeof(buf)) == 0);
nkhorman 0:7f90c8e04249 62 }
nkhorman 0:7f90c8e04249 63
nkhorman 0:7f90c8e04249 64 DateTime RtcDs1307::now()
nkhorman 0:7f90c8e04249 65 { uint8_t buf[7] = {0};
nkhorman 0:7f90c8e04249 66
nkhorman 0:7f90c8e04249 67 if(mI2c.write(DS1307_ADDRESS,(const char *)&buf[0],1,true) == 0)
nkhorman 0:7f90c8e04249 68 mI2c.read(DS1307_ADDRESS,(char *)buf,sizeof(buf));
nkhorman 0:7f90c8e04249 69
nkhorman 0:7f90c8e04249 70 return DateTime (
nkhorman 0:7f90c8e04249 71 bcd2bin(buf[6]) + 2000 // y
nkhorman 0:7f90c8e04249 72 ,bcd2bin(buf[5]) // m
nkhorman 0:7f90c8e04249 73 ,bcd2bin(buf[4]) // d
nkhorman 0:7f90c8e04249 74 ,bcd2bin(buf[2] & 0x3F) // hh - mask off 24h mode
nkhorman 0:7f90c8e04249 75 ,bcd2bin(buf[1]) // mm
nkhorman 0:7f90c8e04249 76 ,bcd2bin(buf[0] & 0x7F) // ss - mask off CH - Clock Halt
nkhorman 0:7f90c8e04249 77 );
nkhorman 0:7f90c8e04249 78 }