示例代码
Dual Timer
示例工程运用Dual Timer实现定时打印程序中相关信息。Dual Timer0处于循环模式,第一个周期为1s,随后每5s产生一次中断,串口打印每次中断;Dual Timer1处于一次性模式,中断周期每次延长1s,中断程序将在15s停止,Dual Timer0和Dual Timer1停止计时。工程目录:SDK_Folder\projects\peripheral\dual_tim\app_dual_tim。
示例工程流程图
初始化参数
以Dual Timer0为例,Dual Timer1类似。
#define DEFAULT_AUTO_RELOAD 63999999
#define TIMER_MS(X) (SystemCoreClock / 1000 *(X) - 1)
app_dual_tim_params_t p_params_tim0 = {
.id = APP_DUAL_TIM_ID_0,
.init = {
.prescaler = DUAL_TIMER_PRESCALER_DIV0,
.counter_mode = DUAL_TIMER_COUNTERMODE_LOOP,
.auto_reload = DEFAULT_AUTO_RELOAD,
},
};
- id:Dual Timer0模块ID选择APP_DUAL_TIM_ID_0
- init.prescaler:预分频系数DUAL_TIMER_PRESCALER_DIV0,时钟除以1
- init.counter_mode:采用循环模式DUAL_TIMER_COUNTERMODE_LOOP
- init. auto_reload:自动装载值DEFAULT_AUTO_RELOAD,默认63999999
重要函数
-
初始化Dual Timer函数
app_dual_tim_init(&p_params_tim0, app_dual_tim0_event_handler)
-
开启计数
app_dual_tim_start(APP_DUAL_TIM_ID_0)
-
设置自动装载值
app_dual_tim_set_background_reload(APP_DUAL_TIM_ID_0, TIMER_MS(5000))
-
重新设置计数参数
app_dual_tim_set_params(&p_params_tim1, APP_DUAL_TIM_ID_1)
-
停止计数
app_dual_tim_stop(APP_DUAL_TIM_ID_0)
测试验证
连接开发板到PC端,打开串口助手,连接串口,查看串口日志。DUAL TIMER0 在1s时打印日志,然后每5s打印一次日志,DUAL TIMER1 在1s、3s、6s、10s和15s时打印日志,DUAL TIMER0 和DUAL TIMER1在15s停止计数。
DUAL TIMER0 and DUAL TIMER1 start
This is 0th call DUAL TIMER0.
This is 0th call DUAL TIMER1.
This is 1th call DUAL TIMER1.
This is 1th call DUAL TIMER0.
This is 2th call DUAL TIMER1.
This is 3th call DUAL TIMER1.
This is 2th call DUAL TIMER0.
This is 4th call DUAL TIMER1.
This example demo end.