中级会员
 
- 积分
- 270
- 金钱
- 270
- 注册时间
- 2015-6-7
- 在线时间
- 21 小时
|
40金钱
本帖最后由 whyil 于 2016-6-21 21:31 编辑
[mw_shl_code=c,true]//RTC初始化
void rtc_init(void)
{
//默认时间 2016.6.20 12:13:30
//这个时间是不是需要根据当前的gps时间或gprs时间设置 是在运行时修改还是在初始化时修改?
//这里暂时使用固定时间
rtc_time time = {
12,13,30,
2016,6,20
};
myprintf("rtc init start\r\n");
/* Enable PWR and BKP clocks */ //这个寄存器不在备份域
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
/* Allow access to BKP Domain */
PWR_BackupAccessCmd(ENABLE); //使能对后备寄存器和RTC的访问
//从指定备份域寄存器读取参数 不等代表第一次初始化rtc 相等已经初始化过rtc 跳过初始化过程
if(BKP_ReadBackupRegister(BKP_DR1) != 0x5050)
{
/* Reset Backup Domain */
BKP_DeInit();
/* Enable the LSI OSC */
RCC_LSICmd(ENABLE);
/* Wait till LSI is ready */
while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
{}
/* Select the RTC Clock Source */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
/* Enable RTC Clock */
RCC_RTCCLKCmd(ENABLE);
/* Wait for RTC registers synchronization */
RTC_WaitForSynchro();
// /* Wait until last write operation on RTC registers has finished */
// RTC_WaitForLastTask();
// // /* Enable the RTC Second */
// // RTC_ITConfig(RTC_IT_SEC, ENABLE);
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask(); //查询RTOFF位,直到RTOFF的值变为’1
/* Set RTC prescaler: set RTC period to 1sec */
RTC_SetPrescaler(40000); //包含了进入和退出RTC设置模式 设置预分频 内部LSI约为40Khz
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask(); //查询RTOFF,直至RTOFF位变为’1’以确认写操作已经完成
//设置系统当前时间
rtc_set_time(time);
BKP_WriteBackupRegister(BKP_DR1, 0X5050); //向指定的后备寄存器中写入用户程序数据
}
else{
/* Wait for RTC registers synchronization */
RTC_WaitForSynchro(); //按下复位后卡在了这里
}
NVIC_Configuration(); //设置中断优先级
myprintf("rtc init end\r\n");
}[/mw_shl_code] |
|