示例代码
示例代码帮助开发者快速理解并使用TIMER驱动。
TIMER
该示例工程实现TIMER的定时功能,工程路径:SDK_Folder\projects\peripheral\tim\app_tim。
示例工程流程图
示例设置TIMER0的reload值为SystemCoreClock - 1,因为TIMER0使用系统时钟,因此会看到每秒产生一次中断并打印。设置TIMER1的reload值为SystemCoreClok /100 - 1,TIMER1每10 ms产生中断,TIMER0和TIMER1计数将在10s后停止。
初始化参数
p_params_tim0.id = APP_TIM_ID_0;
p_params_tim0.init.auto_reload = SystemCoreClock - 1;
- id:TIMER模块ID,选择APP_TIM_ID_0(TIMER0)
- auto_reload:自动装载值,设置为SystemCoreClock – 1,表示1s
重要函数
-
初始化模块
app_tim_init(&p_params_tim0, app_tim0_event_handler)
-
开启TIMER计数
app_tim_start(APP_TIM_ID_0)
-
停止TIMER计数
app_tim_stop(APP_TIM_ID_0)
测试验证
连接开发板到PC端,打开串口助手,连接串口,查看串口日志。TIMER0 1s打印一次串口,10s后停止打印。
This is 0th call TIMER0.
This is 1th call TIMER0.
This is 2th call TIMER0.
This is 3th call TIMER0.
This is 4th call TIMER0.
This is 5th call TIMER0.
This is 6th call TIMER0.
This is 7th call TIMER0.
This is 8th call TIMER0.
This is 9th call TIMER0.
This example demo end.