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

Committer:
CSTritt
Date:
Sat Jul 28 17:55:03 2018 +0000
Revision:
1:811c5881ca5c
Parent:
0:a338d4ab50c9
Updated user I/O. Made time reset optional.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
CSTritt 1:811c5881ca5c 1 /*
CSTritt 0:a338d4ab50c9 2 Project: my_SetRTC, File: main.cpp.
CSTritt 0:a338d4ab50c9 3
CSTritt 1:811c5881ca5c 4 Based on mktime example: weekday calculator from
CSTritt 0:a338d4ab50c9 5 http://www.cplusplus.com/reference/ctime/mktime. Modified to set Mbed/Nucleo
CSTritt 0:a338d4ab50c9 6 RTC.
CSTritt 1:811c5881ca5c 7
CSTritt 1:811c5881ca5c 8 Note the on the Nucleo F446RE the Nucleo the RTC survives (is not reset by)
CSTritt 1:811c5881ca5c 9 device reset an even downloading new code! It is also claimed that it
CSTritt 1:811c5881ca5c 10 continues to keep time even when the processor in in sleep mode.
CSTritt 1:811c5881ca5c 11
CSTritt 0:a338d4ab50c9 12 Modified by C. S. Tritt
CSTritt 1:811c5881ca5c 13 Last modified: 7/28/18 (v. 1.1)
CSTritt 0:a338d4ab50c9 14 */
CSTritt 0:a338d4ab50c9 15 #include <mbed.h> // wait, etc.
CSTritt 0:a338d4ab50c9 16 //#include <mbed_mktime.h> // For _rtc_mktime. Didn't work.
CSTritt 0:a338d4ab50c9 17 //#include <stdio.h> // printf, scanf - incl. in mbed.h.
CSTritt 0:a338d4ab50c9 18 //#include <time.h> // time_t, struct tm, time, mktime in mbed.h.
CSTritt 0:a338d4ab50c9 19
CSTritt 0:a338d4ab50c9 20 int main ()
CSTritt 0:a338d4ab50c9 21 {
CSTritt 1:811c5881ca5c 22 // Announce project and program information.
CSTritt 1:811c5881ca5c 23 printf("\nProject: my_SetRTC, File: main.cpp (v. 1.1)\n");
CSTritt 1:811c5881ca5c 24
CSTritt 1:811c5881ca5c 25 // Create variables.
CSTritt 1:811c5881ca5c 26 time_t rawtime;
CSTritt 1:811c5881ca5c 27 struct tm* timeinfo;
CSTritt 1:811c5881ca5c 28 int year, month ,day, hours, minutes, secs;
CSTritt 1:811c5881ca5c 29 const char* weekday[] = { "Sunday", "Monday",
CSTritt 1:811c5881ca5c 30 "Tuesday", "Wednesday",
CSTritt 1:811c5881ca5c 31 "Thursday", "Friday", "Saturday"
CSTritt 1:811c5881ca5c 32 };
CSTritt 0:a338d4ab50c9 33
CSTritt 1:811c5881ca5c 34 // Get current RTC time structure.
CSTritt 1:811c5881ca5c 35 time(&rawtime);
CSTritt 1:811c5881ca5c 36 timeinfo = localtime(&rawtime); // Modify to local time.
CSTritt 1:811c5881ca5c 37 printf("Current RTC time: %s", ctime(&rawtime)); // Display current time.
CSTritt 1:811c5881ca5c 38
CSTritt 1:811c5881ca5c 39 // See if user wants to reset the RTC.
CSTritt 1:811c5881ca5c 40 printf("\nTurn local echo on to see your input.\n");
CSTritt 1:811c5881ca5c 41 printf("Do you want to set new time (y or n): ");
CSTritt 1:811c5881ca5c 42 char userIn;
CSTritt 1:811c5881ca5c 43 scanf("%c", &userIn);
CSTritt 1:811c5881ca5c 44 if (userIn == 'y') {
CSTritt 1:811c5881ca5c 45 // Prompt user for date and time.
CSTritt 1:811c5881ca5c 46 printf("\nEnter year (YYYY): ");
CSTritt 1:811c5881ca5c 47 scanf ("%d",&year);
CSTritt 1:811c5881ca5c 48 printf("\n%d read.", year);
CSTritt 1:811c5881ca5c 49 printf("\nEnter month (1 to 12): ");
CSTritt 1:811c5881ca5c 50 scanf ("%d",&month);
CSTritt 1:811c5881ca5c 51 printf("\n%d read.", month);
CSTritt 1:811c5881ca5c 52 printf("\nEnter day (D or DD): ");
CSTritt 1:811c5881ca5c 53 scanf ("%d",&day);
CSTritt 1:811c5881ca5c 54 printf("\n%d read.", day);
CSTritt 1:811c5881ca5c 55 printf("\nEnter hours (0 to 23):");
CSTritt 1:811c5881ca5c 56 scanf ("%d",&hours);
CSTritt 1:811c5881ca5c 57 printf("\n%d read.", hours);
CSTritt 1:811c5881ca5c 58 printf("\nEnter minutes (0 to 59):");
CSTritt 1:811c5881ca5c 59 scanf ("%d",&minutes);
CSTritt 1:811c5881ca5c 60 printf("\n%d read.", minutes);
CSTritt 1:811c5881ca5c 61 printf("\nEnter seconds (0 to 59):");
CSTritt 1:811c5881ca5c 62 scanf ("%d",&secs);
CSTritt 1:811c5881ca5c 63 printf("\n%d read.", secs);
CSTritt 1:811c5881ca5c 64
CSTritt 1:811c5881ca5c 65 // Modify it using user input.
CSTritt 1:811c5881ca5c 66 timeinfo->tm_year = year - 1900; // tm_year are from 1900.
CSTritt 1:811c5881ca5c 67 timeinfo->tm_mon = month - 1; // tm_month are 0 to 11.
CSTritt 1:811c5881ca5c 68 timeinfo->tm_mday = day;
CSTritt 1:811c5881ca5c 69 timeinfo->tm_hour = hours;
CSTritt 1:811c5881ca5c 70 timeinfo->tm_min = minutes;
CSTritt 1:811c5881ca5c 71 timeinfo->tm_sec = secs;
CSTritt 1:811c5881ca5c 72
CSTritt 1:811c5881ca5c 73 // Call mktime: timeinfo->tm_wday will be set.
CSTritt 1:811c5881ca5c 74 mktime(timeinfo);
CSTritt 1:811c5881ca5c 75
CSTritt 1:811c5881ca5c 76 // tm_wday are 0 to 6. Use to check entered values.
CSTritt 1:811c5881ca5c 77 printf("\nThis must be a %s.\n", weekday[timeinfo->tm_wday]);
CSTritt 1:811c5881ca5c 78
CSTritt 1:811c5881ca5c 79 // Set the mbed RTC to the current time. Not interupt safe. Should be
CSTritt 1:811c5881ca5c 80 // _rtc_mktime, but that didn't work.
CSTritt 1:811c5881ca5c 81 set_time(mktime(timeinfo));
CSTritt 1:811c5881ca5c 82 }
CSTritt 0:a338d4ab50c9 83
CSTritt 1:811c5881ca5c 84 while (true) { // Loop forever more.
CSTritt 1:811c5881ca5c 85 // Get current time.
CSTritt 1:811c5881ca5c 86 time_t nowSecs = time(NULL);
CSTritt 1:811c5881ca5c 87
CSTritt 1:811c5881ca5c 88 // Display it in various ways.
CSTritt 1:811c5881ca5c 89 printf("\nSeconds since UNIX Epoch: %d\n", nowSecs);
CSTritt 1:811c5881ca5c 90 printf("Time as a ctime string: %s", ctime(&nowSecs)); // String has \n.
CSTritt 1:811c5881ca5c 91 char buffer[32];
CSTritt 1:811c5881ca5c 92 strftime(buffer, 32, "%I:%M %p\n", localtime(&nowSecs));
CSTritt 1:811c5881ca5c 93 printf("Time as a strftime custom string: %s\n", buffer);
CSTritt 1:811c5881ca5c 94
CSTritt 1:811c5881ca5c 95 // Sleep for 1 second.
CSTritt 1:811c5881ca5c 96 wait(1.0);
CSTritt 1:811c5881ca5c 97 }
CSTritt 0:a338d4ab50c9 98 }