![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
RTC DS3234 library
Fork of WDtester by
Revision 2:c2e2b6238a69, committed 2014-12-18
- Comitter:
- duke970
- Date:
- Thu Dec 18 10:09:31 2014 +0000
- Parent:
- 1:7c0fa2bb38df
- Commit message:
- RTC DS3234 library
Changed in this revision
diff -r 7c0fa2bb38df -r c2e2b6238a69 DS3234.cpp --- a/DS3234.cpp Wed Dec 17 08:42:51 2014 +0000 +++ b/DS3234.cpp Thu Dec 18 10:09:31 2014 +0000 @@ -11,12 +11,11 @@ month 0x85 year 0x86 */ -void DS3234_set(PinName pin, struct ts t) + +void set(PinName pin, struct ts t) { - SPI spi(PTD6, PTD7, PTD5); - - - uint8_t i, century; + SPI spi(PTD6, PTD7, PTD5); + uint8_t i, century; if (t.year > 2000) { century = 0x80; @@ -28,16 +27,55 @@ 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); - + cs = 0; spi.write(i + 0x80); if (i == 5) spi.write(dectobcd(TimeDate[5]) + century); else spi.write(dectobcd(TimeDate[i])); - cs.write(1); + cs = 1; } + } +void DS3234_get(PinName pin, struct ts *t) +{ + SPI spi(PTD6, PTD7, PTD5); + uint8_t TimeDate[7]; //second,minute,hour,dow,day,month,year + uint8_t century = 0; + uint8_t i, n; + uint16_t year_full; + DigitalOut cs(pin); + + for (i = 0; i <= 6; i++) { + cs = 0; + spi.write(i + 0x00); + n = spi.write(0x00); + + if (i == 5) { // month address also contains the century on bit7 + TimeDate[5] = bcdtodec(n & 0x1F); + century = (n & 0x80) >> 7; + } else { + TimeDate[i] = bcdtodec(n); + } + } + + if (century == 1) + year_full = 2000 + TimeDate[6]; + else + year_full = 1900 + TimeDate[6]; + + t->sec = TimeDate[0]; + t->min = TimeDate[1]; + t->hour = TimeDate[2]; + t->mday = TimeDate[4]; + t->mon = TimeDate[5]; + t->year = year_full; + t->wday = TimeDate[3]; + t->year_s = TimeDate[6]; +} + uint8_t dectobcd(const uint8_t val) @@ -49,4 +87,3 @@ { return ((val / 16 * 10) + (val % 16)); } -
diff -r 7c0fa2bb38df -r c2e2b6238a69 DS3234.h --- a/DS3234.h Wed Dec 17 08:42:51 2014 +0000 +++ b/DS3234.h Thu Dec 18 10:09:31 2014 +0000 @@ -1,4 +1,5 @@ #include <mbed.h> +#include <SPI.h> #ifndef DS3234_h #define DS3234_h @@ -33,14 +34,16 @@ uint8_t year_s; /* year in short notation*/ }; -void DS3234_init(PinName pin, const uint8_t ctrl_reg); -void DS3234_get(PinName pin, struct ts *t); -void DS3234_set(PinName pin, struct ts t); +void init(PinName pin, const uint8_t ctrl_reg); +void get(PinName pin, struct ts *t); +void set(PinName pin, struct ts t); // Helping Functions + uint8_t dectobcd(const uint8_t val); uint8_t bcdtodec(const uint8_t val); + #endif
diff -r 7c0fa2bb38df -r c2e2b6238a69 mbed-rtos.lib --- a/mbed-rtos.lib Wed Dec 17 08:42:51 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/mbed_official/code/mbed-rtos/#a3452b867ec3