hardware-rtc
硬件实时时钟 API。
详细描述
RTC 以人类可读的格式跟踪时间,并在时间等于预设值时生成事件。可以将其理解为数字时钟,而非大多数计算机使用的 epoch 时间。共有七个字段,分别对应年(12 位)、月(4 位)、日(5 位)、星期(3 位)、时(5 位)、分(6 位)和秒(6 位),数据以二进制格式存储。
另请参阅
示例
hello_rtc.c
#include <stdio.h>
#include "hardware/rtc.h"
#include "pico/stdlib.h"
#include "pico/util/datetime.h"
int main() {
stdio_init_all();
printf("Hello RTC!\n");
char datetime_buf[256];
char *datetime_str = &datetime_buf[0];
// Start on Friday 5th of June 2020 15:45:00
datetime_t t = {
.year = 2020,
.month = 06,
.day = 05,
.dotw = 5, // 0 is Sunday, so 5 is Friday
.hour = 15,
.min = 45,
.sec = 00
};
// Start the RTC
rtc_init();
rtc_set_datetime(&t);
// clk_sys is >2000x faster than clk_rtc, so datetime is not updated immediately when rtc_get_datetime() is called.
// The delay is up to 3 RTC clock cycles (which is 64us with the default clock settings)
sleep_us(64);
// Print the time
while (true) {
rtc_get_datetime(&t);
datetime_to_str(datetime_str, sizeof(datetime_buf), &t);
printf("\r%s ", datetime_str);
sleep_ms(100);
}
}