Simple RTC class based on DS1307. Emphasis on simple. Allows you to run at 100k or 400k Hz (for newer DS1307 capable devices). MapTime() allows you to set the time() service to the same as the RTC. Uses struct tm throughout so you can use traditional time functions for manipulation.
Diff: RTclock.cpp
- Revision:
- 9:3a0ba8364ef2
- Parent:
- 7:3621025e7949
- Child:
- 10:e5eabd3a1ca6
--- a/RTclock.cpp Mon Aug 11 19:10:09 2014 +0000 +++ b/RTclock.cpp Sat Aug 30 15:55:28 2014 +0000 @@ -1,9 +1,10 @@ #include "mbed.h" #include "RTclock.h" -RTclock::RTclock(PinName in_nSDA, PinName in_nSCL, bool in_bHiSpeed) +RTclock::RTclock(PinName in_nSDA, PinName in_nSCL, EClockType in_eClockType, bool in_bHiSpeed) : RTclock_parent(in_nSDA,in_nSCL) , m_bTwelveHour(false) + , m_eClockType(in_eClockType) { // Frequency depends on chip - most are 100KHz frequency(in_bHiSpeed ? 400000 : 100000); @@ -96,28 +97,53 @@ int nHour = in_sTM.tm_hour; if (in_bTwelveHour) nHour %= 12; - aBuffer[0] &= 0x7f; - aBuffer[0] = (aBuffer[0] & 0x80) | (decimalToBcd(in_sTM.tm_sec)& 0x7f); - aBuffer[1] = decimalToBcd(in_sTM.tm_min); - aBuffer[2] = (aBuffer[2] & 0xc4) | (decimalToBcd(nHour) & 0x3f); - aBuffer[3] = in_sTM.tm_wday; - aBuffer[4] = decimalToBcd(in_sTM.tm_mday); - aBuffer[5] = decimalToBcd(in_sTM.tm_mon); - aBuffer[6] = decimalToBcd(in_sTM.tm_year + 1900 - 2000); - - // Handle the 12hr clock bits - if (in_bTwelveHour) + switch (m_eClockType) { - // Turn on 12hr clock - aBuffer[2] |= 0x40; + case eDS1311: + aBuffer[0] &= 0x7f; + aBuffer[0] = (aBuffer[0] & 0x80) | (decimalToBcd(in_sTM.tm_sec)& 0x7f); + aBuffer[1] = decimalToBcd(in_sTM.tm_min); + aBuffer[2] = (aBuffer[2] & 0xc4) | (decimalToBcd(nHour) & 0x3f); + aBuffer[3] = in_sTM.tm_wday; + aBuffer[4] = decimalToBcd(in_sTM.tm_mday); + aBuffer[5] = decimalToBcd(in_sTM.tm_mon); + aBuffer[6] = decimalToBcd(in_sTM.tm_year + 1900 - 2000); - // Set am/pm bit based on hours - if (in_sTM.tm_hour >= 12) aBuffer[2] |= 0x20; else aBuffer[2] &= ~0x20; - } - else - { - aBuffer[2] &= ~64; - } + // Handle the 12hr clock bits + if (in_bTwelveHour) + { + // Turn on 12hr clock + aBuffer[2] |= 0x40; + + // Set am/pm bit based on hours + if (in_sTM.tm_hour >= 12) aBuffer[2] |= 0x20; else aBuffer[2] &= ~0x20; + } + else + { + aBuffer[2] &= ~64; + } + break; + + case eDS3231: + aBuffer[0] = decimalToBcd(in_sTM.tm_sec) & 0x7f; + aBuffer[1] = decimalToBcd(in_sTM.tm_min) & 0x7f; + aBuffer[2] = decimalToBcd(nHour) & (m_bTwelveHour ? 0x1f : 0x3f); + aBuffer[3] = in_sTM.tm_wday; + aBuffer[4] = decimalToBcd(in_sTM.tm_mday); + aBuffer[5] = decimalToBcd(in_sTM.tm_mon) & 0x80 /* 2000+ */; + aBuffer[6] = decimalToBcd(in_sTM.tm_year + 1900 - 2000); + + // Handle the 12hr clock bits + if (in_bTwelveHour) + { + // Turn on 12hr clock + aBuffer[2] |= 0x40; + + // Set am/pm bit based on hours + if (in_sTM.tm_hour >= 12) aBuffer[2] |= 0x20; + } + break; + } // Write new date and time setRunning(false);