RTC DS3234 library

Dependencies:   mbed

Fork of WDtester by green rosh

Revision:
1:7c0fa2bb38df
Child:
2:c2e2b6238a69
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DS3234.cpp	Wed Dec 17 08:42:51 2014 +0000
@@ -0,0 +1,52 @@
+#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));
+}
+