主要初始化函数,以及设置闹钟函数,闹钟中断函数
RTC.c
#include "rtc.h"
#include "delay.h"
//更改这个值,时间与日期将重新初始化
#define BKP 24010
void Rtc_Init(void)
{
RTC_InitTypeDef RTC_InitStruct;
RTC_TimeTypeDef RTC_TimeStruct;
RTC_DateTypeDef RTC_DateStruct;
//1、使能PWR时钟:
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
//2、使能后备寄存器访问。一个断电保存数据的寄存器
PWR_BackupAccessCmd(ENABLE);
//判断后备寄存器的值
if(RTC_ReadBackupRegister(RTC_BKP_DR0) != BKP)
{
//3、配置RTC时钟源,使能RTC时钟:
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
RCC_RTCCLKCmd(ENABLE);
//如果使用LSE,要打开LSE:
RCC_LSEConfig(RCC_LSE_ON);
RTC_InitStruct.RTC_HourFormat = RTC_HourFormat_24; //24小时制
RTC_InitStruct.RTC_AsynchPrediv = 0x7F; //128分频 异步分频
RTC_InitStruct.RTC_SynchPrediv = 0xFF; //256分频 同步分频
//4、 初始化RTC(同步/异步分频系数和时钟格式):
RTC_Init(&RTC_InitStruct);
RTC_TimeStruct.RTC_H12 = RTC_H12_PM; //下午
RTC_TimeStruct.RTC_Hours = 19;//时
RTC_TimeStruct.RTC_Minutes = 57;//分
RTC_TimeStruct.RTC_Seconds = 00;//秒
//5、 设置时间:
RTC_SetTime(RTC_Format_BIN, &RTC_TimeStruct);
RTC_DateStruct.RTC_Year = 24; //年
RTC_DateStruct.RTC_Month = 1; //月
RTC_DateStruct.RTC_Date = 3; //日
RTC_DateStruct.RTC_WeekDay = 3; //星期
//6、设置日期:
RTC_SetDate(RTC_Format_BIN, &RTC_DateStruct);
//将值写入后备寄存器
RTC_WriteBackupRegister(RTC_BKP_DR0,BKP);
}
}
//闹钟A初始化
void RTC_Alarm_A_Init(void)
{
RTC_AlarmTypeDef RTC_AlarmStruct;
RTC_TimeTypeDef RTC_AlarmTimeSet;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//关闭闹钟:
RTC_AlarmCmd(RTC_Alarm_A,DISABLE);
RTC_AlarmTimeSet.RTC_H12 = RTC_H12_PM; //下午
RTC_AlarmTimeSet.RTC_Hours = 16;//时
RTC_AlarmTimeSet.RTC_Minutes = 37;//分
RTC_AlarmTimeSet.RTC_Seconds = 15;//秒
RTC_AlarmStruct.RTC_AlarmTime = RTC_AlarmTimeSet; //闹钟时间设置
RTC_AlarmStruct.RTC_AlarmMask = RTC_AlarmMask_None; //无掩码位 按实际时间响应闹钟
RTC_AlarmStruct.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date;//按日期进行响应闹钟
RTC_AlarmStruct.RTC_AlarmDateWeekDay = 3; //3号
//3、配置闹钟参数:
RTC_SetAlarm(RTC_Format_BIN, RTC_Alarm_A, &RTC_AlarmStruct);
EXTI_InitStructure.EXTI_Line = EXTI_Line17; //中断/事件线
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //中断模式
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //上升沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE; //中断线使能
//配置NVIC
EXTI_Init(&EXTI_InitStructure);
/* Enable and set EXTI Line0 Interrupt to the lowest priority */
NVIC_InitStructure.NVIC_IRQChannel = RTC_Alarm_IRQn; //中断通道,中断通道在stm32f4xx.h查找
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01; //抢占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01; //响应优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //通道使能
NVIC_Init(&NVIC_InitStructure);
//开启配置闹钟中断:
RTC_ITConfig(RTC_IT_ALRA, ENABLE);
//开启闹钟:
RTC_AlarmCmd(RTC_Alarm_A,ENABLE);
}
//闹钟B初始化
void RTC_Alarm_B_Init(void)
{
RTC_AlarmTypeDef RTC_AlarmStruct;
RTC_TimeTypeDef RTC_AlarmTimeSet;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//关闭闹钟:
RTC_AlarmCmd(RTC_Alarm_B,DISABLE);
RTC_AlarmTimeSet.RTC_H12 = RTC_H12_PM; //下午
RTC_AlarmTimeSet.RTC_Hours = 16;//时
RTC_AlarmTimeSet.RTC_Minutes = 37;//分
RTC_AlarmTimeSet.RTC_Seconds = 25;//秒
RTC_AlarmStruct.RTC_AlarmTime = RTC_AlarmTimeSet; //闹钟时间设置
RTC_AlarmStruct.RTC_AlarmMask = RTC_AlarmMask_None; //无掩码位 按实际时间响应闹钟
RTC_AlarmStruct.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date;//按日期进行响应闹钟
RTC_AlarmStruct.RTC_AlarmDateWeekDay = 3; //3号
//3、配置闹钟参数:
RTC_SetAlarm(RTC_Format_BIN, RTC_Alarm_B, &RTC_AlarmStruct);
EXTI_InitStructure.EXTI_Line = EXTI_Line17; //中断/事件线
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //中断模式
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //上升沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE; //中断线使能
//配置NVIC
EXTI_Init(&EXTI_InitStructure);
/* Enable and set EXTI Line0 Interrupt to the lowest priority */
NVIC_InitStructure.NVIC_IRQChannel = RTC_Alarm_IRQn; //中断通道,中断通道在stm32f4xx.h查找
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01; //抢占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01; //响应优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //通道使能
NVIC_Init(&NVIC_InitStructure);
//开启配置闹钟中断:
RTC_ITConfig(RTC_IT_ALRB, ENABLE);
//开启闹钟:
RTC_AlarmCmd(RTC_Alarm_B,ENABLE);
}
void RTC_Alarm_IRQHandler(void)
{
//判断中断标志位是否置1
if(EXTI_GetITStatus(EXTI_Line17) == SET)
{
if(RTC_GetFlagStatus(RTC_FLAG_ALRAF) == SET)
{
GPIO_ResetBits(GPIOF, GPIO_Pin_9);
RTC_ClearFlag(RTC_FLAG_ALRAF);
}
if(RTC_GetFlagStatus(RTC_FLAG_ALRBF) == SET)
{
GPIO_SetBits(GPIOF, GPIO_Pin_9);
RTC_ClearFlag(RTC_FLAG_ALRBF);
}
//清除中断标志位
EXTI_ClearITPendingBit(EXTI_Line17);
}
}
RTC.h文章来源:https://www.toymoban.com/news/detail-856391.html
#ifndef __RTC_H
#define __RTC_H
#include "stm32f4xx.h"
void Rtc_Init(void);
void RTC_Alarm_A_Init(void);
void RTC_Alarm_B_Init(void);
void RTC_Alarm_set(uint32_t Alarm_X ,uint32_t IT_ALRX, uint8_t Hours, uint8_t Minutes , uint8_t Seconds , uint8_t AlarmDateWeekDay);
#endif
main.c文章来源地址https://www.toymoban.com/news/detail-856391.html
Rtc_Init();
RTC_Alarm_A_Init();
RTC_Alarm_B_Init();
//设置闹钟时间
RTC_Alarm_set(RTC_Alarm_A,RTC_IT_ALRA,17,02,5,3);
while(1)
{
RTC_GetDate(RTC_Format_BIN, &RTC_DateStruct);
RTC_GetTime(RTC_Format_BIN, &RTC_TimeStruct);
printf("日期:%d年%d月%d日\r\n",RTC_DateStruct.RTC_Year, RTC_DateStruct.RTC_Month, RTC_DateStruct.RTC_Date);
printf("时间:%d:%d:%d\r\n",RTC_TimeStruct.RTC_Hours, RTC_TimeStruct.RTC_Minutes, RTC_TimeStruct.RTC_Seconds);
到了这里,关于STM32中的RTC实时时钟和配套闹钟设置的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!