STM32 | RTC闹钟实战

avatar
作者
筋斗云
阅读量:0

点击上方"蓝字"关注我们

01、rtc.h

#ifndef __RTC_H#define __RTC_H#include "stm32f4xx.h"#include "delay.h"#include "led.h"void Rtc_My_Init(void);void RTC_Alarm_MyA(void);#endif

02、rtc.c

#include "rtc.h"#define BKP      0x2022void Rtc_My_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); //选择LSE时钟源    RCC_RTCCLKCmd(ENABLE);    //如果使用LSE,要打开LSE:    RCC_LSEConfig(RCC_LSE_ON);    //延时1小段时间,等待系统稳定    delay_ms(50);        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;    //下午  对于24小时制度,此参数可不写    RTC_TimeStruct.RTC_Hours  = 13;        //时    RTC_TimeStruct.RTC_Minutes  = 1;        //分    RTC_TimeStruct.RTC_Seconds  = 1;        //秒    RTC_SetTime(RTC_Format_BIN, &RTC_TimeStruct);        RTC_DateStruct.RTC_Year    = 21;          //年    RTC_DateStruct.RTC_Month  = 12;          //月    RTC_DateStruct.RTC_Date    = 18;          //日    RTC_DateStruct.RTC_WeekDay  = RTC_Weekday_Saturday;  //星期六    RTC_SetDate(RTC_Format_BIN, &RTC_DateStruct);      RTC_WriteBackupRegister(RTC_BKP_DR0, BKP);  }}void RTC_Alarm_MyA(void){     RTC_TimeTypeDef   RTC_AlarmTime;  RTC_AlarmTypeDef  RTC_AlarmStruct;  EXTI_InitTypeDef    EXTI_InitStruct;  NVIC_InitTypeDef    NVIC_InitStruct;        //关闭闹钟:  RTC_AlarmCmd(RTC_Alarm_A,DISABLE);     RTC_AlarmTime.RTC_H12    = RTC_H12_PM;    //下午  对于24小时制度,此参数可不写  RTC_AlarmTime.RTC_Hours    = 13;          //时  RTC_AlarmTime.RTC_Minutes  = 1;          //分  RTC_AlarmTime.RTC_Seconds  = 45;          //秒      RTC_AlarmStruct.RTC_AlarmTime       = RTC_AlarmTime;      // 时间设置  RTC_AlarmStruct.RTC_AlarmMask        = RTC_AlarmMask_None;  // 无掩码位  RTC_AlarmStruct.RTC_AlarmDateWeekDaySel  = RTC_AlarmDateWeekDaySel_Date; //按日期来响应闹钟  RTC_AlarmStruct.RTC_AlarmDateWeekDay  = 18;          //18号    //配置闹钟参数:  RTC_SetAlarm(RTC_Format_BIN, RTC_Alarm_A, &RTC_AlarmStruct);      //开启闹钟:  RTC_AlarmCmd(RTC_Alarm_A, ENABLE);  //开启配置闹钟中断:  RTC_ITConfig(RTC_IT_ALRA, ENABLE);     EXTI_InitStruct.EXTI_Line  = EXTI_Line17;      //中断线17  EXTI_InitStruct.EXTI_Mode  = EXTI_Mode_Interrupt;  //中断模式  EXTI_InitStruct.EXTI_Trigger= EXTI_Trigger_Rising;  //上升沿触发  EXTI_InitStruct.EXTI_LineCmd= ENABLE;        //中断使能  //初始化线上中断,设置触发条件等。  EXTI_Init(&EXTI_InitStruct);    

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!