STM32F103---driver RTC of standard library function

It is very simple to drive the RTC. Check the stm32f10x reference manual to find the following picture content, and you will have an idea.

Introduced by the reference manual

The LSE crystal is a 32.768kHz low speed external crystal or ceramic resonator. It provides
a low-power and accurate clock source for the real-time clock or other timing functions.

We choose the LSE clock source.

According to the above picture, the author's idea is:

    1. First enable the corresponding bit to access the RTC

    2. Select the corresponding clock source

    3. Configure RTC

   4. Select Interrupt

Refer to the figure below to write the code.

 

Finally, the code can be obtained to drive the RTC (the code has comments):

void rtc_init(void)
{
	NVIC_InitTypeDef NVIC_InitStruct;
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP,ENABLE);  //使能BKPEN位
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR,ENABLE);  //使能PWREN位
	PWR_BackupAccessCmd(ENABLE); //使能PWR_CR的DBP位  
	
	RCC_LSEConfig(RCC_LSE_ON); //配置LSE时钟源开
	RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); //RTCCLK时钟源配置为LSE
	RCC_RTCCLKCmd(ENABLE); //使能RTCCLK配置生效
	
	RTC_WaitForLastTask();  //等待RTOFF位为1
	RTC_EnterConfigMode(); //进入配置模式
	
	RTC_WaitForLastTask(); //根据配置RTC寄存器得 每次写RTC的寄存器都要等待RTOFF位为1
	RTC_SetCounter(1672502392); //RTC从这个数开始计时 这是时间戳 可以上网查时间戳转换
	
	RTC_WaitForLastTask(); //再次等待写入完成
	RTC_SetPrescaler(0x7fff); //预分频设定为0x7fff = 32.767 LSE为32.768Khz分频后约得 1KHZ
	
	RTC_WaitForLastTask(); //再次等待写入完成
	RTC_ITConfig(RTC_IT_SEC,ENABLE);//RTC中断配置,让秒中断使能
	
	RTC_WaitForLastTask();//再次等待写入完成
	RTC_ExitConfigMode(); //离开配置模式
	
	RTC_WaitForLastTask();//再次等待写入完成
	
	NVIC_InitStruct.NVIC_IRQChannel = RTC_IRQn; //NVIC是总中断配置 选择RTC这个中断通道
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; //中断使能
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 2; //中断优先级2
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 3; //同等优先级下 这个中断优先级3
	
	NVIC_Init(&NVIC_InitStruct);  //让上面配置生效
}

 -------------------------------------------------dotted line So far the RTC has been driven ---------------------------------------

Read the value in the RTC below:

         

 

/*
函数功能:RTC读取
函数参数:无
函数返回值:无
函数描述:无
*/
void Rtc_Read(void)
{
	RTC_WaitForSynchro(); //软件等待RTC_CRL中的RSF位置1
	TIME = RTC_GetCounter();//正式读取
	timeinfo = localtime(&TIME); //用time.h头文件里面的函数进行 数据处理
}

------------------------Read content completed---------------------- -------------------------------------------------- --

read content description 

        The following variables are defined in the main function file 

                  uint32_t TIME;
                  struct tm* timeinfo;

 After data processing, the read content can be used to obtain the desired year, month, day, hour, minute, and second with the structure pointer.

The content in the picture is not defined but comes with the time.h file.

Friends who think it is helpful to you, please like it and encourage it! Thank you guys

Guess you like

Origin blog.csdn.net/longjintao1/article/details/124530740