Skip to content

Commit 22a4c0c

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 some devices Signed-off-by: Francois Ramu <francois.ramu@st.com>
1 parent 0ab74f8 commit 22a4c0c

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 not valid for STM32F030x4/6/8 and STM32F070x6
12+
13+
https://github.com/stm32duino/STM32RTC
14+
15+
*/
16+
17+
#include <STM32RTC.h>
18+
19+
#if defined(STM32F0xx) && !(defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx)\
20+
| defined(STM32F091xC) || defined(STM32F098xx) || defined(STM32F070xB) || defined(STM32F030xC))
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+
// The LSE is selected as source for better accuracy.
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.print("with seconds Alarm \r\n");
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 \r", hrs, mn, sec);
99+
} else {
100+
Serial.printf("%02d %02d %02d \r", 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)