stm32-learning experience summary——UCOSIII-software timer

Publisher:快乐舞步Latest update time:2018-08-31 Source: eefocusKeywords:stm32  UCOSIII Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Software Timer Introduction 
UC/OS-III provides software timer service. The essence of timer is a decrementing counter. When the counter is reduced to 0, it can trigger the execution of some action. This action is implemented through callback function. The callback function is defined by the user. It can be a simple LED light or a motor. When the timer is finished, the defined callback function will be called immediately. The application can have any number of timers. 
Note! Do not use blocking calls or functions that can block or delete timer tasks in the callback function, such as: OSTimeDly() OSTimeDlyHMSM() and other functions. 
The relevant code of UCOSIII's software timer service is in the OS_TIMR.c file. When OS_CFG_TMR_EN in OS_CFG.H is set to 1, the software timer service is enabled. The 
time resolution of the timer in UCOSIII depends on the time base frequency, that is, the value of OS_CFG_TMR_TASK_RATE_HZ, in HZ. If the time base frequency is set to 10Hz, the resolution of all timers is one tenth of a second.

Common functions of software timer: 
1. OSTmrCreate() creates a timer and specifies the operation mode 
2. OSTmrDel() deletes the timer 
3. OSTmrRemainGet() gets the remaining time of the timer 
4. OSTmrStart() starts the timer counting 
5. OSTmrStateGet() gets the current timer status 
6. OSTmrStop() stops the countdown of the timer

Before using the timer, you must call the OSTmrCreate() function to create it and set related parameters. The function prototype is as follows:

OSTmrCreate (OS_TMR *p_tmr, (1)
             CPU_CHAR *p_name, (2)
             OS_TICK dly, (3)
             OS_TICK period, (4)
             OS_OPT opt, (5)
             OS_TMR_CALLBACK_PTR p_callback, (6)
             void *p_callback_arg, (7)
             OS_ERR *p_err) (8)

The meaning of each parameter: 
(1): Timer, you need to define it yourself, such as: OS_TMR tmr1; //Define a timer 1 
(2): The name of the timer; 
(3): Set the period of the timer's first operation; 
(4): Set the period of the timer's operation after the second operation; 
(5): Configuration mode: OS_OPT_TMR_ONE_SHOT: Single timer 
OS_OPT_TMR_PERIODIC: Periodic mode 
(6): Timer callback function; 
(7): Timer transfer parameters; 
(8): Returned error code;

Note: 
When opt is set to a single timer: the timer starts counting down from the initial value (that is, the parameter dly in the OSTmrCreate() function) until it reaches 0, the callback function is called and the timer stops. The timer of a single timer is executed only once. 
When opt is in periodic mode, when the countdown is completed, the timer calls the callback function, resets the counter and restarts the timing, and continues in a cycle. If the parameter dly is set to 0 when calling the function OSTmrCreate() to create a periodic timer, then each cycle of the timer is period. 
As shown in the following figure: 
Single timer mode, only executed once 
One-shot timer mode, only executed once 
. In the same single timer mode, when the timer is working, calling the OSTmrStart() function will restart the timer, as shown in the following figure. 
One-shot timer 
In periodic mode, there are two types: initial delay and no initial delay. When there is an initial delay, when the countdown is completed, the timer calls the callback function, resets the counter and restarts the timing, and continues in a cycle. If the parameter dly is set to 0 when calling the function OSTmrCreate() to create a periodic timer, then each cycle of the timer is period. As shown in the figure below, 
Cycle mode, with initial delay 
when there is no initial delay, the function OSTmrCreate() parameter dly is used to determine the first cycle, and the counter value is reset to period at the beginning of each subsequent cycle. 
Cyclic mode, no initial delay

Finally, I attach the source code for creating a software timer

    OSTmrCreate((OS_TMR *)&tmr1,    

                (CPU_CHAR *)"tmr1",       

                (OS_TICK) 20,           

                (OS_TICK) 100,         

                (OS_OPT )OS_OPT_TMR_PERIODIC, 

                (OS_TMR_CALLBACK_PTR)tmr1_callback,

                (void *)0,            

                (OS_ERR *)&err);


Keywords:stm32  UCOSIII Reference address:stm32-learning experience summary——UCOSIII-software timer

Previous article:STM32 Experiment 1: Timer interrupt generates two signals of different frequencies simultaneously
Next article:STM32 timer (TIM) general timer

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号