void rtc_init(void)
{
RTC_DateTypeDef RTC_DateStructure;
RTC_TimeTypeDef RTC_TimeStructure;
RTC_InitTypeDef RTC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
/* Enable the PWR clock ,使能电源控制时钟*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
/* Allow access to RTC,允许访问备份电路 */
PWR_BackupAccessCmd(ENABLE);
#if 0
/* 使能LSE*/
RCC_LSEConfig(RCC_LSE_ON);
/* 检查该LSE是否有效*/
while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET);
/* 选择LSE作为RTC的硬件时钟源*/
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
#else //若LSE无法工作,可用内部LSI
/* 使能LSI*/
RCC_LSICmd(ENABLE);
/* 检查该LSI是否有效*/
while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET);
/* 选择LSI作为RTC的硬件时钟源*/
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
#endif
/* Enable the RTC Clock,使能RTC时钟 */
RCC_RTCCLKCmd(ENABLE);
/* Wait for RTC APB registers synchronisation,等待RTC相关寄存器就绪 */
RTC_WaitForSynchro();
#if 0 //LSE
/* Configure the RTC data register and RTC prescaler,配置RTC数据寄存器与RTC的分频值 */
RTC_InitStructure.RTC_AsynchPrediv = 127; //异步分频系数
RTC_InitStructure.RTC_SynchPrediv = 255; //同步分频系数
RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; //24小时格式
RTC_Init(&RTC_InitStructure);
#else //LSI
/* Configure the RTC data register and RTC prescaler,配置RTC数据寄存器与RTC的分频值 */
RTC_InitStructure.RTC_AsynchPrediv = 127; //异步分频系数
RTC_InitStructure.RTC_SynchPrediv = 249; //同步分频系数
RTC_InitStructure.
stm32之22.RTC时钟
于 2023-08-26 19:15:05 首次发布