最近在搞得一个项目要用到时间、日期显示,开启stm32内部RTC,但是发现一个问题,就是我显示的时间老是不准确
大概要比正常的时间晚1个半小时之多。
/*-------------------------配置RTC时钟----------------------------------------*/
void RTC_CLK_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_PeriphCLKInitTypeDef  
eriphClkInitStruct;
/*##-1- Configue LSI as RTC clock soucre ############################*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;//外部LSE时钟为RTC时钟源
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
RCC_OscInitStruct.LSIState = RCC_LSI_OFF;
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
HAL_RCCEx_PeriphCLKConfig(&
eriphClkInitStruct);
/*##-2- Enable RTC peripheral Clocks #######################################*/
/* Enable RTC Clock */
__HAL_RCC_RTC_ENABLE();
}
/*------------------日期时间参数配置------------------------------------------*/
void RTC_Config(void)
{
// HAL_RTC_CLK_ENABLE();
RTC_CLK_Config();
/*##-1- Configure the RTC peripheral #######################################*/
/* Configure RTC prescaler and RTC data registers */
/* RTC configured as follow:
- Hour Format = Format 24
- Asynch Prediv = Value according to source clock
- Synch Prediv = Value according to source clock
- OutPut = Output Disable
- OutPutPolarity = High Polarity
- OutPutType = Open Drain */
RtcHandle.Instance = RTC;
RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24;
RtcHandle.Init.AsynchPrediv = 0x7F; // RTC_ASYNCH_PREDIV;
RtcHandle.Init.SynchPrediv = 0x0130; //RTC_SYNCH_PREDIV;
RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
HAL_RTC_Init(&RtcHandle);
/*##-2- Check if Data stored in BackUp register0: No Need to reconfigure RTC#*/
/* Read the BackUp Register 0 Data */
if(HAL_RTCEx_BKUPRead(&RtcHandle, RTC_BKP_DR0) != 0x32F2)
{
/* Configure RTC Calendar */
RTC_CalendarConfig();
}
}
大神给提提建议哪里出了问题?
一周热门 更多>