RTC DS3234 library

Dependencies:   mbed

Fork of WDtester by green rosh

DS3234.cpp

Committer:
duke970
Date:
2014-12-17
Revision:
1:7c0fa2bb38df
Child:
2:c2e2b6238a69

File content as of revision 1:7c0fa2bb38df:

#include <mbed.h>
#include "DS3234.h"

/*
 The below mentioned are the bits in Read mode.
 seconds 0x80
 minuntes 0x81
 hours  0x82
 day 0x83
 date 0x84
 month 0x85
 year 0x86
*/
void DS3234_set(PinName pin, struct ts t)
{
    SPI spi(PTD6, PTD7, PTD5);
    
    
    uint8_t i, century;

    if (t.year > 2000) {
        century = 0x80;
        t.year_s = t.year - 2000;
    } else {
        century = 0;
        t.year_s = t.year - 1900;
    }

    uint8_t TimeDate[7] = { t.sec, t.min, t.hour, t.wday, t.mday, t.mon, t.year_s };
    for (i = 0; i <= 6; i++) {
        DigitalOut cs(pin);
        
        spi.write(i + 0x80);
        if (i == 5)
        spi.write(dectobcd(TimeDate[5]) + century);
        else
        spi.write(dectobcd(TimeDate[i]));
        cs.write(1);
    }
}


uint8_t dectobcd(const uint8_t val)
{
    return ((val / 10 * 16) + (val % 10));
}

uint8_t bcdtodec(const uint8_t val)
{
    return ((val / 16 * 10) + (val % 16));
}