ERTC
#include "ertc.h"
/**
* @brief 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(21, 5, 1, 5);
/* set time: 12:00:00 */
ertc_time_set(12, 0, 0, ERTC_AM);
}
/**
* @brief display the current date.
* @param none
* @retval none
*/
void ertc_time_show(void)
{
ertc_time_type time;
ertc_calendar_get(&time);
printf("current time: ");
printf("%02d-%02d-%02d ",time.year, time.month, time.day);
printf("%02d:%02d:%02d\r\n\r\n",time.hour, time.min, time.sec);
}
extern uint32_t tick;
uint32_t ertc_get_current_sec(void){
ertc_time_type time;
ertc_calendar_get(&time);
tick++;
return time.sec;
}
#ifndef __ERTC_H
#define __ERTC_H
#include "at32f421_board.h"
#include "at32f421_clock.h"
#include "at32f421.h"
void ertc_config(void);
void ertc_time_show(void);
uint32_t ertc_get_current_sec(void);
#endif
SysTick
在某些情况下,中断处理程序可能会暂时禁用或修改系统定时器(如Systick计数器)的配置,以确保按键中断的响应和处理不受到定时器中断的干扰。
当Systick计数器被暂停或修改后,它就不会再继续累加。这是因为Systick计数器的工作原理是基于系统时钟的定时器,它以固定的频率进行计数。当计数器被暂停或修改时,它的计数值将保持不变,直到再次启用或恢复计数器的正常工作。
因此,按键中断之后要重新启用SysTick的计数器。
#include "sys_tick.h"
uint32_t ticks = 0;
void SysTick_Init(void){
/* 配置嘀嗒时钟源 */
systick_clock_source_config(SYSTICK_CLOCK_SOURCE_AHBCLK_NODIV);
/* 1s/1000 == 1ms */
SysTick_Config((system_core_clock / 1000U));
}
uint32_t get_system_ms(){
return ticks;
}
/**
* @brief systick interrupt handler.
* @param none
* @retval none
*/
void SysTick_Handler(void)
{
ticks ++;
}