定时器
优质
小牛编辑
135浏览
2023-12-01
单次定时器和周期性定时器的使用
单次定时器和周期性定时器的使用
源码
/* * Copyright (c) 2006-2018, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2018-08-24 yangjie the first version *//** 程序清单:定时器例程** 这个例程会创建两个动态定时器,一个是单次定时,一个是周期性定时* 并让周期定时器运行一段时间后停止运行*/#include <rtthread.h>/* 定时器的控制块 */static rt_timer_t timer1;static rt_timer_t timer2;static int cnt = 0;/* 定时器1超时函数 */static void timeout1(void *parameter){ rt_kprintf("periodic timer is timeout %d\n", cnt); /* 运行第10次,停止周期定时器 */ if (cnt++ >= 9) { rt_timer_stop(timer1); rt_kprintf("periodic timer was stopped! \n"); }}/* 定时器2超时函数 */static void timeout2(void *parameter){ rt_kprintf("one shot timer is timeout\n");}int timer_sample(void){ /* 创建定时器1 周期定时器 */ timer1 = rt_timer_create("timer1", timeout1, RT_NULL, 10, RT_TIMER_FLAG_PERIODIC); /* 启动定时器1 */ if (timer1 != RT_NULL) rt_timer_start(timer1); /* 创建定时器2 单次定时器 */ timer2 = rt_timer_create("timer2", timeout2, RT_NULL, 30, RT_TIMER_FLAG_ONE_SHOT); /* 启动定时器2 */ if (timer2 != RT_NULL) rt_timer_start(timer2); return 0;}/* 导出到 msh 命令列表中 */MSH_CMD_EXPORT(timer_sample, timer sample);