Dallas DS1307 real-time clock minimalistic driver
Dependents: testing_RTC_OneWire EMIRv2
DS1307.cpp@3:c5018c71887f, 2014-04-28 (annotated)
- Committer:
- alpov
- Date:
- Mon Apr 28 13:08:38 2014 +0000
- Revision:
- 3:c5018c71887f
- Parent:
- 2:6d8d35a5b3f2
fixed wrong 12/24-hour config
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
alpov | 0:f29e45a25cba | 1 | #include "mbed.h" |
alpov | 0:f29e45a25cba | 2 | #include "DS1307.h" |
alpov | 0:f29e45a25cba | 3 | |
alpov | 1:60383faa35a9 | 4 | DS1307::DS1307(PinName sda, PinName scl) : ds1307_i2c(sda, scl) |
alpov | 0:f29e45a25cba | 5 | { |
alpov | 1:60383faa35a9 | 6 | ds1307_i2c.frequency(DS1307_FREQ); |
alpov | 0:f29e45a25cba | 7 | } |
alpov | 0:f29e45a25cba | 8 | |
alpov | 1:60383faa35a9 | 9 | time_t DS1307::now() |
alpov | 0:f29e45a25cba | 10 | { |
alpov | 0:f29e45a25cba | 11 | struct tm now; |
alpov | 1:60383faa35a9 | 12 | char addr = 0x00; // memory address |
alpov | 0:f29e45a25cba | 13 | char buffer[7]; |
alpov | 1:60383faa35a9 | 14 | |
alpov | 1:60383faa35a9 | 15 | if (ds1307_i2c.write(DS1307_ADDR, &addr, 1) != 0) return 0; |
alpov | 1:60383faa35a9 | 16 | if (ds1307_i2c.read(DS1307_ADDR, buffer, 7) != 0) return 0; |
alpov | 0:f29e45a25cba | 17 | |
alpov | 1:60383faa35a9 | 18 | if (buffer[0] & 0x80) return 0; // clock stopped |
alpov | 3:c5018c71887f | 19 | if (buffer[2] & 0x40) return 0; // 12-hour format not supported |
alpov | 0:f29e45a25cba | 20 | now.tm_sec = bcdToDecimal(buffer[0] & 0x7F); |
alpov | 0:f29e45a25cba | 21 | now.tm_min = bcdToDecimal(buffer[1]); |
alpov | 0:f29e45a25cba | 22 | now.tm_hour = bcdToDecimal(buffer[2] & 0x3F); |
alpov | 0:f29e45a25cba | 23 | now.tm_mday = bcdToDecimal(buffer[4]); |
alpov | 0:f29e45a25cba | 24 | now.tm_mon = bcdToDecimal(buffer[5]) - 1; |
alpov | 0:f29e45a25cba | 25 | now.tm_year = bcdToDecimal(buffer[6]) + 2000 - 1900; |
alpov | 0:f29e45a25cba | 26 | |
alpov | 0:f29e45a25cba | 27 | return mktime(&now); |
alpov | 0:f29e45a25cba | 28 | } |
alpov | 0:f29e45a25cba | 29 | |
alpov | 1:60383faa35a9 | 30 | bool DS1307::set_time(time_t t) |
alpov | 0:f29e45a25cba | 31 | { |
alpov | 0:f29e45a25cba | 32 | struct tm *now; |
alpov | 1:60383faa35a9 | 33 | char buffer[9]; |
alpov | 0:f29e45a25cba | 34 | |
alpov | 0:f29e45a25cba | 35 | now = localtime(&t); |
alpov | 0:f29e45a25cba | 36 | |
alpov | 1:60383faa35a9 | 37 | buffer[0] = 0x00; // memory address |
alpov | 3:c5018c71887f | 38 | buffer[1] = decimalToBcd(now->tm_sec) & 0x7F; // CH = 0 |
alpov | 1:60383faa35a9 | 39 | buffer[2] = decimalToBcd(now->tm_min); |
alpov | 3:c5018c71887f | 40 | buffer[3] = decimalToBcd(now->tm_hour) & 0x3F; // 24-hour format |
alpov | 1:60383faa35a9 | 41 | buffer[4] = now->tm_wday + 1; |
alpov | 1:60383faa35a9 | 42 | buffer[5] = decimalToBcd(now->tm_mday); |
alpov | 1:60383faa35a9 | 43 | buffer[6] = decimalToBcd(now->tm_mon+1); |
alpov | 1:60383faa35a9 | 44 | buffer[7] = decimalToBcd(now->tm_year + 1900 - 2000); |
alpov | 1:60383faa35a9 | 45 | buffer[8] = 0x00; // OUT = 0 |
alpov | 1:60383faa35a9 | 46 | if (ds1307_i2c.write(DS1307_ADDR, buffer, 9) != 0) return false; |
alpov | 0:f29e45a25cba | 47 | |
alpov | 0:f29e45a25cba | 48 | return true; |
alpov | 0:f29e45a25cba | 49 | } |