STM32F4闹钟
比较F1的片子对于F4的RTC来说有很大提升,F4的RTC提供了日历时钟和两个可编程闹钟中断,一个周期性可编程唤醒标志。这样很方便设置系统时间,并不会像F1的RTC那样要自己计算年月日时分秒。F4的RTC是一个独立的BCD定时计数器,系统可以自动将月份天数补偿为28、29(闰年)、30、31天。并且还可以进行夏令时补偿。
这里主要说明FAL库闹钟配置结构体
/** * @brief RTC Time structure definition */ typedef struct { uint8_t Hours; //设置小时,如果选择RTC_HourFormat_12,取值范围【0-12】,如果选择RTC_HourFormat_24,取值范围【0-23】,HourFormat在RTC初始化中设置 uint8_t Minutes; //设置分钟,范围【0-59】 uint8_t Seconds; //设置秒钟,范围【0-59】 uint8_t TimeFormat; //格式设置,选择RTC_HOURFORMAT12_AM(代表AM或24小时格式)/RTC_HOURFORMAT12_PM(代表:PM) uint32_t SubSeconds; //亚秒设置 uint32_t SecondFraction; //亚秒因子 uint32_t DayLightSaving; //夏令时接口,已弃用 uint32_t StoreOperation; //夏令时接口,已弃用 }RTC_TimeTypeDef;
/** * @brief RTC Alarm structure definition */ typedef struct { RTC_TimeTypeDef AlarmTime; //设置时间 uint32_t AlarmMask; //闹钟掩码设置 //RTC_ALARMMASK_NONE,闹钟比较时匹配所有比较项目,包括日期(日/星期)、时、分、秒,完全匹配才会产生闹钟中断/事件 //RTC_ALARMMASK_DATEWEEKDAY,屏蔽日期(日/星期),闹钟比较时将不关心日期(日/星期) //RTC_ALARMMASK_HOURS,屏蔽时钟,闹钟比较时将不关心小时数 //RTC_ALARMMASK_MINUTES,屏蔽分钟,闹钟比较时将不关心分钟数 //RTC_ALARMMASK_SECONDS,屏蔽秒钟,闹钟比较时将不关心秒钟数 //RTC_ALARMMASK_ALL,屏蔽所有,设置后将不会产生闹钟中断/事件 uint32_t AlarmSubSecondMask; //亚秒掩码设置 //在RTC_Alarm_Sub_Seconds_Masks_Definitions中设置亚秒匹配规则 uint32_t AlarmDateWeekDaySel; //日期(日/星期)选择,此位只有当AlarmMask中不设置RTC_ALARMMASK_DATEWEEKDAY时才有效 //RTC_ALARMDATEWEEKDAYSEL_DATE,选择匹配日期 //RTC_ALARMDATEWEEKDAYSEL_WEEKDAY,选择匹配星期 uint8_t AlarmDateWeekDay; //日期(日/星期)设置,此位只有当AlarmMask中不设置RTC_ALARMMASK_DATEWEEKDAY时才有效 //如果AlarmDateWeekDaySel选择RTC_ALARMDATEWEEKDAYSEL_DATE,该值范围【1-31】,代表设置一个月当中的第几号 //如果AlarmDateWeekDaySel选择RTC_ALARMDATEWEEKDAYSEL_WEEKDAY,该值范围在RTC_WeekDay_Definitions定义,代表设置一周当中的星期几 uint32_t Alarm; //设置闹钟 //RTC_ALARM_A,选择闹钟A //RTC_ALARM_B,选择闹钟B }RTC_AlarmTypeDef;
比如设置一个闹钟,在每个月10号,10点10分10秒时产生闹钟事件,则设置如下:
RTC_AlarmTypeDef sAlarm = {0}; sAlarm.AlarmTime.Hours = 10; sAlarm.AlarmTime.Minutes = 10; sAlarm.AlarmTime.Seconds = 10; sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET; sAlarm.AlarmMask = RTC_ALARMMASK_NONE; //不屏蔽日期比较 sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE; //选择匹配日期 sAlarm.AlarmDateWeekDay = 10; //设置10号 sAlarm.Alarm = RTC_ALARM_A; HAL_RTC_SetAlarm_IT(&RTC_Handler, &sAlarm, RTC_FORMAT_BIN);
比如设置一个闹钟,在每周星期五,10点10分10秒时产生闹钟事件,则设置如下:
RTC_AlarmTypeDef sAlarm = {0}; sAlarm.AlarmTime.Hours = 10; sAlarm.AlarmTime.Minutes = 10; sAlarm.AlarmTime.Seconds = 10; sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET; sAlarm.AlarmMask = RTC_ALARMMASK_NONE; //不屏蔽日期比较 sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_WEEKDAY; //选择匹配星期 sAlarm.AlarmDateWeekDay = RTC_WEEKDAY_FRIDAY; //设置星期五 sAlarm.Alarm = RTC_ALARM_A; HAL_RTC_SetAlarm_IT(&RTC_Handler, &sAlarm, RTC_FORMAT_BIN);
比如设置一个闹钟,在每天10点10分10秒时产生闹钟事件,则设置如下:
RTC_AlarmTypeDef sAlarm = {0}; sAlarm.AlarmTime.Hours = 10; sAlarm.AlarmTime.Minutes = 10; sAlarm.AlarmTime.Seconds = 10; sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET; sAlarm.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY; //屏蔽日期/星期比较 sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_WEEKDAY; //日期/星期设置无关 sAlarm.AlarmDateWeekDay = RTC_WEEKDAY_FRIDAY; //日期/星期设置无关 sAlarm.Alarm = RTC_ALARM_A; HAL_RTC_SetAlarm_IT(&RTC_Handler, &sAlarm, RTC_FORMAT_BIN);