|
| 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 | + https://github.com/stm32duino/STM32RTC |
| 12 | +
|
| 13 | +*/ |
| 14 | + |
| 15 | +#include <STM32RTC.h> |
| 16 | + |
| 17 | +/* use led to display seconds */ |
| 18 | +#if defined(LED_BUILTIN) |
| 19 | +#define pin LED_BUILTIN |
| 20 | +#endif |
| 21 | + |
| 22 | +/* Get the rtc object */ |
| 23 | +STM32RTC& rtc = STM32RTC::getInstance(); |
| 24 | + |
| 25 | +/* Change these values to set the current initial time |
| 26 | +
|
| 27 | + format: date: "Dec 31 2017" and time: "23:59:56" |
| 28 | + by default use built date and time |
| 29 | +*/ |
| 30 | +/* Change these values to set the current initial time */ |
| 31 | +const byte seconds = 0; |
| 32 | +const byte minutes = 5; |
| 33 | +const byte hours = 11; |
| 34 | + |
| 35 | +/* Change these values to set the current initial date */ |
| 36 | +/* Monday 25 Nov. 2021 */ |
| 37 | +const byte weekDay = 4; |
| 38 | +const byte day = 25; |
| 39 | +const byte month = 11; |
| 40 | +const byte year = 21; |
| 41 | + |
| 42 | +bool toggling = false; // changed each second by the CallBack function |
| 43 | + |
| 44 | +static STM32RTC::Hour_Format hourFormat = STM32RTC::HOUR_24; |
| 45 | +static STM32RTC::AM_PM period = STM32RTC::AM; |
| 46 | + |
| 47 | +void setup() |
| 48 | +{ |
| 49 | + Serial.begin(115200); |
| 50 | + while (!Serial) {} |
| 51 | + |
| 52 | +#if defined(LED_BUILTIN) |
| 53 | + // configure pin in output mode |
| 54 | + pinMode(pin, OUTPUT); |
| 55 | + digitalWrite(pin, HIGH); |
| 56 | +#endif /* LED_BUILTIN */ |
| 57 | + |
| 58 | + Serial.print("RTC Init "); |
| 59 | + |
| 60 | + // Select RTC clock source: LSI_CLOCK, LSE_CLOCK or HSE_CLOCK. |
| 61 | + // By default the LSE is selected as source for better accuracy. |
| 62 | + rtc.setClockSource(STM32RTC::LSE_CLOCK); |
| 63 | + |
| 64 | + // initialize RTC 24H format |
| 65 | + rtc.begin(hourFormat); |
| 66 | + |
| 67 | + // Set the time |
| 68 | + rtc.setHours(hours, period); |
| 69 | + rtc.setMinutes(minutes); |
| 70 | + rtc.setSeconds(seconds); |
| 71 | + |
| 72 | + // Set the date |
| 73 | + rtc.setWeekDay(weekDay); |
| 74 | + rtc.setDay(day); |
| 75 | + rtc.setMonth(month); |
| 76 | + rtc.setYear(year); |
| 77 | + |
| 78 | + Serial.print("with seconds Alarm \r\n"); |
| 79 | + rtc.attachSecondsInterrupt(rtc_SecondsCB); |
| 80 | +} |
| 81 | + |
| 82 | +void loop() |
| 83 | +{ |
| 84 | + if (toggling) { |
| 85 | + Serial.printf("%02d:%02d:%02d \r", rtc.getHours(), rtc.getMinutes(), rtc.getSeconds()); |
| 86 | + } else { |
| 87 | + Serial.printf("%02d %02d %02d \r", rtc.getHours(), rtc.getMinutes(), rtc.getSeconds()); |
| 88 | + } |
| 89 | +#if defined(LED_BUILTIN) |
| 90 | + digitalWrite(pin, toggling); |
| 91 | +#endif /* LED_BUILTIN */ |
| 92 | + delay(1000); |
| 93 | +} |
| 94 | + |
| 95 | +/* callback function on each second interrupt */ |
| 96 | +void rtc_SecondsCB(void *data) |
| 97 | +{ |
| 98 | + UNUSED(data); |
| 99 | + toggling = !toggling; |
| 100 | +} |
0 commit comments