Skip to content

Commit 4be30fe

Browse files
committed
new example to demonstrate the seconds interrupt
enable the second interrupt and display time each second, thanks to its callback function Raise an error if the sketch not running on the devices (not stm32F1 and no wakeUp interrupt available) Signed-off-by: Francois Ramu <francois.ramu@st.com>
1 parent faddd19 commit 4be30fe

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

examples/RTC_Seconds/RTC_Seconds.ino

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
RTC_Seconds
3+
4+
This sketch allows to test STM32RTC Seconds IRQ.
5+
6+
Creation 25 nov 2021
7+
by FRASTM for STMicroelectronics
8+
9+
This example code is in the public domain.
10+
11+
Note that this sketch is valid for STM32F1xx or stm32 MCU with WakeUp interrupt
12+
(WUTE flag present in the RTC CR register)
13+
14+
https://github.com/stm32duino/STM32RTC
15+
16+
*/
17+
18+
#include <STM32RTC.h>
19+
20+
#ifndef ONESECOND_IRQn
21+
#error "RTC has no feature for One-Second interrupt"
22+
#endif
23+
24+
/* use led to display seconds */
25+
#if defined(LED_BUILTIN)
26+
#define pin LED_BUILTIN
27+
#endif
28+
29+
/* Get the rtc object */
30+
STM32RTC& rtc = STM32RTC::getInstance();
31+
32+
/* Change these values to set the current initial time
33+
34+
format: date: "Dec 31 2017" and time: "23:59:56"
35+
by default use built date and time
36+
*/
37+
/* Change these values to set the current initial time */
38+
const byte seconds = 0;
39+
const byte minutes = 5;
40+
const byte hours = 11;
41+
42+
/* Change these values to set the current initial date */
43+
/* Monday 25 Nov. 2021 */
44+
const byte weekDay = 4;
45+
const byte day = 25;
46+
const byte month = 11;
47+
const byte year = 21;
48+
49+
bool toggling = false; // changed each second by the CallBack function
50+
51+
static STM32RTC::Hour_Format hourFormat = STM32RTC::HOUR_24;
52+
static STM32RTC::AM_PM period = STM32RTC::AM;
53+
54+
void setup()
55+
{
56+
Serial.begin(115200);
57+
while (!Serial) {}
58+
59+
#if defined(LED_BUILTIN)
60+
// configure pin in output mode
61+
pinMode(pin, OUTPUT);
62+
digitalWrite(pin, HIGH);
63+
#endif /* LED_BUILTIN */
64+
65+
Serial.print("RTC Init ");
66+
67+
// Select RTC clock source: LSI_CLOCK, LSE_CLOCK or HSE_CLOCK.
68+
// By default the LSI is selected as source. Use LSE for better accuracy if available
69+
// rtc.setClockSource(STM32RTC::LSE_CLOCK);
70+
71+
// initialize RTC 24H format
72+
rtc.begin(hourFormat);
73+
74+
// Set the time
75+
rtc.setHours(hours, period);
76+
rtc.setMinutes(minutes);
77+
rtc.setSeconds(seconds);
78+
79+
// Set the date
80+
rtc.setWeekDay(weekDay);
81+
rtc.setDay(day);
82+
rtc.setMonth(month);
83+
rtc.setYear(year);
84+
85+
Serial.println("with seconds Alarm");
86+
rtc.attachSecondsInterrupt(rtc_SecondsCB);
87+
}
88+
89+
void loop()
90+
{
91+
uint8_t sec = 0;
92+
uint8_t mn = 0;
93+
uint8_t hrs = 0;
94+
uint32_t subs = 0;
95+
rtc.getTime(&hrs, &mn, &sec, &subs);
96+
97+
if (toggling) {
98+
Serial.printf("%02d:%02d:%02d\n", hrs, mn, sec);
99+
} else {
100+
Serial.printf("%02d %02d %02d\n", hrs, mn, sec);
101+
}
102+
#if defined(LED_BUILTIN)
103+
digitalWrite(pin, toggling);
104+
#endif /* LED_BUILTIN */
105+
delay(1000);
106+
}
107+
108+
/* callback function on each second interrupt */
109+
void rtc_SecondsCB(void *data)
110+
{
111+
UNUSED(data);
112+
toggling = !toggling;
113+
}

0 commit comments

Comments
 (0)