Library for DS3231 RTC

Dependents:   ard2pmod DS3231demo DS3231demo_COM_Port_Output MAXREFDES99_RTC_Display ... more

DS3231 Component Page

Revision:
5:61dfe2690360
Parent:
4:0beb5e9ac927
Child:
6:e8850ad15893
--- a/ds3231.cpp	Thu Nov 20 22:16:15 2014 +0000
+++ b/ds3231.cpp	Fri Nov 28 02:36:26 2014 +0000
@@ -647,6 +647,73 @@
 
 
 /**********************************************************//**
+* Get epoch time based on current RTC time and date.  
+* DS3231 must be configured and running before this fx is 
+* called
+*
+* On Entry:
+*
+* On Exit:
+*     @return return value = epoch time
+*
+* Example:
+* @code
+* 
+* //instantiate rtc object
+* Ds3231 rtc(D14, D15); 
+* 
+* time_t epoch_time; 
+*
+* epoch_time = rtc.get_epoch();
+*
+* @endcode
+**************************************************************/
+time_t Ds3231::get_epoch(void)
+{
+    //system vars
+    struct tm sys_time;
+    
+    //RTC vars
+    ds3231_time_t rtc_time;
+    ds3231_calendar_t rtc_calendar;
+    
+    get_calendar(&rtc_calendar);
+    get_time(&rtc_time);
+    
+    sys_time.tm_wday = rtc_calendar.day - 1;
+    sys_time.tm_mday = rtc_calendar.date;
+    sys_time.tm_mon = rtc_calendar.month - 1;
+    sys_time.tm_year = rtc_calendar.year + 100;
+    
+    //check for 12hr or 24hr mode
+    if(rtc_time.mode)
+    {
+        //check am/pm
+        if(rtc_time.am_pm  && (rtc_time.hours != 12))
+        {
+            sys_time.tm_hour = rtc_time.hours + 12;
+        }
+        else
+        {
+            sys_time.tm_hour = rtc_time.hours;
+        }
+        
+    }
+    else
+    {
+        //24hr mode
+        sys_time.tm_hour = rtc_time.hours;
+    }
+    
+    sys_time.tm_min = rtc_time.minutes;
+    sys_time.tm_sec = rtc_time.seconds;
+    
+    //make epoch time
+    return(mktime(&sys_time));
+}
+
+
+/**********************************************************//**
 * Private mmber fx, converts unsigned char to BCD
 *
 * On Entry: