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