Timers are divided into software timers and hardware timers.
A hardware timer is a type of peripherals provided by the microcontroller. It can generate scheduled events without occupying CPU time. The advantage is that the timing precision is high, independent from the CPU, and many other functions such as PWM can be flexibly configured.
The software timer is a scheduled interface provided by the operating system. Its accuracy depends on the tick time of the operating system. The Tick of RTOS is built on the hardware timer, and it will not change once it is started on. However, it is not limited by the number of hardware timers and is easy to manage in a unified manner in the operating system. In RTT, the software timer is also divided into single trigger and periodic trigger.
This example shows a periodically triggered software timer.
Program
#include <rtthread.h>static rt_timer_t timer1;static rt_uint8_t count;static void timeout1(void *parameter){ rt_kprintf("periodic timer is timeout, %d.\n", count); count++; if (count >= 8) { rt_kprintf("stop it!\n"); rt_timer_stop(timer1); count = 0; }}int rt_application_init(){ timer1 = rt_timer_create("timer1", timeout1, RT_NULL, 10, RT_TIMER_FLAG_PERIODIC); if (timer1 != RT_NULL) rt_timer_start(timer1); return 0;}
Result
periodic timer is timeoutperiodic timer is timeoutperiodic timer is timeoutperiodic timer is timeoutperiodic timer is timeoutperiodic timer is timeoutperiodic timer is timeoutperiodic timer is timeout