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.

Revision:
1:64274190e842
Parent:
0:3940f0ad2ca5
Child:
2:ee81f2c5a706
diff -r 3940f0ad2ca5 -r 64274190e842 Rtc_Ds1307.h
--- a/Rtc_Ds1307.h	Sun Jun 02 09:59:39 2013 +0000
+++ b/Rtc_Ds1307.h	Sun Jun 02 18:57:26 2013 +0000
@@ -25,6 +25,15 @@
 
 #include "mbed.h"
 
+typedef struct {
+    int sec;
+    int min;
+    int hour;
+    int wday;
+    int date;
+    int mon;
+    int year;
+} Time;
 
 /** Class Rtc_Ds1307 implements the real time clock module DS1307
  *
@@ -35,7 +44,9 @@
 class Rtc_Ds1307
 {
         I2C*    m_rtc;
-
+        
+        static const char *m_weekDays[];    
+        
     public:
         /** public constructor which creates the real time clock object
          *
@@ -48,9 +59,47 @@
         
         ~Rtc_Ds1307();
 
-        bool getTime(tm& time);
+        /** Read the current time from RTC chip
+         *
+         * @param time : reference to a struct tm which will be filled with the time from rtc
+         *
+         * @returns true if successful, otherwise an acknowledge error occured
+         */
+        bool getTime(Time& time);
+        
+        /** Write the given time onto the RTC chip
+         *
+         * @param time : refereence to a struct which contains valid date and time information
+         *
+         * @param start : contains true if the clock shall start (or keep on running).
+         *
+         * @param thm : 12-hour-mode if set to true, otherwise 24-hour-mode will be set.
+         *
+         * @returns true if successful, otherwise an acknowledge error occured
+         */
+        bool setTime(Time& time, bool start, bool thm);        
+        
         
-        bool setTime(tm& time);        
+        /** Service function to convert a weekday into a string representation
+         *
+         * @param wday : day of week to convert (starting with sunday = 1, monday = 2, ..., saturday = 7
+         *
+         * @returns the corresponding string representation
+         */
+        const char* weekdayToString( int wday )
+        { return m_weekDays[wday%7]; }
+
+    private:
+        bool read(int address, char* buffer, int len);
+        bool write(int address, char* buffer, int len);
+        
+        static int bcdToDecimal(int bcd)
+        { return ((bcd&0xF0)>>4)*10 + (bcd&0x0F); }
+        
+        static int decimalToBcd(int dec)
+        { return (dec%10) + ((dec/10)<<4); }
+
+
 };