赞
踩
- /*
- * 立创开发板软硬件资料与相关扩展板软硬件资料官网全部开源
- * 开发板官网:www.lckfb.com
- * 技术支持常驻论坛,任何技术问题欢迎随时交流学习
- * 立创论坛:club.szlcsc.com
- * 关注bilibili账号:【立创开发板】,掌握我们的最新动态!
- * 不靠卖板赚钱,以培养中国工程师为己任
- */
- #include "board.h"
- #include "bsp_uart.h"
- #include "stdio.h"
- #include "sys.h"
-
- void RTC_SetDateTime(uint8_t year, uint8_t month, uint8_t date, uint8_t week, uint8_t hour, uint8_t minute, uint8_t second, uint8_t RTC_H12);
- void RTC_DateTimeGet(uint8_t *year, uint8_t *month, uint8_t *date, uint8_t *week, uint8_t *hour, uint8_t *minute, uint8_t *second);
- void RTC_CheckInit(void);
-
- int main(void)
- {
- uint8_t year,month,date,week,hour,minute,second;
- board_init();
- uart1_init(115200);
- /*RTC初始化*/
- RTC_CheckInit();
- while(1)
- {
- RTC_DateTimeGet(&year,&month,&date,&week,&hour,&minute,&second);
- printf("the date is : \r\n");
- printf("%d-%d-%d %d\r\n",year,month,date,week);
- printf("the time is : \r\n");
- printf("%d:%d:%d \r\n",hour,minute,second);
- delay_ms(1000);
- }
- }
-
- /*
- *初始化RTC
- */
- void RTC_CheckInit(void)
- {
- //1.打开电源管理时钟
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR,ENABLE);
- //2.使能备份寄存器操作
- PWR_BackupAccessCmd(ENABLE);
- //3.打开LSI
- RCC_LSICmd(ENABLE);
- //4.配置RTC时钟源
- RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
- //5.使能RTC时钟
- RCC_RTCCLKCmd(ENABLE);
- //6.等待时钟同步
- RTC_WaitForSynchro();
- //7.检查RTC初始化没有
- if(RTC_ReadBackupRegister(RTC_BKP_DR0)!=0x2003)
- {
- printf("SET TIME...\r\n");
- //8.没有初始化则初始化一个时间日期
- RTC_SetDateTime(24,4,25,4,14,30,15,RTC_H12_AM);
- printf("SET TIME END\r\n");
- }
- //9.关闭备份寄存器
- PWR_BackupAccessCmd(DISABLE);
- }
-
- /*
- *RTC设置时间和日期
- */
- void RTC_SetDateTime(uint8_t year, uint8_t month, uint8_t date, uint8_t week, uint8_t hour, uint8_t minute, uint8_t second, uint8_t RTC_H12)
- {
- //10.关闭写保护
- RTC_WriteProtectionCmd(DISABLE);
- //11.RTC进入编辑模式
- RTC_InitTypeDef RTC_InitStruct;
- RTC_EnterInitMode(); //进入编辑模式
- RTC_InitStruct.RTC_AsynchPrediv=0x7F;
- RTC_InitStruct.RTC_HourFormat=RTC_HourFormat_24; //24小时制
- RTC_InitStruct.RTC_SynchPrediv=0xFF;
- RTC_Init(&RTC_InitStruct);
- //12.设置时间
- RTC_TimeTypeDef RTC_TimeStruct;
- RTC_TimeStruct.RTC_H12=RTC_H12;
- RTC_TimeStruct.RTC_Hours=hour;
- RTC_TimeStruct.RTC_Minutes=minute;
- RTC_TimeStruct.RTC_Seconds=second;
- RTC_SetTime(RTC_Format_BIN,&RTC_TimeStruct); //二进制设置时间
- //13.设置日期
- RTC_DateTypeDef RTC_DateStruct;
- RTC_DateStruct.RTC_Date=date;
- RTC_DateStruct.RTC_Month=month;
- RTC_DateStruct.RTC_WeekDay=week;
- RTC_DateStruct.RTC_Year=year;
- RTC_SetDate(RTC_Format_BIN,&RTC_DateStruct); //二进制设置日期
- //14.RTC退出编辑模式
- RTC_ExitInitMode();
- //15.初始化完成,设置备注
- RTC_WriteBackupRegister(RTC_BKP_DR0,0x2003);
- //16.RTC开启写保护
- RTC_WriteProtectionCmd(ENABLE);
- }
-
- /*
- *获取时间
- */
- void RTC_DateTimeGet(uint8_t *year, uint8_t *month, uint8_t *date, uint8_t *week, uint8_t *hour, uint8_t *minute, uint8_t *second)
- {
- //17.定义日期结构体
- RTC_DateTypeDef RTC_DateStruct;
- //18.获取日期
- RTC_GetDate(RTC_Format_BIN,&RTC_DateStruct);
- *year=RTC_DateStruct.RTC_Year;
- *month=RTC_DateStruct.RTC_Month;
- *date=RTC_DateStruct.RTC_Date;
- *week=RTC_DateStruct.RTC_WeekDay;
- //19.定义时间结构体
- RTC_TimeTypeDef RTC_TimeStruct;
- //20.获取时间
- RTC_GetTime(RTC_Format_BIN,&RTC_TimeStruct);
- *hour=RTC_TimeStruct.RTC_Hours;
- *minute=RTC_TimeStruct.RTC_Minutes;
- *second=RTC_TimeStruct.RTC_Seconds;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。