` 在APM32F030R8芯片内部集成有RTC,该RTC 是一个独立的 BCD 定时器/计数器,不仅可以支持日历功能,还具有闹钟中断和阶段性中断功能, 日历功能除了亚秒、秒、分钟、小时(12 或 24 小时格式)、星期、日期、月、年,以BCD(二进制编码的十进制)格式存在的日历时钟外,还具有自动调整一个月为28、 29(闰年)天、 30 和 31 天的功能。 将RTC计时功能与前面介绍的OLED屏相结合,就可实现一个 电子时钟,见下图所示。 RTC电子时钟
实现图示效果的主程序为:
- int main(void)
- {
- uint8_t Presec;
- uint32_t data;
- RTC_DATE_T DateStruct;
- RTC_tiME_T TimeStruct;
- APP_OLED_Init();
- OLED_Init();
- OLED_Clear();
- OLED_ShowString(0,0,"APM32F030 TEST",16);
- OLED_ShowString(16,2,"OLED & RTC",16);
- OLED_ShowString(4,4,"20 - -",16);
- OLED_ShowString(20,6," : :",16);
- /** RTC Reset */
- RTC_Init();
- RTC_Reset();
- RTC_Init();
- /** RTC Enable Init*/
- RTC_EnableInit();
- /** First time Init*/
- if((RTC_ReadBackup(RTC_BAKP_DATA4)&&0X01)==RESET)
- {
- /* Init Date 20/12/6/0*/
- DateStruct.year = 20;
- DateStruct.month = 12;
- DateStruct.date = 6;
- DateStruct.weekday = 0;
- RTC_ConfigDate(RTC_FORMAT_BIN,&DateStruct);
- Delay();
- RTC_WriteBackup(RTC_BAKP_DATA1,RTC->DATE);
- Delay();
- RTC_WriteBackup(RTC_BAKP_DATA4,0X01);
- }
- /** Read Date information from Backup*/
- else
- {
- data = RTC_ReadBackup(RTC_BAKP_DATA1);
- DateStruct.year = data>>16;
- DateStruct.month = (data>>8)&0x0f;
- DateStruct.date = data&0xff;
- DateStruct.weekday = (data>>13)&0x07;
- RTC_ConfigDate(RTC_FORMAT_BCD,&DateStruct);
- Delay();
- }
- /** First time Init*/
- if((RTC_ReadBackup(RTC_BAKP_DATA3)&&0X01)==RESET)
- {
- TimeStruct.H12 = 12;
- TimeStruct.hours = 23;
- TimeStruct.minutes = 59;
- TimeStruct.seconds = 50;
- RTC_ConfigTime(RTC_FORMAT_BIN,&TimeStruct);
- Delay();
- RTC_WriteBackup(RTC_BAKP_DATA0,RTC->TIME);
- Delay();
- RTC_WriteBackup(RTC_BAKP_DATA3,0X01);
- }
- /** Read Time information from Backup */
- else
- {
- data = RTC_ReadBackup(RTC_BAKP_DATA0);
- TimeStruct.hours = data>>16;
- TimeStruct.minutes = (data>>8)&0xff;
- TimeStruct.seconds = (data)&0xff;
- RTC_ConfigTime(RTC_FORMAT_BCD,&TimeStruct);
- Delay();
- }
- /* Wait is neccessary*/
- Delay();
- /** RTC Disable Init */
- RTC_DisableInit();
- for(;;)
- {
- /** Read last Time*/
- Presec = TimeStruct.seconds;
- /** Read time*/
- RTC_ReadTime(RTC_FORMAT_BIN,&TimeStruct);
- /** If pass 1 second */
- if(Presec!=TimeStruct.seconds)
- {
- /** Read Date */
- RTC_ReadDate(RTC_FORMAT_BIN,&DateStruct);
- /** Write DATE information to Backup*/
- RTC_WriteBackup(RTC_BAKP_DATA1,RTC->DATE);
- OLED_ShowNum(20,4,DateStruct.year,2,16);
- OLED_ShowNum(44,4,DateStruct.month,2,16);
- OLED_ShowNum(68,4,DateStruct.date,2,16);
- OLED_ShowNum(20,6,TimeStruct.hours,2,16);
- OLED_ShowNum(44,6,TimeStruct.minutes,2,16);
- OLED_ShowNum(68,6,TimeStruct.seconds,2,16);
- }
- }
- }
复制代码
`
|