Ejemplo RTC DS3231

Dependencies:   mbed ds3231

Revision:
7:e049867a7716
Parent:
6:2641d53a460a
diff -r 2641d53a460a -r e049867a7716 main.cpp
--- a/main.cpp	Thu Mar 19 21:59:08 2015 +0000
+++ b/main.cpp	Mon Jan 27 03:36:50 2020 +0000
@@ -1,139 +1,33 @@
-/**********************************************************************
-* 
-* Demo DS3231 Library
-*
-***********************************************************************
-* Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
-*
-* Permission is hereby granted, free of charge, to any person obtaining a
-* copy of this software and associated documentation files (the "Software"),
-* to deal in the Software without restriction, including without limitation
-* the rights to use, copy, modify, merge, publish, distribute, sublicense,
-* and/or sell copies of the Software, and to permit persons to whom the
-* Software is furnished to do so, subject to the following conditions:
-*
-* The above copyright notice and this permission notice shall be included
-* in all copies or substantial portions of the Software.
-*
-* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
-* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
-* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
-* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
-* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-* OTHER DEALINGS IN THE SOFTWARE.
-*
-* Except as contained in this notice, the name of Maxim Integrated
-* Products, Inc. shall not be used except as stated in the Maxim Integrated
-* Products, Inc. Branding Policy.
-*
-* The mere transfer of this software does not imply any licenses
-* of trade secrets, proprietary technology, copyrights, patents,
-* trademarks, maskwork rights, or any other form of intellectual
-* property whatsoever. Maxim Integrated Products, Inc. retains all
-* ownership rights.
-**********************************************************************/
-
 #include "ds3231.h"
 
-#define ESC 0x1B
+//--------------------------Puertos---------------------------------//
+Ds3231 rtc(PTE0, PTE1); //rtc object Ds3231::Ds3231(PinName sda, PinName scl) : I2C(sda, scl)
 
+//----------------------Funciones--------------------------------//
 void get_user_input(char* message, uint8_t min, uint8_t max, uint32_t* member);
 void get_user_input(char* message, uint8_t min, uint8_t max, bool* member);
+void set_RTC(void);
 
 int main(void)
 {
-    //rtc object
-    Ds3231 rtc(D14, D15); 
-    
-    time_t epoch_time;
-    
-    //DS3231 rtc variables
-    
-    //default, use bit masks in ds3231.h for desired operation
+  //DS3231 rtc variables - default, use bit masks in ds3231.h for desired operation
     ds3231_cntl_stat_t rtc_control_status = {0,0}; 
-    ds3231_time_t rtc_time;
-    ds3231_calendar_t rtc_calendar;
-    
     rtc.set_cntl_stat_reg(rtc_control_status);
     
-    //get day from user
-    get_user_input("\nPlease enter day of week, 1 for Sunday (1-7): ", 1,
-                    7, &rtc_calendar.day);
-
-    //get day of month from user
-    get_user_input("\nPlease enter day of month (1-31): ", 1, 31, 
-                    &rtc_calendar.date);
-
-    //get month from user
-    get_user_input("\nPlease enter the month, 1 for January (1-12): ", 1, 
-                    12, &rtc_calendar.month);
-
-    //get year from user
-    get_user_input("\nPlease enter the year (0-99): ",0, 99, 
-                    &rtc_calendar.year);
-      
-    //Get time mode
-    get_user_input("\nWhat time mode? 1 for 12hr 0 for 24hr: ", 0, 1, 
-                   &rtc_time.mode);  
+    //******************************//
+    ds3231_time_t time; 
+    ds3231_calendar_t calendar; 
+    
+    set_RTC(); //Configuración del RTC
     
-    if(rtc_time.mode)
-    {
-        //Get AM/PM status
-        get_user_input("\nIs it AM or PM? 0 for AM 1 for PM: ", 0, 1, 
-                       &rtc_time.am_pm);  
-        //Get hour from user
-        get_user_input("\nPlease enter the hour (1-12): ", 1, 12, 
-                       &rtc_time.hours);
-    }
-    else
-    {
-        //Get hour from user
-        get_user_input("\nPlease enter the hour (0-23): ", 0, 23, 
-                       &rtc_time.hours);
-    }
-     
-    //Get minutes from user
-    get_user_input("\nPlease enter the minute (0-59): ", 0, 59, 
-                   &rtc_time.minutes);
-    
-    
-    //Get seconds from user
-    get_user_input("\nPlease enter the second (0-59): ", 0, 59, 
-                   &rtc_time.seconds);
-    
-    
-    
-    //Set the time, uses inverted logic for return value
-    if(rtc.set_time(rtc_time))
-    {
-        printf("\nrtc.set_time failed!!\n");
-        exit(0);
-    }
-    
-    //Set the calendar, uses inverted logic for return value
-    if(rtc.set_calendar(rtc_calendar))
-    {
-        printf("\nrtc.set_calendar failed!!\n");
-        exit(0);
-    }
-    
-    char buffer[32];
-    
-    for(;;)
+    while(1)
     {   
-        printf("%c[2J", ESC); //clear screen
-        printf("%c[H", ESC); //move cursor to Home
-        
-        //new epoch time fx
-        epoch_time = rtc.get_epoch();
+        rtc.get_time(&time);
+        printf("Time %02d:%02d:%02d - RTC\n", time.hours, time.minutes, time.seconds);
         
-        printf("\nTime as seconds since January 1, 1970 = %d\n", epoch_time);
-        
-        printf("\nTime as a basic string = %s", ctime(&epoch_time));
- 
-        strftime(buffer, 32, "%I:%M %p\n", localtime(&epoch_time));
-        printf("\nTime as a custom formatted string = %s", buffer);
+        rtc.get_calendar(&calendar);
+        printf("Calendar %02d/%02d/%02d\n",calendar.date, calendar.month, calendar.year);
+        printf("\n");
         
         wait(1.0);
     }//loop 
@@ -196,4 +90,75 @@
     while((*(member) < min) || (*(member) > max));
 }
 
+/**********************************************************************/
+void set_RTC(void){
+
+//Cambio de la configuración del RTC DS3231
+    ds3231_time_t rtc_time;
+    ds3231_calendar_t rtc_calendar;
+    
+    //get day from user
+    get_user_input("\nPlease enter day of week, 1 for Sunday (1-7): ", 1,
+                    7, &rtc_calendar.day);
+
+    //get day of month from user
+    get_user_input("\nPlease enter day of month (1-31): ", 1, 31, 
+                    &rtc_calendar.date);
+
+    //get month from user
+    get_user_input("\nPlease enter the month, 1 for January (1-12): ", 1, 
+                    12, &rtc_calendar.month);
+
+    //get year from user
+    get_user_input("\nPlease enter the year (0-99): ",0, 99, 
+                    &rtc_calendar.year);
+      
+    //Get time mode
+    get_user_input("\nWhat time mode? 1 for 12hr 0 for 24hr: ", 0, 1, 
+                   &rtc_time.mode);  
+    
+    if(rtc_time.mode)
+    {
+        //Get AM/PM status
+        get_user_input("\nIs it AM or PM? 0 for AM 1 for PM: ", 0, 1, 
+                       &rtc_time.am_pm);  
+        //Get hour from user
+        get_user_input("\nPlease enter the hour (1-12): ", 1, 12, 
+                       &rtc_time.hours);
+    }
+    else
+    {
+        //Get hour from user
+        get_user_input("\nPlease enter the hour (0-23): ", 0, 23, 
+                       &rtc_time.hours);
+    }
+     
+    //Get minutes from user
+    get_user_input("\nPlease enter the minute (0-59): ", 0, 59, 
+                   &rtc_time.minutes);
+    
+    
+    //Get seconds from user
+    get_user_input("\nPlease enter the second (0-59): ", 0, 59, 
+                   &rtc_time.seconds);
+    
+    
+    
+    //Set the time, uses inverted logic for return value
+    if(rtc.set_time(rtc_time))
+    {
+        printf("\nrtc.set_time failed!!\n");
+        exit(0);
+    }
+    
+    //Set the calendar, uses inverted logic for return value
+    if(rtc.set_calendar(rtc_calendar))
+    {
+        printf("\nrtc.set_calendar failed!!\n");
+        exit(0);
+    }    
+    
+}
+
+
     
\ No newline at end of file