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
. 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.
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,
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.
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);
Previous article:STM32 Experiment 1: Timer interrupt generates two signals of different frequencies simultaneously
Next article:STM32 timer (TIM) general timer
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- P22-009_Butterfly E3106 Cord Board Solution
- Keysight Technologies Helps Samsung Electronics Successfully Validate FiRa® 2.0 Safe Distance Measurement Test Case
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Pingtouge RISC-V development board--STEP2--CDK environment construction learning, let the LED light start flashing
- Getting Started with MSP430F5529
- HPM6750 Evaluation of Pioneer Semiconductor Step 4 (FreeRTOS+TinyUSB+LVGL) Comprehensive Example
- Understanding the configuration of C2000 series CMD files
- MicroPython Hands-on (01)——After the Spring Festival, I bought a K210 chip AI development board
- FP8102 1A single-cell lithium-ion linear charging IC
- Design of Passive Acoustic Target Detection Platform Based on Single Chip Microcomputer and DSP
- 6. "Wanli" Raspberry Pi car - wiringPi learning (PWM and external interrupt simulation timer)
- Design of low power portable electrocardiograph based on MSP430
- EEWORLD University Hall----Live Replay: Application of TI mmWave Radar Sensors in Smart Home and Smart Security