【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还是最基本的处理,比如还需要增加实时更改时间等。下一步,将继续以搭积木的方法加入对其他模块的测试。
|