timer_sample.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-08-24 yangjie the first version
  9. */
  10. /*
  11. * 程序清单:定时器例程
  12. *
  13. * 这个例程会创建两个动态定时器,一个是单次定时,一个是周期性定时
  14. */
  15. #include <rtthread.h>
  16. /* 定时器的控制块 */
  17. static rt_timer_t timer1;
  18. static rt_timer_t timer2;
  19. static int cnt = 0;
  20. /* 定时器1超时函数 */
  21. static void timeout1(void *parameter)
  22. {
  23. rt_kprintf("periodic timer is timeout %d\n", cnt);
  24. /* 运行第10次,停止周期定时器 */
  25. if (cnt++ >= 9)
  26. {
  27. rt_timer_stop(timer1);
  28. rt_kprintf("periodic timer was stopped! \n");
  29. }
  30. }
  31. /* 定时器2超时函数 */
  32. static void timeout2(void *parameter)
  33. {
  34. rt_kprintf("one shot timer is timeout\n");
  35. }
  36. int timer_sample(void)
  37. {
  38. /* 创建定时器1 周期定时器 */
  39. timer1 = rt_timer_create("timer1", timeout1,
  40. RT_NULL, 10,
  41. RT_TIMER_FLAG_PERIODIC);
  42. /* 启动定时器1 */
  43. if (timer1 != RT_NULL) rt_timer_start(timer1);
  44. /* 创建定时器2 单次定时器 */
  45. timer2 = rt_timer_create("timer2", timeout2,
  46. RT_NULL, 30,
  47. RT_TIMER_FLAG_ONE_SHOT);
  48. /* 启动定时器2 */
  49. if (timer2 != RT_NULL) rt_timer_start(timer2);
  50. return 0;
  51. }
  52. /* 导出到 msh 命令列表中 */
  53. MSH_CMD_EXPORT(timer_sample, timer sample);