[RTT routine exercises] 4.1 basic timer usage

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.