- void ertc_config(void)
- {
- /* enable the pwc clock interface */
- crm_periph_clock_enable(CRM_PWC_PERIPH_CLOCK, TRUE);
- /* allow access to ertc */
- pwc_battery_powered_domain_access(TRUE);
- /* reset ertc domain */
- crm_battery_powered_domain_reset(TRUE);
- crm_battery_powered_domain_reset(FALSE);
- #if defined (ERTC_CLOCK_SOURCE_LICK)
- /* enable the lick osc */
- crm_clock_source_enable(CRM_CLOCK_SOURCE_LICK, TRUE);
- /* wait till lick is ready */
- while(crm_flag_get(CRM_LICK_STABLE_FLAG) == RESET)
- {
- }
- /* select the ertc clock source */
- crm_ertc_clock_select(CRM_ERTC_CLOCK_LICK);
- /* ertc second(1hz) = ertc_clk(lick) / (ertc_clk_div_a + 1) * (ertc_clk_div_b + 1) */
- ertc_clk_div_b = 255;
- ertc_clk_div_a = 127;
- #elif defined (ERTC_CLOCK_SOURCE_LEXT)
- /* enable the lext osc */
- crm_clock_source_enable(CRM_CLOCK_SOURCE_LEXT, TRUE);
- /* wait till lext is ready */
- while(crm_flag_get(CRM_LEXT_STABLE_FLAG) == RESET)
- {
- }
- /* select the ertc clock source */
- crm_ertc_clock_select(CRM_ERTC_CLOCK_LEXT);
- /* ertc second(1hz) = ertc_clk / (ertc_clk_div_a + 1) * (ertc_clk_div_b + 1) */
- ertc_clk_div_b = 255;
- ertc_clk_div_a = 127;
- #endif
- /* enable the ertc clock */
- crm_ertc_clock_enable(TRUE);
- /* deinitializes the ertc registers */
- ertc_reset();
- /* wait for ertc registers update */
- ertc_wait_update();
- /* configure the ertc divider */
- ertc_divider_set(ertc_clk_div_a, ertc_clk_div_b);
- /* configure the ertc hour mode */
- ertc_hour_mode_set(ERTC_HOUR_MODE_24);
- /* set date: 2021-05-01 */
- ertc_date_set(21, 5, 1, 5);
- /* set time: 12:00:00 */
- ertc_time_set(12, 0, 0, ERTC_AM);
- /* set the alarm 12:00:10 */
- ertc_alarm_mask_set(ERTC_ALA, ERTC_ALARM_MASK_DATE_WEEK);
- ertc_alarm_week_date_select(ERTC_ALA, ERTC_SLECT_DATE);
- ertc_alarm_set(ERTC_ALA, 1, 12, 0, 10, ERTC_AM);
- /* enable ertc alarm a interrupt */
- ertc_interrupt_enable(ERTC_ALA_INT, TRUE);
- /* enable the alarm */
- ertc_alarm_enable(ERTC_ALA, TRUE);
- ertc_flag_clear(ERTC_ALAF_FLAG);
- /* indicator for the ertc configuration */
- ertc_bpr_data_write(ERTC_DT1, 0x1234);
- }
2、时钟显示
- void ertc_time_show(void)
- {
- ertc_time_type time;
- char buff[8];
- /* get the current time */
- ertc_calendar_get(&time);
- /* display date format : year-month-day */
- printf("Time: %02d-%02d-%02d ",time.year, time.month, time.day);
- /* display time format : hour:min:sec */
- printf("%02d:%02d:%02d\r\n",time.hour, time.min, time.sec);
-
- sprintf(buff,"%02d:%02d:%02d",time.hour, time.min, time.sec);
-
- GUI_WriteASCII_BIG(15,64,(uint8_t*)buff,0X64BD,0X92C);
- }
3、闹钟显示
- void ertc_alarm_show(void)
- {
- ertc_alarm_value_type alarm;
- /* get the current alarm */
- ertc_alarm_get(ERTC_ALA, &alarm);
- /* display alarm format : hour:min:sec */
- printf("Alarm: %02d:%02d:%02d\r\n",alarm.hour, alarm.min, alarm.sec);
- }
4、ERTC中断处理
- void ERTC_IRQHandler(void)
- {
- if(ertc_interrupt_flag_get(ERTC_ALAF_FLAG) != RESET)
- {
- /* display the alarm */
- ertc_alarm_show();
- at32_led_on(LED2);
- /* clear alarm flag */
- ertc_flag_clear(ERTC_ALAF_FLAG);
- /* clear exint flag */
- exint_flag_clear(EXINT_LINE_17);
- }
- }
5、main调用
配置LCD、ERTC时钟、中断设置、使能
- void pre_while(void)
- {
-
- InitLCD();
- clrScr(VGA_RED);
-
- GUI_Show**(10,10,184,88,VGA_WHITE,VGA_RED);
- GUI_Write16CnChar(30,100,(unsigned char*)"AT-START-L021测评6",VGA_WHITE,VGA_RED);
-
- /* enable the pwc clock interface */
- crm_periph_clock_enable(CRM_PWC_PERIPH_CLOCK, TRUE);
- /* allow access to ertc */
- pwc_battery_powered_domain_access(TRUE);
- if (ertc_bpr_data_read(ERTC_DT1) != 0x1234)
- {
- /* printf ertc status */
- printf("ertc has not been initialized\r\n\r\n");
- /* ertc configuration */
- ertc_config();
- }
- else
- {
- /* printf ertc status */
- printf("ertc has been initialized\r\n\r\n");
- /* wait for ertc registers update */
- ertc_wait_update();
- /* clear the ertc alarm flag */
- ertc_flag_clear(ERTC_ALAF_FLAG);
- /* clear the exint line 17 pending bit */
- exint_flag_clear(EXINT_LINE_17);
- }
-
- /* ertc alarm interrupt configuration */
- exint_default_para_init(&exint_init_struct);
- exint_init_struct.line_enable = TRUE;
- exint_init_struct.line_mode = EXINT_LINE_INTERRUPT;
- exint_init_struct.line_select = EXINT_LINE_17;
- exint_init_struct.line_polarity = EXINT_TRIGGER_RISING_EDGE;
- exint_init(&exint_init_struct);
- /* enable the ertc interrupt */
- nvic_irq_enable(ERTC_IRQn, 0, 1);
- delay_ms(1000);
- clrScr(0X92C);
- }
外部中断line 17是ERTC闹钟事件:
6、运行效果
7、源码
test1.rar
(85.63 KB, 下载次数: 4)