Files
CHJ/user/MCU/lhl_rtc.c
2026-03-20 21:19:04 +08:00

65 lines
2.2 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "../main/SystemInclude.h"
void RTC_SEC_Init(void)
{
RTC_InitTypeDef RTC_InitStructure;
/* 1. 初始化RTC参数 */
RTC_InitStructure.ClockSource = RCC_RTCCLKSource_LSI; // RTC时钟源LSI
RTC_InitStructure.Prescaler = 32767; // RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) = 1S
RTC_InitStructure.Counter = 0; // 从0开始计数
RTC_InitStructure.Alarm = 10; // 闹钟时间10S
LHL_RTC_Init(&RTC_InitStructure);
/* 2. 开启秒中断 */
LHL_RTC_ITConfig(RTC_IT_SECIE, ENABLE);
NVIC_EnableIRQ(RTC_IRQn); // 秒中断和溢出中断在RTC_IRQn处理
}
void RTC_ALR_Init(void)
{
RTC_InitTypeDef RTC_InitStructure;
/* 1. 初始化RTC参数 */
RTC_InitStructure.ClockSource = RCC_RTCCLKSource_LSI; // RTC时钟源LSI
RTC_InitStructure.Prescaler = 32767; // RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) = 1S
RTC_InitStructure.Counter = 0; // 从0开始计数
RTC_InitStructure.Alarm = 5-1; // 闹钟时间5S
LHL_RTC_Init(&RTC_InitStructure);
/* 2. 开启闹钟 */
LHL_RTC_ITConfig(RTC_IT_ALRIE, ENABLE);
NVIC_EnableIRQ(RTCAlarm_IRQn); // 独立开启闹钟中断则在RTCAlarm_IRQn处理
}
/* 不建议同时开启秒中断和闹钟中断 */
//秒中断
void RTC_IRQHandler(void)
{
uint32_t Time_Count;
if (LHL_RTC_GetPending(RTC_FLAG_SECF) == SET)
{
// 清除RTC秒中断标志
LHL_RTC_ClearPending(RTC_FLAG_SECF);
// 获取当前RTC 计数值并打印
LHL_RTC_GetCounter(&Time_Count);
// printf("%d\n", Time_Count);
}
}
//闹钟中断
void RTCAlarm_IRQHandler(void)
{
uint32_t Time_Count;
if (LHL_RTC_GetPending(RTC_FLAG_ALRF) == SET)
{
LHL_RTC_ClearPending(RTC_FLAG_ALRF); // 清除RTC闹钟中断标志
LHL_RTC_GetCounter(&Time_Count); // 获取当前RTC 计数值并打印
LHL_RTC_SetAlarm(Time_Count+4); // 设置新的闹钟,当前时间+5S
// printf("Alarm is ringing: %d\n", Time_Count);
}
}