Skip to content

Commit 93d6346

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 ticks after the current time, whenAlarm A is set in calendar mode. Mainly used on stm32wl55 devices.
1 parent 7abbe02 commit 93d6346

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

examples/mixRTCAlarm/mixRTCAlarm.ino

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
mixRTCAlarm
3+
4+
This sketch shows how to configure the RTC alarm 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 = 05;
25+
const byte minutes = 14;
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 = 9;
31+
const byte year = 15;
32+
33+
uint32_t timenow;
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+
// By default the LSI is selected as source : choose LSE
42+
rtc.setClockSource(STM32RTC::LSE_CLOCK);
43+
#if defined(RTC_BINARY_NONE)
44+
rtc.setRTCMode(STM32RTC::MODE_MIX);
45+
#endif /* RTC_BINARY_NONE */
46+
rtc.begin(false); // initialize RTC 24H format
47+
48+
rtc.setTime(hours, minutes, seconds);
49+
rtc.setDate(day, month, year);
50+
51+
rtc.attachInterrupt(alarmAMatch, STM32RTC::ALARM_A);
52+
#ifdef RTC_ALARM_B
53+
rtc.attachInterrupt(alarmBMatch, STM32RTC::ALARM_B);
54+
#endif /* RTC_ALARM_B */
55+
56+
/* wait for a while */
57+
for (uint32_t i = 0 ; i < 0x8ffff ; i++);
58+
59+
timenow = rtc.getSubSeconds();
60+
Serial.printf("start at subsec = %d\r\n", timenow);
61+
62+
/* program Alarm A in calendar mode + 5 seconds */
63+
rtc.setAlarmDay(day, STM32RTC::ALARM_A);
64+
rtc.setAlarmTime(hours, minutes, seconds + 5, STM32RTC::ALARM_A);
65+
rtc.enableAlarm(rtc.MATCH_DHHMMSS, STM32RTC::ALARM_A);
66+
67+
Serial.printf("set Alarm A \r\n");
68+
69+
#ifdef RTC_ALARM_B
70+
/* program Alarm B at timeout + ms from now */
71+
timeout = timenow + 300; /* for ex. 4096 (0x1000) is 16 seconds with a fqce_APRE*/
72+
73+
rtc.setAlarmSubSeconds(timeout, STM32RTC::ALARM_B);
74+
rtc.enableAlarm(rtc.MATCH_SUBSEC, STM32RTC::ALARM_B);
75+
76+
Serial.printf("set Alarm B at = %d\r\n", timeout);
77+
#endif /* RTC_ALARM_B */
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 subsec = %d\r\n", rtc.getSubSeconds());
90+
}
91+
92+
#ifdef RTC_ALARM_B
93+
void alarmBMatch(void *data)
94+
{
95+
UNUSED(data);
96+
Serial.printf("Alarm B Match! at subsec = %d\r\n", rtc.getSubSeconds());
97+
}
98+
#endif /* RTC_ALARM_B */
99+

0 commit comments

Comments
 (0)