RTC demonstration program. Prompts user for input and sets RTC. Created for Nucleo F446RE. On the F446RE board, reset and even downloading new code doesn't disrupt or reset the RTC.

Dependencies:   mbed

Revision:
0:a338d4ab50c9
Child:
1:811c5881ca5c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sat Jul 28 12:31:40 2018 +0000
@@ -0,0 +1,71 @@
+/* 
+  Project: my_SetRTC, File: main.cpp.
+
+  Based on mktime example: weekday calculator from 
+  http://www.cplusplus.com/reference/ctime/mktime. Modified to set Mbed/Nucleo
+  RTC.
+  
+  Modified by C. S. Tritt
+  Last modified: 7/28/18 (v. 1.0)
+*/
+#include <mbed.h> // wait, etc.
+//#include <mbed_mktime.h> // For _rtc_mktime. Didn't work.
+//#include <stdio.h> // printf, scanf - incl. in mbed.h.
+//#include <time.h> // time_t, struct tm, time, mktime in mbed.h.
+
+int main ()
+{
+  // Create variables.
+  time_t rawtime;
+  struct tm* timeinfo;
+  int year, month ,day, hours, minutes, secs;
+  const char* weekday[] = { "Sunday", "Monday",
+                             "Tuesday", "Wednesday",
+                             "Thursday", "Friday", "Saturday"};
+
+  // Prompt user for date and time.
+  printf("\nTurn echo on to see your input.\n");
+  printf("\nEnter year (YYYY): "); scanf ("%d",&year);
+  printf("\nEnter month (1 to 12): "); scanf ("%d",&month);
+  printf("\nEnter day (D or DD): "); scanf ("%d",&day);
+  printf("\nEnter hours (0 to 23):"); scanf ("%d",&hours);
+  printf("\nEnter hours (0 to 59):"); scanf ("%d",&minutes);
+  printf("\nEnter hours (0 to 59):"); scanf ("%d",&secs);
+
+  // Get current RTC time structure.
+  time(&rawtime);
+  timeinfo = localtime(&rawtime); // Modify to local time.
+  
+  // Modify it using user input.
+  timeinfo->tm_year = year - 1900; // tm_year are from 1900.
+  timeinfo->tm_mon = month - 1; // tm_month are 0 to 11.
+  timeinfo->tm_mday = day;
+  timeinfo->tm_hour = hours;
+  timeinfo->tm_min = minutes;
+  timeinfo->tm_sec = secs;
+
+  // Call mktime: timeinfo->tm_wday will be set.
+  mktime(timeinfo);
+
+  // tm_wday are 0 to 6. Use to check entered values.
+  printf("\nThis must be a %s.\n", weekday[timeinfo->tm_wday]);
+  
+  // Set the mbed RTC to the current time. Not interupt safe. Should be 
+  // _rtc_mktime, but that didn't work.
+  set_time(mktime(timeinfo));
+  
+  while (true) { // Loop forever more.
+    // Get current time.
+    time_t nowSecs = time(NULL);
+    
+    // Display it in various ways.   
+    printf("\nTime as seconds since January 1, 1970 = %d\n", nowSecs);
+    printf("Time as a basic string = %s", ctime(&nowSecs)); // ctime incl. \n.
+    char buffer[32];
+    strftime(buffer, 32, "%I:%M %p\n", localtime(&nowSecs));
+    printf("As a custom string = %s\n", buffer);
+    
+    // Sleep for 1 second.
+    wait(1.0);
+  }
+}
\ No newline at end of file