Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
ds1337.cpp
00001 #include "ds1337.h" 00002 00003 #define TIME_LENGTH 8 //Length of buffer needed for time/date 00004 00005 DS1337::DS1337(PinName sda, PinName scl) : _i2c(sda, scl) 00006 { 00007 _i2c.frequency(DS1337_I2C_FCY); 00008 memset(err, 0, ERR_BUFFER_LEN); 00009 } 00010 00011 void DS1337::time2str(Time * t, char * str) 00012 { 00013 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); 00014 } 00015 00016 bool DS1337::now(Time * now) 00017 { 00018 buffer[0] = 0x00; // memory address 00019 00020 if (_i2c.write(DS1337_ADDR, &buffer[0], 1) != 0) 00021 { 00022 sprintf(err, "RTC command write Failed"); 00023 return false; 00024 } 00025 00026 if (_i2c.read(DS1337_ADDR, buffer, DS1337_BUFFER_SIZE) != 0) 00027 { 00028 sprintf(err, "RTC buffer read Failed"); 00029 return false; 00030 } 00031 00032 if (buffer[0] & 0x80) 00033 { 00034 sprintf(err, "Clock Stopped"); 00035 return false; 00036 } 00037 00038 if (buffer[2] & 0x40) 00039 { 00040 sprintf(err, "12hour not supported"); 00041 return false; 00042 } 00043 00044 now->tm_sec = bcdToDecimal(buffer[0] & 0x7F); 00045 now->tm_min = bcdToDecimal(buffer[1] & 0x7F); 00046 now->tm_hour = bcdToDecimal(buffer[2] & 0x3F); 00047 now->tm_wday = buffer[3] & 0x07; 00048 now->tm_mday = bcdToDecimal(buffer[4] & 0x3F); 00049 now->tm_mon = bcdToDecimal(buffer[5] & 0x1F); 00050 now->tm_year = bcdToDecimal(buffer[6] & 0xFF); 00051 00052 return true; 00053 } 00054 00055 bool DS1337::set_time(Time * now) 00056 { 00057 buffer[0] = 0x00; // memory address 00058 buffer[1] = decimalToBcd(now->tm_sec) & 0x7F; 00059 buffer[2] = decimalToBcd(now->tm_min) & 0x7F; 00060 buffer[3] = decimalToBcd(now->tm_hour) & 0x3F; // 24-hour format 00061 buffer[4] = now->tm_wday & 0x07; 00062 buffer[5] = decimalToBcd(now->tm_mday) & 0x3F; 00063 buffer[6] = decimalToBcd(now->tm_mon) & 0x1F; 00064 buffer[7] = decimalToBcd(now->tm_year) & 0xFF; 00065 00066 if (_i2c.write(DS1337_ADDR, buffer, TIME_LENGTH) != 0) 00067 { 00068 sprintf(err, "RTC command write Failed"); 00069 return false; 00070 } 00071 00072 return true; 00073 } 00074
Generated on Wed Jul 13 2022 18:43:15 by
1.7.2