1 cubemx 关于RTC的配置:
(1)
(2)
在这里一定要启用方框内的低速外部时钟,不然RTC时钟无法启用(硬件上也需要接上低速外部时钟)
(3)
(4)
激活时钟源,激活日历,如果不使用秒中断可以不使用Alarm A,直接Disable.
接下来参数设置等。
(5)
"General"里面的设置RTC为1秒的时间。“Data Format” 选项意义可自行百度。hours,minutes,seconds时间可自行设置(如果要实现秒中断,"Alarm A"的"Seconds"应比"Calender Time"里面的"Seconds"多1)
"Alarm MASK Date Week day"到“Alarm MASK Seconds”全都屏蔽“Enable”进行屏蔽。
“Alarm Sub Second Mask”选择“Only SS[8:0] are compared”.
(6)
中断勾选上
(7)然后就可以生成代码了。
2:代码修改:
/* RTC init function */
void MX_RTC_Init(void)
{
/* USER CODE BEGIN RTC_Init 0 */
/* USER CODE END RTC_Init 0 */
RTC_TimeTypeDef sTime = {0};
RTC_DateTypeDef sDate = {0};
RTC_AlarmTypeDef sAlarm = {0};
/* USER CODE BEGIN RTC_Init 1 */
__HAL_RCC_PWR_CLK_ENABLE();
HAL_PWR_EnableBkUpAccess();
hrtc.Instance = RTC;
/* USER CODE END RTC_Init 1 */
/** Initialize RTC Only
*/
hrtc.Instance = RTC;
hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
hrtc.Init.AsynchPrediv = 127;
hrtc.Init.SynchPrediv = 255;
hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
if (HAL_RTC_Init(&hrtc) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN Check_RTC_BKUP */
if(HAL_RTCEx_BKUPRead(&hrtc,RTC_BKP_DR0)!=0x5A5A) /* 检查是否写入过一次RTC,保证掉电不丢失RTC时钟 */
{
/* USER CODE END Check_RTC_BKUP */
/** Initialize RTC and set the Time and Date
*/
sTime.Hours = 0x23;
sTime.Minutes = 0x55;
sTime.Seconds = 0x55;
sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sTime.StoreOperation = RTC_STOREOPERATION_RESET;
if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)
{
Error_Handler();
}
sDate.WeekDay = RTC_WEEKDAY_SUNDAY;
sDate.Month = RTC_MONTH_MARCH;
sDate.Date = 0x16;
sDate.Year = 0x19;
if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)
{
Error_Handler();
}
/** Enable the Alarm A
*/
sAlarm.AlarmTime.Hours = 0x23;
sAlarm.AlarmTime.Minutes = 0x55;
sAlarm.AlarmTime.Seconds = 0x56;
sAlarm.AlarmTime.SubSeconds = 0x0;
sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
sAlarm.AlarmMask = RTC_ALARMMASK_ALL;
sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_SS14_9;
sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
sAlarm.AlarmDateWeekDay = 0x1;
sAlarm.Alarm = RTC_ALARM_A;
if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BCD) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN RTC_Init 2 */
/* 写入寄存器 */
HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR0, 0x5A5A); //这里就是将这个寄存器的标志设为刚才的那个值,下次掉电后就不会进入到这里来
}
else
{
RTC_AlarmConfig(); /* RTC的中断配置初始化以及启动 */
}
/* USER CODE END RTC_Init 2 */
}
/* RTC的秒中断配置启动函数 */
void RTC_AlarmConfig(void)
{
RTC_AlarmTypeDef sAlarm = {0};
HAL_RTC_GetTime(&hrtc,&Now_Time,RTC_FORMAT_BCD);
HAL_RTC_GetDate(&hrtc,&Now_Date,RTC_FORMAT_BCD);
sAlarm.AlarmTime.Hours = 0x23;
sAlarm.AlarmTime.Minutes = 0x55;
sAlarm.AlarmTime.Seconds = Now_Time.Seconds+1;
sAlarm.AlarmTime.SubSeconds = 0x0;
sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
sAlarm.AlarmMask = RTC_ALARMMASK_ALL;
sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_SS14_9;
sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
sAlarm.AlarmDateWeekDay = 0x1;
sAlarm.Alarm = RTC_ALARM_A;
if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BCD) != HAL_OK)
{
Error_Handler();
}
}
/* RTC中断回调 */
void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
{
xxxxxxxx;
}
注意:硬件上要有纽扣电池给RTC供电
if(HAL_RTCEx_BKUPRead(&hrtc,RTC_BKP_DR0)!=0x5A5A) 用于检查RTC是否初始化过,在上电的时候就不用直接初始化,RTC时间就可以不用重置。保证关机RTC时间不重置。
RTC_AlarmConfig 每次开机的时候就会启动,保证秒中断产生。并进入到中断回调函数中去“HAL_RTC_AlarmAEventCallback”。