Working Version of the Real Time Clock module DS1307.

Dependents:   Rtc_Ds1307_Sample TAREA_5_PROCESADORES Rtc_Ds1307_lcd_alarma Rtc_Ds1307_Reloj_con_alarma_aplazable ... more

This is my implementation of the DS1307.

I plan to add functionality which will make use of the OSC Input and which will increment the time continuously. A query to the module will then only have to be made when the MBED has been powered down.

Files at this revision

API Documentation at this revision

Comitter:
leihen
Date:
Sun Jun 23 17:02:21 2013 +0000
Parent:
4:d2cc690aaac7
Child:
6:bba89618ee63
Commit message:
Update

Changed in this revision

RtcCls.c Show annotated file Show diff for this revision Revisions of this file
RtcCls.h Show annotated file Show diff for this revision Revisions of this file
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/RtcCls.c	Sun Jun 23 16:29:35 2013 +0000
+++ b/RtcCls.c	Sun Jun 23 17:02:21 2013 +0000
@@ -1,6 +1,5 @@
+
 #include "RtcCls.h"
-#define DEBUG
-#include "debug.h"
 
 
 
@@ -8,10 +7,10 @@
 RtcCls::RtcCls(PinName sda, PinName scl, PinName sqw, bool bUseSqw)
     : Rtc_Ds1307(sda, scl), m_sqw(sqw), m_bUseSqw(bUseSqw), m_bAlarmEnabled(false), m_alarmfunc(NULL)
 {
-    //  Only register the callback and start the SQW if requested to do so. Otherwise the system 
+    //  Only register the callback and start the SQW if requested to do so. Otherwise the system
     //  will use the MBED built-in RTC.
     if (m_bUseSqw) {
-        Time t;
+        Time_rtc t;
         //  start the wave
         setSquareWaveOutput(true, RS_1Hz);
         //  query time
@@ -38,20 +37,20 @@
 {
     //  when not using the HW support, we have to query the RTC chip. Other wise we can just return out stored value
     if (!m_bUseSqw) {
-        Time t;
+        Time_rtc t;
         getTime(t);
         struct tm now = {t.sec, t.min, t.hour, t.date, t.mon, t.year};
-        m_time = mktime(&tm); 
+        m_time = mktime(&tm);
     }
     return m_time;
 }
 
 void RtcCls::setTime(time_t t)
 {
-    Time tim;
+    Time_rtc tim;
     struct tm *now;
     now = localtime(&t);
-    
+
     tim.sec = now->tm_sec;
     tim.min = now->tm_min;
     tim.hour = now->tm_hour;
@@ -59,10 +58,6 @@
     tim.mon = now->tm_mon;
     tim.year = now->tm_year + 1900;
     tim.wday = now->tm_wday +1;
-    
+
     setTime( tim, true, true);
-}
-
-
-
-
+}
\ No newline at end of file
--- a/RtcCls.h	Sun Jun 23 16:29:35 2013 +0000
+++ b/RtcCls.h	Sun Jun 23 17:02:21 2013 +0000
@@ -1,38 +1,36 @@
-#ifndef __RTC_H__
-#define __RTC_H__
+#ifndef __DAMNFILEHEADER__
+#define __DAMNFILEHEADER__
+
 
 #include "Rtc_Ds1307.h"
-#include "mbed.h"
 
 typedef void (*callback_t)(void);
 
 class RtcCls   :   public Rtc_Ds1307
 {
-    protected:
-        InterruptIn         m_sqw;
-        bool                m_bUseSqw;
-        time_t              m_time;             //  Only used in case of SQW use
-        
-        bool                m_bAlarmEnabled;
-        callback_t          m_alarmfunc;
-        time_t              m_alarmTime;
-        
-    public:
-        RtcCls(PinName sda, PinName scl, PinName sqw, bool bUseSqw);
+protected:
+//    InterruptIn         m_sqw;
+    bool                m_bUseSqw;
+    time_t              m_time;             //  Only used in case of SQW use
+
+    bool                m_bAlarmEnabled;
+    callback_t          m_alarmfunc;
+    time_t              m_alarmTime;
+
+public:
+    RtcCls(PinName sda, PinName scl, PinName sqw, bool bUseSqw);
 
-    protected:        
-        static void _callback(void);
-        
-    public:
-        time_t              getTime();
-        void                setTime(time_t t);
-    public: 
-        void                setAlarm(int nSeconds, callback_t alarmfunc)
-        {
-            m_alarmfunc = alarmfunc;
-            m_alarmTime = m_time + nSeconds;
-            m_bAlarmEnabled = (alarmfunc == NULL) ? false : true;
-        }
+protected:
+    static void _callback(void);
+
+public:
+    time_t              getTime();
+    void                setTime(time_t t);
+public:
+    void                setAlarm(int nSeconds, callback_t alarmfunc) {
+        m_alarmfunc = alarmfunc;
+        m_alarmTime = m_time + nSeconds;
+        m_bAlarmEnabled = (alarmfunc == NULL) ? false : true;
+    }
 };
-
-#endif //   __RTC_H__
\ No newline at end of file
+#endif //   __RTC_CLS_H__
--- a/Rtc_Ds1307.cpp	Sun Jun 23 16:29:35 2013 +0000
+++ b/Rtc_Ds1307.cpp	Sun Jun 23 17:02:21 2013 +0000
@@ -1,8 +1,9 @@
 /* Rtc_Ds1307.cpp */
-
+#include "mbed.h"
 #include "Rtc_Ds1307.h"
+#ifndef DEBUG
 //#define DEBUG
-#undef DEBUG
+#endif
 #include "debug.h"
 
 const char *Rtc_Ds1307::m_weekDays[] = { "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };    
@@ -25,7 +26,7 @@
         delete m_rtc;
 }
 
-bool Rtc_Ds1307::setTime(Time& time, bool start, bool thm)
+bool Rtc_Ds1307::setTime(Time_rtc& time, bool start, bool thm)
 {
     char buffer[7];
     INFO("reading clock registers to write the new time : %d:%d:%d\n", time.hour, time.min, time.sec);
@@ -57,7 +58,7 @@
     return true;
 }
 
-bool Rtc_Ds1307::getTime(Time& time)
+bool Rtc_Ds1307::getTime(Time_rtc& time)
 {
     char buffer[7];
     bool thm = false;
@@ -133,7 +134,7 @@
     return true;
 }
 
-bool Rtc_Ds1307::setSquareWaveOutput(bool ena, RateSelect_t rs)
+bool Rtc_Ds1307::setSquareWaveOutput(bool ena, SqwRateSelect_t rs)
 {
     char reg;
     INFO("Reading register value first\n");
--- a/Rtc_Ds1307.h	Sun Jun 23 16:29:35 2013 +0000
+++ b/Rtc_Ds1307.h	Sun Jun 23 17:02:21 2013 +0000
@@ -23,7 +23,6 @@
 #ifndef __RTC_DS1307_H__
 #define __RTC_DS1307_H__
 
-#include "mbed.h"
 
 /** Structure which is used to exchange the time and date
  */
@@ -35,18 +34,16 @@
     int date;       /*!< day of month [0..31] */
     int mon;        /*!< month of year [1..12] */
     int year;       /*!< year [2000..2255] */
-} Time;
+} Time_rtc;
 
 /** 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;
-
+    RS1Hz = 0,
+    RS4kHz = 1,
+    RS8kHz = 2,
+    RS32kHz = 3
+} SqwRateSelect_t;
 
 /** Class Rtc_Ds1307 implements the real time clock module DS1307
  *
@@ -78,7 +75,7 @@
          *
          * @returns true if successful, otherwise an acknowledge error occured
          */
-        bool getTime(Time& time);
+        bool getTime(Time_rtc& time);
         
         /** Write the given time onto the RTC chip
          *
@@ -90,7 +87,7 @@
          *
          * @returns true if successful, otherwise an acknowledge error occured
          */
-        bool setTime(Time& time, bool start, bool thm);        
+        bool setTime(Time_rtc& 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
@@ -128,7 +125,7 @@
          *
          * @return true if the operation was successful or false otherwise
          */
-        bool setSquareWaveOutput(bool ena, RateSelect_t rs);
+        bool setSquareWaveOutput(bool ena, SqwRateSelect_t rs);
         
     private:
         bool read(int address, char* buffer, int len);