DS1338 RTC library RTC + 55 bytes nvram

This library let you access a DS1338, which includes a RTC and some nvram.

Most of the mbed chip already have a good rtc, but they can't be powered by a separate backup battery.

With a DS1338, plus a small lithium 3V battery, you will keep trace of time.

You will also have 55 bytes (DS1338 says 56 bytes, but I use one of them for the library) of NVRAM backed up by the battery, to store fast changing data (which would tear down an eeprom), and keep it, even in the case of a watchdog reset.

Demo code

#include "mbed.h"
#include "fr_time.h"
#include "ds1338.h"

int main() {
    struct tm time;
    int count;

    pc=new RawSerial(USBTX, USBRX);
    pc->baud(115200);
    pc->printf("DS1338 Demo\r\n");
    DS1338 ds1338(P0_19,P0_20);
#ifdef INIT_TIME
    count=0;
    time.tm_sec=0;
    time.tm_min=0;
    time.tm_hour=8;
    time.tm_mday=4;
    time.tm_mon=2;
    time.tm_year=115;
    time.tm_wday=1;
    time.tm_yday=0;
    time.tm_isdst=0;
    ds1338.writeTime(&time);
#endif


    while (true) {
        wait(0.5);
        ds1338.readTime(&time);
        ds1338.read(0,4,(char *)(&count));
        count++;
        ds1338.write(0,4,(char *)(&count));
        pc->printf("loop %d at %s\r\n",count,asctime(&time));
    }
}
Committer:
scachat
Date:
Wed Mar 04 13:09:54 2015 +0000
Revision:
1:e77d69913c46
Parent:
0:0ffb7046206a
bug in month handling

Who changed what in which revision?

UserRevisionLine numberNew contents of line
scachat 0:0ffb7046206a 1 #ifndef __EEPROM__H_
scachat 0:0ffb7046206a 2 #define __EEPROM__H_
scachat 0:0ffb7046206a 3
scachat 0:0ffb7046206a 4 #include "mbed.h"
scachat 0:0ffb7046206a 5
scachat 0:0ffb7046206a 6 #define DS1338_ADR 0xd0
scachat 0:0ffb7046206a 7 #define DS1338_BUFFER_SIZE 10
scachat 0:0ffb7046206a 8
scachat 0:0ffb7046206a 9
scachat 0:0ffb7046206a 10 /**
scachat 0:0ffb7046206a 11 * class to use a DS1338 rtc
scachat 0:0ffb7046206a 12 */
scachat 0:0ffb7046206a 13 class DS1338 {
scachat 0:0ffb7046206a 14 public:
scachat 0:0ffb7046206a 15 /*
scachat 0:0ffb7046206a 16 * Constructor, initialize the ds1338 on i2c interface.
scachat 0:0ffb7046206a 17 * @param sda : sda i2c pin (PinName)
scachat 0:0ffb7046206a 18 * @param scl : scl i2c pin (PinName)
scachat 0:0ffb7046206a 19 */
scachat 0:0ffb7046206a 20 DS1338(PinName sda, PinName scl);
scachat 0:0ffb7046206a 21 /**
scachat 0:0ffb7046206a 22 * read bytes
scachat 0:0ffb7046206a 23 * @param adr the start address
scachat 0:0ffb7046206a 24 * @param count number of byte to read
scachat 0:0ffb7046206a 25 * @param data where to put the bytes
scachat 0:0ffb7046206a 26 * @return the byte
scachat 0:0ffb7046206a 27 */
scachat 0:0ffb7046206a 28 void read(unsigned char adr,unsigned char count,char * data);
scachat 0:0ffb7046206a 29 /**
scachat 0:0ffb7046206a 30 * write bytes
scachat 0:0ffb7046206a 31 * @param adr the start address
scachat 0:0ffb7046206a 32 * @param count number of byte to write
scachat 0:0ffb7046206a 33 * @param data to be written
scachat 0:0ffb7046206a 34 * @return the byte
scachat 0:0ffb7046206a 35 */
scachat 0:0ffb7046206a 36 void write(unsigned char adr,unsigned char count,char * data);
scachat 0:0ffb7046206a 37 /**
scachat 0:0ffb7046206a 38 * read the current time
scachat 0:0ffb7046206a 39 * @param x the time;
scachat 0:0ffb7046206a 40 */
scachat 0:0ffb7046206a 41 void readTime(tm * x);
scachat 0:0ffb7046206a 42 /**
scachat 0:0ffb7046206a 43 * read the current time
scachat 0:0ffb7046206a 44 * @param x the time;
scachat 0:0ffb7046206a 45 */
scachat 0:0ffb7046206a 46 void writeTime(tm * x);
scachat 0:0ffb7046206a 47 private:
scachat 0:0ffb7046206a 48 I2C _i2c;
scachat 0:0ffb7046206a 49 char buffer[DS1338_BUFFER_SIZE];
scachat 0:0ffb7046206a 50 };
scachat 0:0ffb7046206a 51 #endif