- int main(void)
- {
- exint_init_type exint_init_struct;
- ertc_time_type time;
- uint32_t temp = 0;
- system_clock_config();
- nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
- at32_board_init();
- uart_print_init(115200);
- crm_periph_clock_enable(CRM_PWC_PERIPH_CLOCK, TRUE);
- pwc_battery_powered_domain_access(TRUE);
- if (ertc_bpr_data_read(ERTC_DT1) != 0x1234)
- {
- printf("ertc has not been initialized\r\n\r\n");
- ertc_config();
- }
- else
- {
- printf("ertc has been initialized\r\n\r\n");
- ertc_wait_update();
- ertc_flag_clear(ERTC_ALAF_FLAG);
- exint_flag_clear(EXINT_LINE_17);
- }
- exint_default_para_init(&exint_init_struct);
- exint_init_struct.line_enable = TRUE;
- exint_init_struct.line_mode = EXINT_LINE_INTERRUPUT;
- exint_init_struct.line_select = EXINT_LINE_17;
- exint_init_struct.line_polarity = EXINT_TRIGGER_RISING_EDGE;
- exint_init(&exint_init_struct);
- nvic_irq_enable(ERTC_IRQn, 0, 1);
- app_oled_init();
- OLED_Init();
- OLED_Clear();
- OLED_ShowString(0,0,"AT32F425",16);
- OLED_ShowString(0,2,"OLED & RTC",16);
- OLED_ShowString(8,4,"20 - -",16);
- OLED_ShowString(24,6," : :",16);
- while(1)
- {
- ertc_calendar_get(&time);
- if(temp != time.sec)
- {
- temp = time.sec;
- OLED_ShowNum(24,4,time.year,2,16);
- OLED_ShowNum(48,4,time.month,2,16);
- OLED_ShowNum(72,4,time.day,2,16);
- OLED_ShowNum(24,6,time.hour,2,16);
- OLED_ShowNum(48,6,time.min,2,16);
- OLED_ShowNum(72,6,time.sec,2,16);
- }
- }
- }
该程序的处理流程为:
首先进行RTC初始化,然后判别识别标志,若未设置初始计时值,则读取程序中的初始值来计时。否则,会在已有的计时值基础上进行计时处理,其显示效果如图2所示。
图2 显示效果
其中RTC初始计时值在该函数中设置或修改:
- void ertc_config(void)
- {
- crm_periph_clock_enable(CRM_PWC_PERIPH_CLOCK, TRUE);
- pwc_battery_powered_domain_access(TRUE);
- crm_battery_powered_domain_reset(TRUE);
- crm_battery_powered_domain_reset(FALSE);
- #if defined (ERTC_CLOCK_SOURCE_LICK)
- crm_clock_source_enable(CRM_CLOCK_SOURCE_LICK, TRUE);
- while(crm_flag_get(CRM_LICK_STABLE_FLAG) == RESET)
- {
- }
- crm_ertc_clock_select(CRM_ERTC_CLOCK_LICK);
- ertc_clk_div_b = 255;
- ertc_clk_div_a = 127;
- #elif defined (ERTC_CLOCK_SOURCE_LEXT)
- crm_clock_source_enable(CRM_CLOCK_SOURCE_LEXT, TRUE);
- while(crm_flag_get(CRM_LEXT_STABLE_FLAG) == RESET)
- {
- }
- crm_ertc_clock_select(CRM_ERTC_CLOCK_LEXT);
- ertc_clk_div_b = 255;
- ertc_clk_div_a = 127;
- #endif
- crm_ertc_clock_enable(TRUE);
- ertc_reset();
- ertc_wait_update();
- ertc_divider_set(ertc_clk_div_a, ertc_clk_div_b);
- ertc_hour_mode_set(ERTC_HOUR_MODE_24);
- ertc_date_set(22, 3, 2, 3);
- ertc_time_set(12, 0, 0, ERTC_AM);
- ertc_alarm_mask_set(ERTC_ALA, ERTC_ALARM_MASK_DATE_WEEK);
- ertc_alarm_week_date_select(ERTC_ALA, ERTC_SLECT_DATE);
- ertc_flag_clear(ERTC_ALAF_FLAG);
- ertc_bpr_data_write(ERTC_DT1, 0x1234);
- }
由此可见RTC计时还是十分有用的,并且也十分好用。