请问为什么每次重启开发板后时间都是恢复成我设置的时间 而不是纽扣电池供电呢 F103MINI开发板
void MX_RTC_Init(void)
{
RTC_TimeTypeDef sTime;
RTC_DateTypeDef DateToUpdate;
/**Initialize RTC and set the Time and Date
*/
hrtc.Instance = RTC;
hrtc.Init.AsynchPrediv = RTC_AUTO_1_SECOND;
hrtc.Init.OutPut = RTC_OUTPUTSOURCE_NONE;
HAL_RTC_Init(&hrtc);
sTime.Hours = 13;
sTime.Minutes = 36;
sTime.Seconds = 0;
HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
DateToUpdate.WeekDay = RTC_WEEKDAY_WEDNESDAY;
DateToUpdate.Month = RTC_MONTH_JULY;
DateToUpdate.Date = 8;
DateToUpdate.Year = 17;
HAL_RTC_SetDate(&hrtc, &DateToUpdate, RTC_FORMAT_BIN);
}
MAIN函数
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
/* Get the RTC current Time ,must get time first*/
HAL_RTC_GetTime(&hrtc, &stimestructure, RTC_FORMAT_BIN);
/* Get the RTC current Date */
HAL_RTC_GetDate(&hrtc, &sdatestructure, RTC_FORMAT_BIN);
/* Display date Format : yy/mm/dd */
printf("%02d/%02d/%02d
",2000 + sdatestructure.Year, sdatestructure.Month, sdatestructure.Date);
/* Display time Format : hh:mm:ss */
printf("%02d:%02d:%02d
",stimestructure.Hours, stimestructure.Minutes, stimestructure.Seconds);
printf("
");
HAL_Delay(1000);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
#if 1
///* RTC init function */
void MX_RTC_Init(void)
{
RTC_TimeTypeDef sTime;
RTC_DateTypeDef DateToUpdate;
/**Initialize RTC Only
*/
hrtc.Instance = RTC;
hrtc.Init.AsynchPrediv = RTC_AUTO_1_SECOND;
hrtc.Init.OutPut = RTC_OUTPUTSOURCE_NONE;
if (HAL_RTC_Init(&hrtc) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
/**Initialize RTC and set the Time and Date
*/
if(HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR1) != 0x32F2){
sTime.Hours = 0x5;
sTime.Minutes = 0x28;
sTime.Seconds = 0x0;
if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
DateToUpdate.WeekDay = RTC_WEEKDAY_SATURDAY;
DateToUpdate.Month = RTC_MONTH_JUNE;
DateToUpdate.Date = 0x7;
DateToUpdate.Year = 0x17;
if (HAL_RTC_SetDate(&hrtc, &DateToUpdate, RTC_FORMAT_BCD) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR1,0x32F2);
}
// printf("%d ",2000+DateToUpdate.Year);
}
#endif
一周热门 更多>