DS1337 RTC library RTC 55 bytes nvram
ds1337.cpp@2:66dbf3ae0f89, 2015-10-29 (annotated)
- Committer:
- labishrestha
- Date:
- Thu Oct 29 02:38:18 2015 +0000
- Revision:
- 2:66dbf3ae0f89
- Parent:
- 1:8a0e272ed8e6
Intermediate changes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
labishrestha | 0:366fa629ac27 | 1 | #include "ds1337.h" |
labishrestha | 0:366fa629ac27 | 2 | |
labishrestha | 0:366fa629ac27 | 3 | #define TIME_LENGTH 8 //Length of buffer needed for time/date |
labishrestha | 0:366fa629ac27 | 4 | |
labishrestha | 0:366fa629ac27 | 5 | DS1337::DS1337(PinName sda, PinName scl) : _i2c(sda, scl) |
labishrestha | 0:366fa629ac27 | 6 | { |
labishrestha | 0:366fa629ac27 | 7 | _i2c.frequency(DS1337_I2C_FCY); |
labishrestha | 2:66dbf3ae0f89 | 8 | memset(err, 0, ERR_BUFFER_LEN); |
labishrestha | 0:366fa629ac27 | 9 | } |
labishrestha | 0:366fa629ac27 | 10 | |
labishrestha | 0:366fa629ac27 | 11 | void DS1337::time2str(Time * t, char * str) |
labishrestha | 0:366fa629ac27 | 12 | { |
labishrestha | 0:366fa629ac27 | 13 | sprintf(str, "%02d/%02d/%04d-%02d:%02d:%02d",t->tm_mon, t->tm_mday,(t->tm_year+1900), t->tm_hour,t->tm_min,t->tm_sec); |
labishrestha | 0:366fa629ac27 | 14 | } |
labishrestha | 0:366fa629ac27 | 15 | |
labishrestha | 0:366fa629ac27 | 16 | bool DS1337::now(Time * now) |
labishrestha | 0:366fa629ac27 | 17 | { |
labishrestha | 0:366fa629ac27 | 18 | buffer[0] = 0x00; // memory address |
labishrestha | 0:366fa629ac27 | 19 | |
labishrestha | 0:366fa629ac27 | 20 | if (_i2c.write(DS1337_ADDR, &buffer[0], 1) != 0) |
labishrestha | 0:366fa629ac27 | 21 | { |
labishrestha | 0:366fa629ac27 | 22 | sprintf(err, "RTC command write Failed"); |
labishrestha | 0:366fa629ac27 | 23 | return false; |
labishrestha | 0:366fa629ac27 | 24 | } |
labishrestha | 0:366fa629ac27 | 25 | |
labishrestha | 0:366fa629ac27 | 26 | if (_i2c.read(DS1337_ADDR, buffer, DS1337_BUFFER_SIZE) != 0) |
labishrestha | 0:366fa629ac27 | 27 | { |
labishrestha | 0:366fa629ac27 | 28 | sprintf(err, "RTC buffer read Failed"); |
labishrestha | 0:366fa629ac27 | 29 | return false; |
labishrestha | 0:366fa629ac27 | 30 | } |
labishrestha | 0:366fa629ac27 | 31 | |
labishrestha | 0:366fa629ac27 | 32 | if (buffer[0] & 0x80) |
labishrestha | 0:366fa629ac27 | 33 | { |
labishrestha | 0:366fa629ac27 | 34 | sprintf(err, "Clock Stopped"); |
labishrestha | 0:366fa629ac27 | 35 | return false; |
labishrestha | 0:366fa629ac27 | 36 | } |
labishrestha | 0:366fa629ac27 | 37 | |
labishrestha | 0:366fa629ac27 | 38 | if (buffer[2] & 0x40) |
labishrestha | 0:366fa629ac27 | 39 | { |
labishrestha | 0:366fa629ac27 | 40 | sprintf(err, "12hour not supported"); |
labishrestha | 0:366fa629ac27 | 41 | return false; |
labishrestha | 0:366fa629ac27 | 42 | } |
labishrestha | 0:366fa629ac27 | 43 | |
labishrestha | 0:366fa629ac27 | 44 | now->tm_sec = bcdToDecimal(buffer[0] & 0x7F); |
labishrestha | 0:366fa629ac27 | 45 | now->tm_min = bcdToDecimal(buffer[1] & 0x7F); |
labishrestha | 0:366fa629ac27 | 46 | now->tm_hour = bcdToDecimal(buffer[2] & 0x3F); |
labishrestha | 1:8a0e272ed8e6 | 47 | now->tm_wday = buffer[3] & 0x07; |
labishrestha | 0:366fa629ac27 | 48 | now->tm_mday = bcdToDecimal(buffer[4] & 0x3F); |
labishrestha | 0:366fa629ac27 | 49 | now->tm_mon = bcdToDecimal(buffer[5] & 0x1F); |
labishrestha | 0:366fa629ac27 | 50 | now->tm_year = bcdToDecimal(buffer[6] & 0xFF); |
labishrestha | 0:366fa629ac27 | 51 | |
labishrestha | 0:366fa629ac27 | 52 | return true; |
labishrestha | 0:366fa629ac27 | 53 | } |
labishrestha | 0:366fa629ac27 | 54 | |
labishrestha | 0:366fa629ac27 | 55 | bool DS1337::set_time(Time * now) |
labishrestha | 0:366fa629ac27 | 56 | { |
labishrestha | 0:366fa629ac27 | 57 | buffer[0] = 0x00; // memory address |
labishrestha | 0:366fa629ac27 | 58 | buffer[1] = decimalToBcd(now->tm_sec) & 0x7F; |
labishrestha | 0:366fa629ac27 | 59 | buffer[2] = decimalToBcd(now->tm_min) & 0x7F; |
labishrestha | 0:366fa629ac27 | 60 | buffer[3] = decimalToBcd(now->tm_hour) & 0x3F; // 24-hour format |
labishrestha | 0:366fa629ac27 | 61 | buffer[4] = now->tm_wday & 0x07; |
labishrestha | 0:366fa629ac27 | 62 | buffer[5] = decimalToBcd(now->tm_mday) & 0x3F; |
labishrestha | 0:366fa629ac27 | 63 | buffer[6] = decimalToBcd(now->tm_mon) & 0x1F; |
labishrestha | 0:366fa629ac27 | 64 | buffer[7] = decimalToBcd(now->tm_year) & 0xFF; |
labishrestha | 0:366fa629ac27 | 65 | |
labishrestha | 0:366fa629ac27 | 66 | if (_i2c.write(DS1337_ADDR, buffer, TIME_LENGTH) != 0) |
labishrestha | 0:366fa629ac27 | 67 | { |
labishrestha | 0:366fa629ac27 | 68 | sprintf(err, "RTC command write Failed"); |
labishrestha | 0:366fa629ac27 | 69 | return false; |
labishrestha | 0:366fa629ac27 | 70 | } |
labishrestha | 0:366fa629ac27 | 71 | |
labishrestha | 0:366fa629ac27 | 72 | return true; |
labishrestha | 0:366fa629ac27 | 73 | } |
labishrestha | 2:66dbf3ae0f89 | 74 |