定时器(timer)
创建两个动态定时器,一个是单次定时,一个是周期性定时,并让周期定时器运行一段时间后停止运行 硬件定时器,定时器的回调函数将在系统时钟中断里调用。 软件定时器,定时器的回调函数将在定时器线程中调用。
"board.h"
#include "rtthread.h"
#include
static rt_timer_t timer_1;
static rt_timer_t timer_2;
static int count_c = 0;
static int short_c = 0;
static void timeout_1(void *parameter);
static void timeout_2(void *parameter);
/* timer_1 is run 10 times then stop */
static void timeout_1(void *parameter){
rt_kprintf("periodic timer_[1] is start. --> count is [%d]\n",count_c);
if(count_c++>=90){
rt_timer_stop(timer_1);
rt_kprintf("timer_[1] was stoped!\n");
}
}
static void timeout_2(void *parameter){
++;
short_c
rt_kprintf("short timer_[2] is start. -->short is [%d]\n", short_c);
}
int main(void)
{
// create timer_1 and flag is periodic model
= rt_timer_create("timer1", timeout_1,RT_NULL,90,RT_TIMER_FLAG_PERIODIC);
timer_1
// create timer_2 and flag is short model(count only once then stop)
= rt_timer_create("timer2", timeout_2, RT_NULL, 30, RT_TIMER_FLAG_PERIODIC);
timer_2
if(timer_1!=RT_NULL){
rt_timer_start(timer_1);
}
if (timer_2 != RT_NULL)
{
rt_timer_start(timer_2);
}
return 0;
}