main文件
- int main(void)
- {
- exint_init_type exint_init_struct;
- ertc_time_type time;
- uint32_t temp = 0;
-
- /*初始化系统时钟 */
- system_clock_config();
-
- /*配置 NVIC 优先级组 */
- nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
-
- /* 初始化 ATSTART 板子资源 */
- at32_board_init();
- /*初始化串口*/
- uart_init(115200);
- /* 开启 PWC 时钟 */
- 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 初始化 */
- ertc_config();
- }
- else
- {
- /* 打印信息 */
- printf("ertc has been initialized\r\n\r\n");
-
- /* 等待 ERTC 寄存器同步,最多需要 2 个 ERTC_CLK */
- ertc_wait_update();
- /* 清除闹钟标志 */
- ertc_flag_clear(ERTC_ALAF_FLAG);
- /* 清除 exint 挂起标志 */
- exint_flag_clear(EXINT_LINE_17);
- }
- /* 显示当前的 ertc 时间和闹钟时间 */
- ertc_time_show();
- ertc_alarm_show();
- printf("\r\n");
-
- /* 闹钟初始化 */
- 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(ERTCAlarm_IRQn, 0, 1);
- while(1)
- {
- /* 获取当前日历 */
- ertc_calendar_get(&time);
-
- if(temp != time.sec)
- {
- temp = time.sec;
-
- /* 打印日期:年-月-日 */
- printf("%02d-%02d-%02d ",time.year, time.month, time.day);
-
- /* 打印时间:时:分:秒 */
- printf("%02d:%02d:%02d\r\n",time.hour, time.min, time.sec);
- }
- }
- }
ERTC 初始化 ertc_config 函数代码描述
- void ertc_config(void)
- {
- /* 开启 PWC 时钟 */
- 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)
- /* 使能 LICK 时钟 */
- crm_clock_source_enable(CRM_CLOCK_SOURCE_LICK, TRUE);
- /* 等待 LICK 时钟稳定 */
- while(crm_flag_get(CRM_LICK_STABLE_FLAG) == RESET)
- {
- }
- /* 选择 ERTC 时钟源 */
- 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)
- /* 使能 LEXT 时钟 */
- crm_clock_source_enable(CRM_CLOCK_SOURCE_LEXT, TRUE);
- /* 等待 LEXT 时钟稳定 */
- while(crm_flag_get(CRM_LEXT_STABLE_FLAG) == RESET)
- {
- }
- /* 选择 ERTC 时钟源 */
- 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
- /* 使能 ERTC 时钟 */
- crm_ertc_clock_enable(TRUE);
- /* 复位所有 ERTC 寄存器 */
- ertc_reset();
- /* 等待 ERTC 寄存器同步,最多需要 2 个 ERTC_CLK */
- ertc_wait_update();
-
- /* 配置 ERTC 分频器 */
- ertc_divider_set(ertc_clk_div_a, ertc_clk_div_b);
-
- /* 配置 ERTC 小时格式为 24 小时格式 */
- ertc_hour_mode_set(ERTC_HOUR_MODE_24);
-
- /* 设置日期: 2021-05-01 */
- ertc_date_set(21, 5, 1, 5);
-
- /* 设置时间: 12:00:00 */
- ertc_time_set(12, 0, 0, ERTC_AM);
-
- /* 设置闹钟 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);
-
- /* 使能闹钟中断 */
- ertc_interrupt_enable(ERTC_ALA_INT, TRUE);
- /* 开启闹钟 */
- ertc_alarm_enable(ERTC_ALA, TRUE);
-
- ertc_flag_clear(ERTC_ALAF_FLAG);
-
- /* 指示 ERTC 初始化 */
- ertc_bpr_data_write(ERTC_DT1, 0x1234);
- }
闹钟中断函数代码描述
- void ERTCAlarm_IRQHandler(void)
- {
- if(ertc_flag_get(ERTC_ALAF_FLAG) != RESET)
- {
- /* 显示闹钟 */
- ertc_alarm_show();
-
- at32_led_on(LED2);
-
- /* 清除闹钟标志 */
- ertc_flag_clear(ERTC_ALAF_FLAG);
-
- /* 清除 EXINT 标志 */
- exint_flag_clear(EXINT_LINE_17);
- }
- }
最后中断处理程序的清理标志位很重要,如果不清理没法正常进入下一个周期的循环。
|