芯片:STM32F29IIGT6 阿波罗开发板
1.配置时钟
2.HAL外设配置
3.代码解析
生成rtc文件
RTC_HandleTypeDef hrtc;
/* RTC init function */
void MX_RTC_Init(void)
{
/* USER CODE BEGIN RTC_Init 0 */
RTC_TimeTypeDef rtc_time;
RTC_DateTypeDef rtc_data;
/* USER CODE END RTC_Init 0 */
RTC_TimeTypeDef sTime = {0};
RTC_DateTypeDef sDate = {0};
/* USER CODE BEGIN RTC_Init 1 */
/* 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 */
/* USER CODE END Check_RTC_BKUP */
/** Initialize RTC and set the Time and Date
*/
// sTime.Hours = 0x21;
// sTime.Minutes = 0x33;
// sTime.Seconds = 0x0;
// 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_FRIDAY;
// sDate.Month = RTC_MONTH_JUNE;
// sDate.Date = 0x2;
// sDate.Year = 0x23;
// if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)
// {
// Error_Handler();
// }
/** Enable the WakeUp
*/
__HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(&hrtc, RTC_FLAG_WUTF);
if (HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 0, RTC_WAKEUPCLOCK_CK_SPRE_16BITS) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN RTC_Init 2 */
/* USER CODE END RTC_Init 2 */
}
void HAL_RTC_MspInit(RTC_HandleTypeDef* rtcHandle)
{
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
if(rtcHandle->Instance==RTC)
{
/* USER CODE BEGIN RTC_MspInit 0 */
/* USER CODE END RTC_MspInit 0 */
/** Initializes the peripherals clock
*/
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
{
Error_Handler();
}
/* RTC clock enable */
__HAL_RCC_RTC_ENABLE();
/* RTC interrupt Init */
HAL_NVIC_SetPriority(RTC_WKUP_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(RTC_WKUP_IRQn);
/* USER CODE BEGIN RTC_MspInit 1 */
/* USER CODE END RTC_MspInit 1 */
}
}
void HAL_RTC_MspDeInit(RTC_HandleTypeDef* rtcHandle)
{
if(rtcHandle->Instance==RTC)
{
/* USER CODE BEGIN RTC_MspDeInit 0 */
/* USER CODE END RTC_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_RTC_DISABLE();
/* RTC interrupt Deinit */
HAL_NVIC_DisableIRQ(RTC_WKUP_IRQn);
/* USER CODE BEGIN RTC_MspDeInit 1 */
/* USER CODE END RTC_MspDeInit 1 */
}
}
生成的中断函数
/**
* @brief This function handles RTC wake-up interrupt through EXTI line 22.
*/
void RTC_WKUP_IRQHandler(void)
{
/* USER CODE BEGIN RTC_WKUP_IRQn 0 */
/* USER CODE END RTC_WKUP_IRQn 0 */
HAL_RTCEx_WakeUpTimerIRQHandler(&hrtc);
/* USER CODE BEGIN RTC_WKUP_IRQn 1 */
/* USER CODE END RTC_WKUP_IRQn 1 */
}
点击进 HAL_RTCEx_WakeUpTimerIRQHandler(&hrtc);
增加自我编写代码, 启动备份区数据,保护关机不掉时间
RTC_TimeTypeDef rtc_t; //时间
RTC_DateTypeDef rtc_d; //日期
//void RTC_Set_WakeUp(uint32_t wksel,uint16_t cnt);//配置WAKE UP中断,1秒钟中断一次
//!RTC时间设置
//hour,min,sec:小时,分钟,秒钟
//ampm:@RTC_AM_PM_Definitions:RTC_HOURFORMAT12_AM/RTC_HOURFORMAT12_PM
//返回值:SUCEE(1),成功
// ERROR(0),进入初始化模式失败
HAL_StatusTypeDef RTC_Set_Time(uint8_t hour, uint8_t min, uint8_t sec, uint8_t ampm)
{
RTC_TimeTypeDef RTC_TimeStructure;
RTC_TimeStructure.Hours = hour;
RTC_TimeStructure.Minutes = min;
RTC_TimeStructure.Seconds = sec;
RTC_TimeStructure.TimeFormat = ampm;
RTC_TimeStructure.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
RTC_TimeStructure.StoreOperation = RTC_STOREOPERATION_RESET;
return HAL_RTC_SetTime(&hrtc, &RTC_TimeStructure, RTC_FORMAT_BIN);
}
//!RTC日期设置
//year,month,date:年(0~99),月(1~12),日(0~31)
//week:星期(1~7,0,非法!)
//返回值:SUCEE(1),成功
// ERROR(0),进入初始化模式失败
HAL_StatusTypeDef RTC_Set_Date(uint8_t year,uint8_t month,uint8_t date,uint8_t week)
{
RTC_DateTypeDef RTC_DateStructure;
RTC_DateStructure.Date = date;
RTC_DateStructure.Month = month;
RTC_DateStructure.WeekDay = week;
RTC_DateStructure.Year = year;
return HAL_RTC_SetDate(&hrtc,&RTC_DateStructure,RTC_FORMAT_BIN);
}
/* RTC_HandleTypeDef hrtc; */
//RTC初始化
int RTC_AutoInit(void)
{
__HAL_RCC_PWR_CLK_ENABLE();//使能电源时钟PWR
HAL_PWR_EnableBkUpAccess();//取消备份区域写保护
COM_Printf("RTC_BKP_DR0 = %04X\r\n",HAL_RTCEx_BKUPRead(&hrtc,RTC_BKP_DR0));
if(HAL_RTCEx_BKUPRead(&hrtc,RTC_BKP_DR0)!=0xAA55) //检查备份区数据
{
RTC_Set_Time(8,12,0,RTC_HOURFORMAT12_AM); //写时间(时分秒,(上,下午))
RTC_Set_Date(23,6,1,4); //写日期(年月日,星期)
HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR0,0xAA55);
}
return 0;
}
//INIT_BOARD_EXPORT(RTC_AutoInit);
//RTC WAKE UP中断处理 (1秒回调一次读时间)
void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *phrtc)
{
// 处理唤醒定时器超时中断
HAL_RTC_GetTime(phrtc, &rtc_t, RTC_FORMAT_BIN);
HAL_RTC_GetDate(phrtc, &rtc_d, RTC_FORMAT_BIN);
}
4.闹钟回调(暂时无)