|
| 1 | +/* |
| 2 | + F1RTCDateRetention |
| 3 | +
|
| 4 | + This sketch shows how to keep the RTC date with backup registers on a stm32F1 MCU. |
| 5 | + It is reserved for stm32F1 as other devices actually embed the feature. |
| 6 | + If a VBat is present on the target board, then the content is kept when |
| 7 | + the VDD power supply is off, else the content is reset |
| 8 | +
|
| 9 | + Creation 03 dec 2021 |
| 10 | + by FRASTM for STMicroelectronics |
| 11 | +
|
| 12 | + This example code is in the public domain. |
| 13 | +
|
| 14 | + https://github.com/stm32duino/STM32RTC |
| 15 | +*/ |
| 16 | + |
| 17 | +#include <STM32RTC.h> |
| 18 | + |
| 19 | +/* Get the rtc object */ |
| 20 | +STM32RTC& rtc = STM32RTC::getInstance(); |
| 21 | + |
| 22 | +#if !defined(STM32F1xx) |
| 23 | +#error "Not applicable (only stm32F1xx save date in backup memory)" |
| 24 | +#endif /* !STM32F1xx */ |
| 25 | + |
| 26 | +#define INITIAL_SEC 53 |
| 27 | +#define INITIAL_MIN 59 |
| 28 | +#define INITIAL_HOUR 23 |
| 29 | +#define INITIAL_WDAY 2 |
| 30 | +#define INITIAL_DAY 7 |
| 31 | +#define INITIAL_MONTH 12 |
| 32 | +#define INITIAL_YEAR 21 |
| 33 | + |
| 34 | +uint32_t subSec; |
| 35 | +byte seconds; |
| 36 | +byte minutes; |
| 37 | +byte hours; |
| 38 | +byte am_pm; |
| 39 | + |
| 40 | +/* these values are read in the backUp register */ |
| 41 | +byte weekDay; |
| 42 | +byte day; |
| 43 | +byte month; |
| 44 | +byte year; |
| 45 | + |
| 46 | +void setup() |
| 47 | +{ |
| 48 | + Serial.begin(115200); |
| 49 | + |
| 50 | + // Select RTC clock source: LSI_CLOCK, LSE_CLOCK or HSE_CLOCK. |
| 51 | + // By default the LSI is selected as source. |
| 52 | + //rtc.setClockSource(STM32RTC::LSE_CLOCK); |
| 53 | + |
| 54 | + // true: reset the backUp register (restart from the initial date/time above on each reset) |
| 55 | + // false: keep the latest date from the BackUp register on each reset |
| 56 | + bool reset_bkup = false; |
| 57 | + rtc.begin(reset_bkup); // initialize RTC 24H format |
| 58 | + // get the date if stored in BackUp reg |
| 59 | + rtc.getDate(&weekDay, &day, &month, &year); |
| 60 | + if (reset_bkup) { |
| 61 | + Serial.printf("reset the date to %02d/%02d/%02d\n", day, month, year); |
| 62 | + } else { |
| 63 | + Serial.printf("date from the BackUp %02d/%02d/%02d\n", day, month, year); |
| 64 | + } |
| 65 | + |
| 66 | + // Check if date is valid = read in the backUp reg. |
| 67 | + // Note that backup reg only keep the date /time |
| 68 | + // and a power off reset the content if not saved by Vbat |
| 69 | + // HAL_RTC init date is set to 1st of January 2000 |
| 70 | +if ((day == 1) && (month == RTC_MONTH_JANUARY) && (year == 0)) { |
| 71 | + // Set the time |
| 72 | + rtc.setHours(INITIAL_HOUR); |
| 73 | + rtc.setMinutes(INITIAL_MIN); |
| 74 | + rtc.setSeconds(INITIAL_SEC); |
| 75 | + |
| 76 | + // Set the date |
| 77 | + rtc.setWeekDay(INITIAL_WDAY); |
| 78 | + rtc.setDay(INITIAL_DAY); |
| 79 | + rtc.setMonth(INITIAL_MONTH); |
| 80 | + rtc.setYear(INITIAL_YEAR); |
| 81 | + |
| 82 | + // Here you can also use |
| 83 | + //rtc.setTime(hours, minutes, seconds); |
| 84 | + //rtc.setDate(weekDay, day, month, year); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +void loop() |
| 89 | +{ |
| 90 | + // Print date... |
| 91 | + Serial.printf("%02d/%02d/%02d \t", rtc.getDay(), rtc.getMonth(), rtc.getYear()); |
| 92 | + |
| 93 | + uint8_t sec = 0; |
| 94 | + uint8_t mn = 0; |
| 95 | + uint8_t hrs = 0; |
| 96 | + uint32_t subs = 0; |
| 97 | + rtc.getTime(&hrs, &mn, &sec, &subs); |
| 98 | + |
| 99 | + // ...and time |
| 100 | + Serial.printf("%02d:%02d:%02d\n", hrs, mn, sec); |
| 101 | + |
| 102 | + delay(1000); |
| 103 | +} |
0 commit comments