forked from raspberrypi/pico-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtc_alarm_repeat.c
61 lines (51 loc) · 1.24 KB
/
rtc_alarm_repeat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* Copyright (c) 2021 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <stdio.h>
#include "hardware/rtc.h"
#include "pico/stdlib.h"
#include "pico/util/datetime.h"
static volatile bool fired = false;
static void alarm_callback(void) {
datetime_t t = {0};
rtc_get_datetime(&t);
char datetime_buf[256];
char *datetime_str = &datetime_buf[0];
datetime_to_str(datetime_str, sizeof(datetime_buf), &t);
printf("Alarm Fired At %s\n", datetime_str);
stdio_flush();
fired = true;
}
int main() {
stdio_init_all();
printf("RTC Alarm Repeat!\n");
// Start on Wednesday 13th January 2021 11:20:00
datetime_t t = {
.year = 2020,
.month = 01,
.day = 13,
.dotw = 3, // 0 is Sunday, so 3 is Wednesday
.hour = 11,
.min = 20,
.sec = 00
};
// Start the RTC
rtc_init();
rtc_set_datetime(&t);
// Alarm once a minute
datetime_t alarm = {
.year = -1,
.month = -1,
.day = -1,
.dotw = -1,
.hour = -1,
.min = -1,
.sec = 00
};
rtc_set_alarm(&alarm, &alarm_callback);
// Alarm will keep firing forever
while(1);
return 0;
}