This commit is contained in:
小冯Sir 2024-05-03 03:31:39 +08:00
parent 234a4151ae
commit 8054529380

View File

@ -27,8 +27,7 @@
RTC_HandleTypeDef hrtc; RTC_HandleTypeDef hrtc;
/* RTC init function */ /* RTC init function */
void MX_RTC_Init(void) void MX_RTC_Init(void) {
{
/* USER CODE BEGIN RTC_Init 0 */ /* USER CODE BEGIN RTC_Init 0 */
@ -46,13 +45,22 @@ void MX_RTC_Init(void)
hrtc.Instance = RTC; hrtc.Instance = RTC;
hrtc.Init.AsynchPrediv = RTC_AUTO_1_SECOND; hrtc.Init.AsynchPrediv = RTC_AUTO_1_SECOND;
hrtc.Init.OutPut = RTC_OUTPUTSOURCE_ALARM; hrtc.Init.OutPut = RTC_OUTPUTSOURCE_ALARM;
if (HAL_RTC_Init(&hrtc) != HAL_OK) if (HAL_RTC_Init(&hrtc) != HAL_OK) {
{
Error_Handler(); Error_Handler();
} }
/* USER CODE BEGIN Check_RTC_BKUP */ /* USER CODE BEGIN Check_RTC_BKUP */
__HAL_RTC_SECOND_ENABLE_IT(&hrtc, RTC_IT_SEC);
if (HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR1) == 0x1234) {
// 从备份寄存器还原日期
hrtc.DateToUpdate.Year = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR2);
hrtc.DateToUpdate.Month = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR3);
hrtc.DateToUpdate.Date = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR4);
// 把RTC计数单元的计数值更新到日期
HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
return;
}
/* USER CODE END Check_RTC_BKUP */ /* USER CODE END Check_RTC_BKUP */
/** Initialize RTC and set the Time and Date /** Initialize RTC and set the Time and Date
@ -61,8 +69,7 @@ void MX_RTC_Init(void)
sTime.Minutes = 0; sTime.Minutes = 0;
sTime.Seconds = 0; sTime.Seconds = 0;
if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK) if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK) {
{
Error_Handler(); Error_Handler();
} }
DateToUpdate.WeekDay = RTC_WEEKDAY_MONDAY; DateToUpdate.WeekDay = RTC_WEEKDAY_MONDAY;
@ -70,21 +77,18 @@ void MX_RTC_Init(void)
DateToUpdate.Date = 1; DateToUpdate.Date = 1;
DateToUpdate.Year = 0; DateToUpdate.Year = 0;
if (HAL_RTC_SetDate(&hrtc, &DateToUpdate, RTC_FORMAT_BIN) != HAL_OK) if (HAL_RTC_SetDate(&hrtc, &DateToUpdate, RTC_FORMAT_BIN) != HAL_OK) {
{
Error_Handler(); Error_Handler();
} }
/* USER CODE BEGIN RTC_Init 2 */ /* USER CODE BEGIN RTC_Init 2 */
HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR1,0x1234);
/* USER CODE END RTC_Init 2 */ /* USER CODE END RTC_Init 2 */
} }
void HAL_RTC_MspInit(RTC_HandleTypeDef* rtcHandle) void HAL_RTC_MspInit(RTC_HandleTypeDef *rtcHandle) {
{
if(rtcHandle->Instance==RTC) if (rtcHandle->Instance == RTC) {
{
/* USER CODE BEGIN RTC_MspInit 0 */ /* USER CODE BEGIN RTC_MspInit 0 */
/* USER CODE END RTC_MspInit 0 */ /* USER CODE END RTC_MspInit 0 */
@ -105,11 +109,9 @@ void HAL_RTC_MspInit(RTC_HandleTypeDef* rtcHandle)
} }
} }
void HAL_RTC_MspDeInit(RTC_HandleTypeDef* rtcHandle) void HAL_RTC_MspDeInit(RTC_HandleTypeDef *rtcHandle) {
{
if(rtcHandle->Instance==RTC) if (rtcHandle->Instance == RTC) {
{
/* USER CODE BEGIN RTC_MspDeInit 0 */ /* USER CODE BEGIN RTC_MspDeInit 0 */
/* USER CODE END RTC_MspDeInit 0 */ /* USER CODE END RTC_MspDeInit 0 */