Skip to content

Commit e66f1d3

Browse files
committed
chore(examples): RTC running in BINary mode with Alarm
This examples is configuring the RTC in BIN mode (binary only) and sets Alarm A & B (if exists) a few ms after the current time. Signed-off-by: Francois Ramu <[email protected]>
1 parent a3cf418 commit e66f1d3

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
mode BINary only RTC alarm
3+
4+
This sketch shows how to configure the alarm A & B of the RTC in BIN mode
5+
6+
Creation 12 Dec 2017
7+
by Wi6Labs
8+
Modified 03 Jul 2020
9+
by Frederic Pillon for STMicroelectronics
10+
Modified 03 sept 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+
uint32_t timeout;
24+
25+
void setup()
26+
{
27+
Serial.begin(115200);
28+
29+
// Select RTC clock source: LSI_CLOCK, LSE_CLOCK or HSE_CLOCK.
30+
rtc.setClockSource(STM32RTC::LSE_CLOCK);
31+
32+
/* Configure the RTC mode */
33+
rtc.setBinaryMode(STM32RTC::MODE_BIN);
34+
35+
/* in BIN mode time and Date register are not used, only the subsecond register for milisseconds */
36+
rtc.begin(true, STM32RTC::HOUR_24);
37+
38+
/* wait for a while */
39+
delay(300);
40+
41+
/* subsecond expressed in milliseconds */
42+
Serial.printf("Start at %d ms \r\n", rtc.getSubSeconds());
43+
44+
/* Attach the callback function before enabling Interrupt */
45+
rtc.attachInterrupt(alarmAMatch);
46+
47+
/* Program the AlarmA in 12 seconds */
48+
rtc.setAlarmTime(0, 0, 0, 12000);
49+
rtc.enableAlarm(rtc.MATCH_SUBSEC);
50+
Serial.printf("Set Alarm A in 12s (at %d ms)\r\n", rtc.getAlarmSubSeconds());
51+
52+
#ifdef RTC_ALARM_B
53+
/* Program ALARM B in 600ms ms from now (keep timeout < 1000ms) */
54+
timeout = rtc.getSubSeconds() + 600;
55+
56+
rtc.attachInterrupt(alarmBMatch, STM32RTC::ALARM_B);
57+
rtc.setAlarmSubSeconds(timeout, STM32RTC::ALARM_B);
58+
rtc.enableAlarm(rtc.MATCH_SUBSEC, STM32RTC::ALARM_B);
59+
Serial.printf("Set Alarm B (in %d ms) at %d ms\r\n", 600,
60+
rtc.getAlarmSubSeconds(STM32RTC::ALARM_B));
61+
#endif /* RTC_ALARM_B */
62+
63+
}
64+
65+
void loop()
66+
{
67+
68+
}
69+
70+
void alarmAMatch(void *data)
71+
{
72+
UNUSED(data);
73+
rtc.disableAlarm(STM32RTC::ALARM_A);
74+
Serial.printf("Alarm A Match at %d ms \r\n", rtc.getSubSeconds());
75+
}
76+
77+
#ifdef RTC_ALARM_B
78+
void alarmBMatch(void *data)
79+
{
80+
UNUSED(data);
81+
rtc.disableAlarm(STM32RTC::ALARM_B); /* Else it will trig again */
82+
Serial.printf("Alarm B Match at %d ms\r\n", rtc.getSubSeconds());
83+
}
84+
#endif /* RTC_ALARM_B */
85+

0 commit comments

Comments
 (0)