[RTT例程练习] 4.4 定时器控制
定时器可以通过rt_timer_control
(),来改变定时器的某些特性,比如定时时间。
这里,一开始定时周期是100,并且在程序中使用
rt_tick_get()-tick来确认定时周期是否如预设那样。当8次之后,使用
rt_timer_control
(),将定时周期改为3000。实际效果通过打印的tick信息和直观感受都可以验证。
程序:
#include <rtthread.h> static rt_timer_t timer1; //static rt_timer_t timer2; static rt_uint8_t count; static rt_tick_t tick; static void timeout1(void* parameter) { rt_tick_t timeout = 3000; rt_kprintf("periodic timer is timeout\n"); rt_kprintf("the time set is %d\n", (rt_tick_get()-tick)); tick = rt_tick_get(); count ++; if (count >= 8) { rt_timer_control(timer1, RT_TIMER_CTRL_SET_ONESHOT, (void *)&timeout); rt_timer_control(timer1, RT_TIMER_CTRL_SET_TIME, (void *)&timeout); count = 0; } } int rt_application_init() { timer1 = rt_timer_create("timer1", timeout1, RT_NULL, 100, RT_TIMER_FLAG_PERIODIC); if (timer1 != RT_NULL) rt_timer_start(timer1); return 0; }
结果:
periodic timer is timeout the time set is 100 periodic timer is timeout the time set is 100 periodic timer is timeout the time set is 100 periodic timer is timeout the time set is 100 periodic timer is timeout the time set is 100 periodic timer is timeout the time set is 100 periodic timer is timeout the time set is 100 periodic timer is timeout the time set is 100 periodic timer is timeout the time set is 3000 periodic timer is timeout the time set is 3000 periodic timer is timeout the time set is 3000 periodic timer is timeout the time set is 3000