|
| 1 | +/* |
| 2 | + mode Mix RTC alarm |
| 3 | +
|
| 4 | + This sketch shows how to configure the alarm A & B (if exists) |
| 5 | + of the RTC in MIX or BCD (BINARY none) mode |
| 6 | +
|
| 7 | + Creation 12 Dec 2017 |
| 8 | + by Wi6Labs |
| 9 | + Modified 03 Jul 2020 |
| 10 | + by Frederic Pillon for STMicroelectronics |
| 11 | + Modified 03 Jul 2023 |
| 12 | + by Francois Ramu for STMicroelectronics |
| 13 | +
|
| 14 | + This example code is in the public domain. |
| 15 | +
|
| 16 | + https://github.com/stm32duino/STM32RTC |
| 17 | +*/ |
| 18 | + |
| 19 | +#include <STM32RTC.h> |
| 20 | + |
| 21 | +/* Get the rtc object */ |
| 22 | +STM32RTC& rtc = STM32RTC::getInstance(); |
| 23 | + |
| 24 | +/* Change these values to set the current initial time */ |
| 25 | +const byte seconds = 06; |
| 26 | +const byte minutes = 22; |
| 27 | +const byte hours = 16; |
| 28 | + |
| 29 | +/* Change these values to set the current initial date */ |
| 30 | +const byte day = 25; |
| 31 | +const byte month = 6; |
| 32 | +const byte year = 23; |
| 33 | + |
| 34 | +uint32_t timeout; |
| 35 | + |
| 36 | +void setup() |
| 37 | +{ |
| 38 | + Serial.begin(115200); |
| 39 | + |
| 40 | + // Select RTC clock source: LSI_CLOCK, LSE_CLOCK or HSE_CLOCK. |
| 41 | + rtc.setClockSource(STM32RTC::LSE_CLOCK); |
| 42 | + |
| 43 | + /* Configure the RTC mode : STM32RTC::MODE_MIX or STM32RTC::MODE_BCD */ |
| 44 | + rtc.setBinaryMode(STM32RTC::MODE_BCD); |
| 45 | + |
| 46 | + rtc.begin(true, STM32RTC::HOUR_24); |
| 47 | + |
| 48 | + rtc.setTime(hours, minutes, seconds); |
| 49 | + rtc.setDate(day, month, year); |
| 50 | + |
| 51 | + /* wait for a while */ |
| 52 | + delay(300); |
| 53 | + |
| 54 | + Serial.printf("Start at %02d:%02d:%02d.%03d\r\n", |
| 55 | + rtc.getHours(), rtc.getMinutes(), rtc.getSeconds(), rtc.getSubSeconds()); |
| 56 | + |
| 57 | + /* Attach the callback function before enabling Interrupt */ |
| 58 | + rtc.attachInterrupt(alarmAMatch); |
| 59 | + |
| 60 | + /* Program the AlarmA in 12 seconds */ |
| 61 | + rtc.setAlarmDay(day); |
| 62 | + rtc.setAlarmTime(hours, minutes, seconds + 12); |
| 63 | + rtc.enableAlarm(rtc.MATCH_DHHMMSS); |
| 64 | + Serial.printf("Set Alarm A in 12s (at %02d:%02d:%02d)\r\n", |
| 65 | + rtc.getAlarmHours(), rtc.getAlarmMinutes(), rtc.getAlarmSeconds()); |
| 66 | + |
| 67 | +#ifdef RTC_ALARM_B |
| 68 | + /* Program ALARM B in 600ms ms from now (keep timeout < 1000ms) */ |
| 69 | + timeout = rtc.getSubSeconds() + 600; |
| 70 | + |
| 71 | + rtc.attachInterrupt(alarmBMatch, STM32RTC::ALARM_B); |
| 72 | + rtc.setAlarmSubSeconds(timeout, STM32RTC::ALARM_B); |
| 73 | + rtc.enableAlarm(rtc.MATCH_SUBSEC, STM32RTC::ALARM_B); |
| 74 | + Serial.printf("Set Alarm B (in %d ms) at %d ms\r\n", 600, |
| 75 | + rtc.getAlarmSubSeconds(STM32RTC::ALARM_B)); |
| 76 | +#endif /* RTC_ALARM_B */ |
| 77 | +} |
| 78 | + |
| 79 | +void loop() |
| 80 | +{ |
| 81 | + /* Just wait for Alarm */ |
| 82 | +} |
| 83 | + |
| 84 | +void alarmAMatch(void *data) |
| 85 | +{ |
| 86 | + UNUSED(data); |
| 87 | + rtc.disableAlarm(STM32RTC::ALARM_A); |
| 88 | + Serial.printf("Alarm A Match at %02d:%02d:%02d\r\n", |
| 89 | + rtc.getHours(), rtc.getMinutes(), rtc.getSeconds()); |
| 90 | +} |
| 91 | + |
| 92 | +#ifdef RTC_ALARM_B |
| 93 | +void alarmBMatch(void *data) |
| 94 | +{ |
| 95 | + UNUSED(data); |
| 96 | + rtc.disableAlarm(STM32RTC::ALARM_B); |
| 97 | + Serial.printf("Alarm B Match at %d ms\r\n", rtc.getSubSeconds()); |
| 98 | +} |
| 99 | +#endif /* RTC_ALARM_B */ |
0 commit comments