|
| 1 | +/* |
| 2 | + change RTC mode from BCD --> MIX --> BIN |
| 3 | +
|
| 4 | + This sketch shows that changing the RTC mode does not affect the calendar |
| 5 | + Configure the RTC, set an alarm, on alarm match change the RTC mode. |
| 6 | +
|
| 7 | + Creation 01 march 2024 |
| 8 | + by Francois Ramu for STMicroelectronics |
| 9 | +
|
| 10 | + This example code is in the public domain. |
| 11 | +
|
| 12 | + https://github.com/stm32duino/STM32RTC |
| 13 | +*/ |
| 14 | + |
| 15 | +#include <STM32RTC.h> |
| 16 | + |
| 17 | +/* Get the rtc object */ |
| 18 | +STM32RTC& rtc = STM32RTC::getInstance(); |
| 19 | +bool rtc_mode_changed; |
| 20 | + |
| 21 | +void setup() |
| 22 | +{ |
| 23 | + Serial.begin(115200); |
| 24 | + |
| 25 | + // Select RTC clock source: LSI_CLOCK, LSE_CLOCK or HSE_CLOCK. |
| 26 | + rtc.setClockSource(STM32RTC::LSE_CLOCK); |
| 27 | + |
| 28 | + /* Configure the RTC mode */ |
| 29 | + rtc.setBinaryMode(STM32RTC::MODE_BCD); |
| 30 | + rtc_mode_changed = true; |
| 31 | + |
| 32 | + /* RTC mode is in BCD mode by default */ |
| 33 | + rtc.begin(true, STM32RTC::HOUR_24); |
| 34 | + |
| 35 | + /* wait for a while */ |
| 36 | + delay(500); |
| 37 | + |
| 38 | + // Set the calendar |
| 39 | + rtc.setDate(01, 03, 24); |
| 40 | + rtc.setTime(14, 02, 58); |
| 41 | + |
| 42 | + // Program ALARM A to change the RTC mode (set then enable) |
| 43 | + rtc.attachInterrupt(alarmMatch); |
| 44 | + rtc.setAlarmSeconds(5, STM32RTC::ALARM_A); |
| 45 | + rtc.enableAlarm(rtc.MATCH_SS, STM32RTC::ALARM_A); |
| 46 | +} |
| 47 | + |
| 48 | +void loop() |
| 49 | +{ |
| 50 | + /* Reports the RTC mode when it has been changed */ |
| 51 | + if (rtc_mode_changed) { |
| 52 | + if (rtc.getBinaryMode() == STM32RTC::MODE_BCD) { |
| 53 | + Serial.printf("RTC mode is MODE_BCD at "); |
| 54 | + } else if (rtc.getBinaryMode() == STM32RTC::MODE_MIX) { |
| 55 | + Serial.printf("RTC mode is MODE_MIX at "); |
| 56 | + } else { |
| 57 | + Serial.printf("RTC mode is MODE_BIN at "); |
| 58 | + } |
| 59 | + |
| 60 | + Serial.printf("%02d/%02d/%02d - ", rtc.getDay(), rtc.getMonth(), rtc.getYear()); |
| 61 | + Serial.printf("%02d:%02d:%02d \n", rtc.getHours(), rtc.getMinutes(), rtc.getSeconds()); |
| 62 | + |
| 63 | + rtc_mode_changed = false; |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +void alarmMatch(void *data) |
| 68 | +{ |
| 69 | + UNUSED(data); |
| 70 | + Serial.printf("Alarm A Match! : change RTC mode at "); |
| 71 | + Serial.printf("%02d:%02d:%02d \n", rtc.getHours(), rtc.getMinutes(), rtc.getSeconds()); |
| 72 | + |
| 73 | + /* Configure the new RTC mode */ |
| 74 | + if (rtc.getBinaryMode() == STM32RTC::MODE_BCD) { |
| 75 | + rtc.setBinaryMode(STM32RTC::MODE_MIX); |
| 76 | + } else if (rtc.getBinaryMode() == STM32RTC::MODE_MIX) { |
| 77 | + rtc.setBinaryMode(STM32RTC::MODE_BIN); |
| 78 | + } else { |
| 79 | + rtc.setBinaryMode(STM32RTC::MODE_BCD); |
| 80 | + } |
| 81 | + rtc_mode_changed = true; |
| 82 | +} |
0 commit comments