STM32F0303C8T6 在使用RTC的过程中发现,概率性RTC初始化失败;但是LSE 是成功起振;
最终的原因是ISR寄存器的INTF 标志位写失败;
我用的是F0 V1.5的库函数;
发现 枚举类型RTC_Init;返回值是ERROR
/**
* @brief Initializes the RTC registers according to the specified parameters
* in RTC_InitStruct.
* @param RTC_InitStruct: pointer to a RTC_InitTypeDef structure that contains
* the configuration information for the RTC peripheral.
*
@note The RTC Prescaler register is write protected and can be written in
* initialization mode only.
* @retval An ErrorStatus enumeration value:
* - SUCCESS: RTC registers are initialized
* - ERROR: RTC registers are not initialized
*/
ErrorStatus RTC_Init(RTC_InitTypeDef* RTC_InitStruct)
{
ErrorStatus status = ERROR;
/* Check the parameters */
assert_param(IS_RTC_HOUR_FORMAT(RTC_InitStruct->RTC_HourFormat));
assert_param(IS_RTC_ASYNCH_PREDIV(RTC_InitStruct->RTC_AsynchPrediv));
assert_param(IS_RTC_SYNCH_PREDIV(RTC_InitStruct->RTC_SynchPrediv));
/* Disable the write protection for RTC registers */
RTC->WPR = 0xCA;
RTC->WPR = 0x53;
/* Set Initialization mode */
if (RTC_EnterInitMode() == ERROR)
{
status = ERROR;
}
else
{
/* Clear RTC CR FMT Bit */
RTC->CR &= ((uint32_t)~(RTC_CR_FMT));
/* Set RTC_CR register */
RTC->CR |= ((uint32_t)(RTC_InitStruct->RTC_HourFormat));
/* Configure the RTC PRER */
RTC->PRER = (uint32_t)(RTC_InitStruct->RTC_SynchPrediv);
RTC->PRER |= (uint32_t)(RTC_InitStruct->RTC_AsynchPrediv << 16);
/* Exit Initialization mode */
RTC_ExitInitMode();
status = SUCCESS;
}
/* Enable the write protection for RTC registers */
RTC->WPR = 0xFF;
return status;
}
最后附上 整个RTC初始化的代码;
RTC_InitTypeDef RTC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR,DISABLE);
PWR_BackupAccessCmd(DISABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR,ENABLE);
/* Allow access to RTC */
PWR_BackupAccessCmd(ENABLE);
// RTC->WPR = 0x000000CA;
// RTC->WPR = 0x00000053;//取消写保护
RCC_LSEConfig(RCC_LSE_ON);
while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == 0)
{
//printf("
LSE Error!");
}
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
RTC_DeInit();
/* Calendar Configuration */
RTC_InitStructure.RTC_AsynchPrediv = 0x7f;//127
RTC_InitStructure.RTC_SynchPrediv = 0xFF;//255
RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
RTC_Init(&RTC_InitStructure);//进入初始化模式,
//RTC->ISR |= RTC_ISR_INIT; //进入初始化模式
//while (!(RTC->ISR | RTC_ISR_INITF));
RTC_DateInitStructure.RTC_Year = 17;
RTC_DateInitStructure.RTC_Month = 1;
RTC_DateInitStructure.RTC_Date = 1;
RTC_DateInitStructure.RTC_WeekDay = 5;
RTC_SetDate(RTC_Format_BIN,&RTC_DateInitStructure);
RTC_TimeInitStructure.RTC_H12 = RTC_H12_AM;
RTC_TimeInitStructure.RTC_Hours = 0;
RTC_TimeInitStructure.RTC_Minutes = 0;
RTC_TimeInitStructure.RTC_Seconds = 0;
RTC_SetTime(RTC_Format_BIN,&RTC_TimeInitStructure);//
RCC_RTCCLKCmd(ENABLE);//使能RTC
RTC_WaitForSynchro();//等待时钟同步
其实,关键我在想为什么是概率性的;
对其他方面也没什么影响,就是初始化的时间没写进去;
INTF标志位写入失败,到底是什么原因导致的
有关系吧,我后来完全按照库DEMO写就没问题了
一周热门 更多>