Skip to content

Commit 50ffe71

Browse files
committed
add new sketch for 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 <francois.ramu@st.com>
1 parent 3b9a253 commit 50ffe71

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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(200);
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 400ms ms from now (keep timeout < 1000ms) */
54+
timeout = rtc.getSubSeconds() + 400;
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", 400,
60+
rtc.getAlarmSubSeconds(STM32RTC::ALARM_B));
61+
#endif
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+
void alarmBMatch(void *data)
78+
{
79+
UNUSED(data);
80+
rtc.disableAlarm(STM32RTC::ALARM_B); /* Else it will trig again */
81+
Serial.printf("Alarm B Match at %d ms\r\n", rtc.getSubSeconds());
82+
}
83+

0 commit comments

Comments
 (0)