48 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			48 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
#include "base.h"
 | 
						|
#include "rtc.h"
 | 
						|
 | 
						|
 | 
						|
 | 
						|
int RTC_InitNormal (RTC_InitStruct *init)
 | 
						|
{
 | 
						|
	RTC_InitTypeDef RTC_InitStructure;
 | 
						|
	u16 retry=0X1FFF; 
 | 
						|
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);//使能PWR时钟
 | 
						|
	PWR_BackupAccessCmd(ENABLE);	//使能后备寄存器访问 
 | 
						|
 | 
						|
	if(RTC_ReadBackupRegister(RTC_BKP_DR0)!=0x5050)		//是否第一次配置?
 | 
						|
	{
 | 
						|
		RCC_LSEConfig(RCC_LSE_ON);//LSE 开启    
 | 
						|
		while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)	//检查指定的RCC标志位设置与否,等待低速晶振就绪
 | 
						|
		{
 | 
						|
			retry++;
 | 
						|
			if (init->delay_ms)
 | 
						|
				init->delay_ms(10);
 | 
						|
		}
 | 
						|
		if(retry==0)return -1;		//LSE 开启失败. 
 | 
						|
 | 
						|
		RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);		//设置RTC时钟(RTCCLK),选择LSE作为RTC时钟    
 | 
						|
		RCC_RTCCLKCmd(ENABLE);	//使能RTC时钟 
 | 
						|
 | 
						|
		RTC_InitStructure.RTC_AsynchPrediv = 0x7F;//RTC异步分频系数(1~0X7F)
 | 
						|
		RTC_InitStructure.RTC_SynchPrediv  = 0xFF;//RTC同步分频系数(0~7FFF)
 | 
						|
		RTC_InitStructure.RTC_HourFormat   = RTC_HourFormat_24;//RTC设置为,24小时格式
 | 
						|
		RTC_Init(&RTC_InitStructure);
 | 
						|
		
 | 
						|
		RTC_TimeTypeDef time={0};
 | 
						|
		time.RTC_Hours=12;
 | 
						|
		time.RTC_H12=RTC_H12_AM;
 | 
						|
		RTC_SetTime (RTC_Format_BIN,&time);
 | 
						|
		RTC_DateTypeDef data={0};
 | 
						|
		data.RTC_Year=19;data.RTC_Month=12;data.RTC_Date=3;data.RTC_WeekDay=1;
 | 
						|
		RTC_SetDate(RTC_Format_BIN,&data);
 | 
						|
		RTC_WriteBackupRegister(RTC_BKP_DR0,0x5050);	//标记已经初始化过了
 | 
						|
	} 
 | 
						|
	return 0;
 | 
						|
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 |