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.
Fork of RTC-DS1307 by
Revision 2:ee81f2c5a706, committed 2013-06-05
- Comitter:
- leihen
- Date:
- Wed Jun 05 20:42:37 2013 +0000
- Parent:
- 1:64274190e842
- Child:
- 3:e89d63f3342e
- Commit message:
- Fully working Version.
Changed in this revision
| Rtc_Ds1307.cpp | Show annotated file Show diff for this revision Revisions of this file |
| Rtc_Ds1307.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/Rtc_Ds1307.cpp Sun Jun 02 18:57:26 2013 +0000
+++ b/Rtc_Ds1307.cpp Wed Jun 05 20:42:37 2013 +0000
@@ -37,24 +37,47 @@
bool Rtc_Ds1307::setTime(Time& time, bool start, bool thm)
{
- INFO("Setting new time : %d:%d:%d\n", time.hour, time.min, time.sec);
-
+ char buffer[7];
+ INFO("reading clock registers to write the new time : %d:%d:%d\n", time.hour, time.min, time.sec);
+ if (!read(0,buffer,7)) {
+ ERR("Failed to read from RTC\n");
+ return false;
+ }
+ // Now update only the time part (saving the existing flags)
+ if (start) { buffer[0] &= 0x7F; } else { buffer[0] |= 0x80; }
+ buffer[0] = (buffer[0]&0x80) | (decimalToBcd(time.sec)& 0x7f);
+ buffer[1] = decimalToBcd(time.min);
+ if (thm) {
+ // AM PM format
+ buffer[2] = (buffer[2]& 196) | (time.hour>12 ? (0x20 | ((decimalToBcd(time.hour-12)))) : decimalToBcd(time.hour));
+ }
+ else {
+ // 24 hours format
+ buffer[2] = (buffer[2]& 196) | (decimalToBcd(time.hour) & 0x3F);
+ }
+ buffer[3] = time.wday;
+ buffer[4] = decimalToBcd(time.date);
+ buffer[5] = decimalToBcd(time.mon);
+ buffer[6] = decimalToBcd(time.year-2000);
+ INFO("Writing new time and date data to RTC\n");
+ if (!write(0, buffer, 7) ) {
+ ERR("Failed to write the data to RTC!\n");
+ return false;
+ }
return true;
}
bool Rtc_Ds1307::getTime(Time& time)
{
char buffer[7];
- bool bClock_halt = false;
bool thm = false;
INFO("Getting time from RTC\n");
- if (read(0, buffer, 7) != 0) {
+ if (!read(0, buffer, 7) ) {
// Failed to read
ERR("Failed to read from RTC\n");
return false;
}
- bClock_halt = ((buffer[0] & 128) == 128);
thm = ((buffer[2] & 64) == 64);
time.sec = bcdToDecimal(buffer[0]&0x7F);
time.min = bcdToDecimal(buffer[1]);
@@ -70,18 +93,89 @@
time.wday = buffer[3];
time.date = Rtc_Ds1307::bcdToDecimal( buffer[4]);
time.mon = Rtc_Ds1307::bcdToDecimal( buffer[5]);
- time.year = Rtc_Ds1307::bcdToDecimal(buffer[6]) + 100; // plus hundret is because RTC is giving the years since 2000, but std c struct tm needs years since 1900
+ time.year = Rtc_Ds1307::bcdToDecimal(buffer[6]) + 2000; // plus hundret is because RTC is giving the years since 2000, but std c struct tm needs years since 1900
+
+ return true;
+}
+
+
+bool Rtc_Ds1307::startClock()
+{
+ char strtStop;
+
+ INFO ("Reading clock start/stop register value\n");
+ if (!read(0, &strtStop, 1)) {
+ ERR("Failed to read clock start stop register !\n");
+ return false;
+ }
+
+ strtStop &= 0x7F;
+
+ INFO("Writing back start/stop register value\n");
+ if (!write(0, &strtStop, 1)) {
+ ERR("Failed to write the start stop register !\n");
+ return false;
+ }
+
+ INFO("Start/stop register value successfully written\n");
+ return true;
+}
+
+bool Rtc_Ds1307::stopClock()
+{
+ char strtStop;
+
+ INFO ("Reading clock start/stop register value\n");
+ if (!read(0, &strtStop, 1)) {
+ ERR("Failed to read clock start stop register !\n");
+ return false;
+ }
- INFO("Clock is %s\n", bClock_halt ? "halted" : "running");
- return false;
+ strtStop |= 0x80;
+
+ INFO("Writing back start/stop register value\n");
+ if (!write(0, &strtStop, 1)) {
+ ERR("Failed to write the start stop register !\n");
+ return false;
+ }
+
+ INFO("Start/stop register value successfully written\n");
+ return true;
}
+bool Rtc_Ds1307::setSquareWaveOutput(bool ena, RateSelect_t rs)
+{
+ char reg;
+ INFO("Reading register value first\n");
+
+ if (!read(7,®, 1)) {
+ ERR("Failed to read register value !\n");
+ return false;
+ }
+ INFO("[Reg:0x07] = %02x\n", reg);
+
+ // preserve the OUT control bit while writing the frequency and enable bits
+ reg = (reg & 0x80) | (ena ? 0x10 : 0) | ((char)rs & 0x03);
+
+ INFO("Writing back register value\n");
+ INFO("[Reg:0x07] = %02x\n", reg);
+
+ if (!write(7, ®, 1)) {
+ ERR("Failed to write register value !\n");
+ return false;
+ }
+
+ INFO("Successfully changed the square wave output.\n");
+ return true;
+}
+
+
bool Rtc_Ds1307::read(int address, char *buffer, int len)
{
char buffer2[2] = {(char)address, 0};
- m_rtc->start();
+// m_rtc->start();
if (m_rtc->write(0xd0, buffer2, 1) != 0) {
ERR("Failed to write register address on rtv!\n");
m_rtc->stop();
@@ -104,7 +198,7 @@
for (int i = 0 ; i < len ; i++)
buffer2[i+1] = buffer[i];
- m_rtc->start();
+// m_rtc->start();
if (m_rtc->write(0xd0, buffer2, len+1) != 0) {
ERR("Failed to write data to rtc\n");
m_rtc->stop();
--- a/Rtc_Ds1307.h Sun Jun 02 18:57:26 2013 +0000
+++ b/Rtc_Ds1307.h Wed Jun 05 20:42:37 2013 +0000
@@ -25,16 +25,29 @@
#include "mbed.h"
+/** Structure which is used to exchange the time and date
+ */
typedef struct {
- int sec;
- int min;
- int hour;
- int wday;
- int date;
- int mon;
- int year;
+ int sec; /*!< seconds [0..59] */
+ int min; /*!< minutes {0..59] */
+ int hour; /*!< hours [0..23] */
+ int wday; /*!< weekday [1..7, where 1 = sunday, 2 = monday, ... */
+ int date; /*!< day of month [0..31] */
+ int mon; /*!< month of year [1..12] */
+ int year; /*!< year [2000..2255] */
} Time;
+/** RateSelect specifies the valid frequency values for the square wave output
+ */
+typedef enum {
+ RS_1Hz= 0, /*!< 1 Hz frequency output */
+ RS_4kHz= 1, /*!< 4.096 kHz frequency output */
+ RS_8kHz= 2, /*!< 8.192 kHz frequency output */
+ RS_32kHz= 3 /*!< 32.768 kHz frequency output */
+
+} RateSelect_t;
+
+
/** Class Rtc_Ds1307 implements the real time clock module DS1307
*
* You can read the clock and set a new time and date.
@@ -79,6 +92,23 @@
*/
bool setTime(Time& time, bool start, bool thm);
+ /** Start the clock. Please note that the seconds register need to be read and
+ * written in order to start or stop the clock. This can lead to an error
+ * in the time value. The recommended way of starting and stoping the clock is
+ * to write the actual date and time and set the start bit accordingly.
+ *
+ * @returns true if the clock was started, false if a communication error occured
+ */
+ bool startClock();
+
+ /** Stop the clock. Please note that the seconds register need to be read and
+ * written in order to start or stop the clock. This can lead to an error
+ * in the time value. The recommended way of starting and stoping the clock is
+ * to write the actual date and time and set the start bit accordingly.
+ *
+ * @returns true if the clock was stopped, false if a communication error occured
+ */
+ bool stopClock();
/** Service function to convert a weekday into a string representation
*
@@ -89,6 +119,17 @@
const char* weekdayToString( int wday )
{ return m_weekDays[wday%7]; }
+ /** Enable Square Wave output. The function enables or disables the square wave output
+ * of the module and sets the desired frequency.
+ *
+ * @param ena : if set to true, the square wave output is enabled.
+ *
+ * @param rs : rate select, can be either one of the four values defined by type /c RateSelect_t
+ *
+ * @return true if the operation was successful or false otherwise
+ */
+ bool setSquareWaveOutput(bool ena, RateSelect_t rs);
+
private:
bool read(int address, char* buffer, int len);
bool write(int address, char* buffer, int len);
