2095|0

7212

帖子

11

TA的资源

版主

楼主
 

【AT-START-F425测评】堆积木之实时时钟(ERTC) [复制链接]

 

【实时时钟(ERTC)

实时时钟用于配置日历时钟,修改 ERTC 中日历寄存器值可以修改系统的当前时间和日期。 ERTC 计数逻辑位于电池供电域,只要电池供电域有电,ERTC 便会一直运行,不受系统复位影响。在前面驱动LCD的前提下,参照官方提供的例子,把ertc驱动,并显示在LCD上:

1、在drivers文件夹下新建ertc.c跟ertc.h分别如下:

  • #ifndef __ERTC_H
  • #define __ERTC_H
  • #include "at32f425_clock.h"
  • void ertc_time_show(void);
  • void ertc_timestamp_show(void);
  • void ertc_config(void);
  • void ertc_timestamp_config(void);
  • #endif

ertc.c

  • #include "ertc.h"
  • #include "at32f425_board.h"
  • #include "rtthread.h"
  • /**
  • * [url=home.php?mod=space&uid=159083]@brief[/url] configure the ertc peripheral by selecting the clock source.
  • * @param none
  • * @retval none
  • */
  • void ertc_config(void)
  • {
  • /* enable the pwc clock */
  • crm_periph_clock_enable(CRM_PWC_PERIPH_CLOCK, TRUE);
  • /* allow access to ertc */
  • pwc_battery_powered_domain_access(TRUE);
  • /* reset ertc bpr domain */
  • crm_battery_powered_domain_reset(TRUE);
  • crm_battery_powered_domain_reset(FALSE);
  • /* 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);
  • /* enable the ertc clock */
  • crm_ertc_clock_enable(TRUE);
  • /* deinitializes the ertc registers */
  • ertc_reset();
  • /* wait for ertc apb registers update */
  • ertc_wait_update();
  • /* configure the ertc divider */
  • /* ertc second(1hz) = ertc_clk / (div_a + 1) * (div_b + 1) */
  • ertc_divider_set(127, 255);
  • /* configure the ertc hour mode */
  • ertc_hour_mode_set(ERTC_HOUR_MODE_24);
  • /* set date: 2021-05-01 */
  • ertc_date_set(22, 3, 29, 5);
  • /* set time: 12:00:00 */
  • ertc_time_set(21, 12, 0, ERTC_AM);
  • }
  • /**
  • * @brief configure the ertc timestamp.
  • * @param none
  • * @retval none
  • */
  • void ertc_timestamp_config(void)
  • {
  • exint_init_type exint_init_struct;
  • /* configure nvic */
  • nvic_irq_enable(ERTC_IRQn, 0, 0);
  • /* configure exint */
  • 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_19;
  • exint_init_struct.line_polarity = EXINT_TRIGGER_RISING_EDGE;
  • exint_init(&exint_init_struct);
  • /* enable the timestamp */
  • ertc_timestamp_valid_edge_set(ERTC_TIMESTAMP_EDGE_FALLING);
  • ertc_timestamp_enable(TRUE);
  • /* enable the timestamp int */
  • ertc_interrupt_enable(ERTC_TS_INT, TRUE);
  • }
  • /**
  • * @brief display the current date.
  • * @param none
  • * @retval none
  • */
  • void ertc_time_show(void)
  • {
  • ertc_time_type time;
  • /* get the current time */
  • ertc_calendar_get(&time);
  • /* display the curent time */
  • rt_kprintf("current time: ");
  • /* display date format : year-month-day */
  • rt_kprintf("%02d-%02d-%02d ",time.year, time.month, time.day);
  • /* display time format : hour:min:sec */
  • rt_kprintf("%02d:%02d:%02d\r\n\r\n",time.hour, time.min, time.sec);
  • }
  • /**
  • * @brief display the current timestamp.
  • * @param none
  • * @retval none
  • */
  • void ertc_timestamp_show(void)
  • {
  • ertc_time_type time;
  • /* get the current timestamp */
  • ertc_timestamp_get(&time);
  • /* display the current timestamp */
  • printf("timestamp: ");
  • /* display date format : month-day */
  • printf(" %02d-%02d ", time.month, time.day);
  • /* display time format : hour:min:sec */
  • printf("%02d:%02d:%02d\r\n", time.hour, time.min, time.sec);
  • }

main.c 新建任务:

  • static rt_thread_t tid_ertc = RT_NULL;
  • static void tid_ertc_entry(void *parameter)
  • {
  • ertc_time_type time;
  • char str[10];
  • while(1)
  • {
  • /* get the current time */
  • ertc_calendar_get(&time);
  • //sprintf(str,"%d - %d - %d ",time.year, time.month, time.day);
  • LCD_ShowIntNum(10,160,time.year+2000,4,BLACK, WHITE,32);
  • LCD_ShowIntNum(80,160,time.month,2,BLACK, WHITE,32);
  • LCD_ShowIntNum(120,160,time.day,2,BLACK, WHITE,32);
  • LCD_ShowIntNum(10,200,time.hour,2,BLACK, WHITE,32);
  • LCD_ShowChar(45,200,':',BLACK,WHITE,32,0);
  • LCD_ShowIntNum(70,200,time.min,2,BLACK, WHITE,32);
  • LCD_ShowChar(100,200,':',BLACK,WHITE,32,0);
  • LCD_ShowIntNum(120,200,time.sec,2,BLACK, WHITE,32);
  • rt_thread_mdelay(500);
  • }
  • }

在mian主函数中启动任务:

  • tid_ertc = rt_thread_create("ERTC",
  • tid_ertc_entry, RT_NULL,
  • THREAD_STACK_SIZE,
  • THREAD_PRIORITY, THREAD_TIMESLICE);
  • /* 如果获得线程控制块,启动这个线程 */
  • if (tid_ertc != RT_NULL)
  • rt_thread_startup(tid_ertc);

编译通过后就可以显示实时时间了:

 【总结】RT_Thread就是方便搭积木,每写一个模块,加入任务函数就可以了。当然这个ERTC还是最基本的处理,比如还需要增加实时更改时间等。下一步,将继续以搭积木的方法加入对其他模块的测试。

点赞 关注
 
 

回复
举报
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/10 下一条
【干货上新】电源解决方案和技术第二趴 | DigiKey 应用探索站
当月好物、电源技术资源、特色活动、DigiKey在线实用工具,干货多多~

查看 »

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

 
机器人开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网 1

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表