Skip to content

Commit ab48ac8

Browse files
committed
add new sketch for RTC running in MIX mode with Alarm
This examples is configuring the RTC in MIX mode (binary + calendar BCD) and set Alarm B (if exists) few ms after the current time, whenAlarm A is set in calendar mode. Mainly used on stm32wl55 devices.
1 parent cbd5bcb commit ab48ac8

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

examples/mixRTCAlarm/mixRTCAlarm.ino

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
#define ALARM_DELAY_MS 400
36+
37+
void setup()
38+
{
39+
Serial.begin(115200);
40+
41+
// Select RTC clock source: LSI_CLOCK, LSE_CLOCK or HSE_CLOCK.
42+
rtc.setClockSource(STM32RTC::LSE_CLOCK);
43+
44+
/* Configure the MIX mode */
45+
rtc.setRTCMode(STM32RTC::MODE_MIX);
46+
47+
rtc.begin(true, STM32RTC::HOUR_24);
48+
49+
rtc.setTime(hours, minutes, seconds);
50+
rtc.setDate(day, month, year);
51+
52+
/* wait for a while */
53+
delay(200);
54+
55+
Serial.printf("Start at %02d:%02d:%02d.%03d\r\n",
56+
rtc.getHours(), rtc.getMinutes(), rtc.getSeconds(), rtc.getSubSeconds());
57+
58+
/* Attach the callback function before enabling Interrupt */
59+
rtc.attachInterrupt(alarmAMatch);
60+
61+
/* Program the AlarmA in a 12 seconds */
62+
rtc.setAlarmDay(day);
63+
rtc.setAlarmTime(hours, minutes, seconds + 12);
64+
rtc.enableAlarm(rtc.MATCH_DHHMMSS);
65+
Serial.printf("Set Alarm A in 12s (at %02d:%02d:%02d)\r\n",
66+
rtc.getAlarmHours(), rtc.getAlarmMinutes(), rtc.getAlarmSeconds());
67+
68+
#ifdef RTC_ALARM_B
69+
/* Program ALARM B in ALARM_DELAY_MS ms from now (keep timeout < 1000ms) */
70+
timeout = rtc.getSubSeconds() + ALARM_DELAY_MS;
71+
72+
rtc.attachInterrupt(alarmBMatch, STM32RTC::ALARM_B);
73+
rtc.setAlarmSubSeconds(timeout, STM32RTC::ALARM_B);
74+
rtc.enableAlarm(rtc.MATCH_SUBSEC, STM32RTC::ALARM_B);
75+
Serial.printf("Set Alarm B (in %d ms) at %d ms\r\n", ALARM_DELAY_MS,
76+
rtc.getAlarmSubSeconds(STM32RTC::ALARM_B));
77+
#endif
78+
79+
}
80+
81+
void loop()
82+
{
83+
84+
}
85+
86+
void alarmAMatch(void *data)
87+
{
88+
UNUSED(data);
89+
Serial.printf("Alarm A Match at %02d:%02d:%02d\r\n",
90+
rtc.getHours(), rtc.getMinutes(), rtc.getSeconds());
91+
}
92+
93+
void alarmBMatch(void *data)
94+
{
95+
UNUSED(data);
96+
Serial.printf("Alarm B Match at %d ms\r\n", rtc.getSubSeconds());
97+
}
98+

0 commit comments

Comments
 (0)