Small RTOS has small RAM requirements and is very suitable for systems with relatively few resources such as microcontrollers. RTOS has multi-tasking, strong real-time performance, and a scalable kernel, making it easier to design, expand, and maintain real-time applications. The introduction of RTOS ideas has changed the traditional microcontroller software design method, making it no longer a single-thread structure. By dividing the application program into several independent tasks, RTOS greatly simplifies the application design process. This article briefly analyzes the design ideas of Small RTOS and the application of the message queue communication mechanism based on the software design of electronic lockers based on microcontrollers.
1. Basic principles of Small RTOS51
Small RTOS51 is a very small kernel that is fully integrated in the KEIL C51 compiler. It only takes up less program storage space and can run in a 51 microcontroller system without external data memory. The kernel is responsible for the initialization and opening of the system, scheduling other tasks, and reasonably allocating CPU time between different tasks according to the priority of each task. The kernel can generally provide functions such as task scheduling and interrupt services. Corresponding a program to a task, each separate and independently executed part of the task is called a thread. All event-driven and time-driven are reflected in setting the corresponding task identification and thread identification. When the hardware environment is certain, based on these indicators, by arranging the interrupt response method in the system and adjusting the task scheduling algorithm, the method of setting environment variables is used, so that after the interrupt exits, you can return to any one of the multiple setting entries for execution, which is effective It effectively solves the key problem of flexible switching of foreground and background task threads. The user tasks of Small RTOS 51 have running state, ready state, waiting and suspending, and interrupt service routines. At any moment, the status of the task is one of these four states. In order to save memory, the OS cannot dynamically create and delete tasks. After using the function OSSTART() to start the multi-tasking environment, the OS creates all tasks, sets them to the ready state, and starts running the highest priority task. Only when all tasks with a higher priority than it are transferred to wait. Only ready tasks can enter the running state. The scheduler considers the highest priority task in the "task ready list" as the next task to be executed.
The task calls the os_wait function to suspend the current task and wait for one or several interval (K_IVL), timeout (K_TMO), and signal (K_SIG) events. If the event you are waiting for has occurred, continue to execute the current task; if the event you are waiting for has not occurred, set the corresponding wait flag, suspend the task, and switch to the task switching program section to switch to the next task.
Small RTOS51 assigns the initial value to the register by executing the function init (), calls the function OSStart () to initialize the stack of each task and execute the first task. The reason for selecting the first task for execution is the priority level of the task. Task scheduling is completed during the selection process.
1.1 Communication between tasks
The communication between tasks uses the message queue, which goes through three steps: creating the message queue OSQCreate(), sending the message OSQPost() to the message queue, and getting the message OSQPend(). When calling the OSQPost () function to send a message, if there is already a task waiting for the message, it will immediately switch to the task waiting for the message. If the message queue is full, the message cannot be delivered and an error message will be returned. If the message queue is not full and there are no other tasks waiting for the message, it is queued. It is worth noting that when the queue is full, this function does not suspend the current task, so the task calling this function should determine the return condition. If the message is full, the current task should be suspended. When receiving a message, if the message queue is empty, the task of fetching the message will be suspended until there is a message in the message queue.
2. Application examples
2.1 Hardware platform
Taking electronic lockers used in large supermarkets and bookstores as an example, the system block diagram is as follows
The maximum number of cargo storage boxes that can be controlled by this electronic locker is 32, with 16 boxes on the left and right respectively. Only 16 control channels are shown in the above figure. The control chip uses an AT89C55 microcontroller, equipped with a real-time clock module, a barcode reader, a liquid crystal display module, and a thermal printer. It is designed as three PCB boards.
1 Main control board, performs human-computer interaction for user's bag storage and bag retrieval, saves the bag storage password, displays the user's password input during the bag retrieval process and the status of all 32 bag storage boxes.
2. The door control panel consists of three parts. The first part detects items in the box and includes 16 channels of infrared detection input and 16 channels of infrared emission control drive. The second part is 16-way relay control, and the box opening control consists of 16 small DC motors to form the door opening control of 16 boxes. The third part is door status detection, which detects the opening/closing of 16 doors.
3 Bar code printing and voice prompts for access packages. The entire system is coordinated by 4 microcontrollers. The main control microcontroller manages the operations of package deposit users and records their historical parameters. Then it issues instructions to the slave microcontrollers, allocates empty boxes and prints out the package deposits. Article, to the user.
2.1 Software design
The electronic package locker mainly includes modules such as button processing, display processing, and communication processing, which are handled by interrupt processing, barcode reading and serial port communication. Therefore, the tasks can be divided into four tasks: key display, data reception, command processing, and data transmission.
Take the main control microcontroller 1 as an example to explain the main design idea of receiving the user's password input when pressing the package, key value processing, password display and box status display, and sending unboxing commands.
After the main program initializes the system, it calls the function OSStart() to set up the multi-tasking environment, establishes a task stack for each task, and sequentially establishes 4 tasks, waiting for the corresponding task to wake up. Users only need to define the function they want to implement as a task, declare the task function name and its priority in cONfig.h, and write their own code segments. There is no need for up and down calls in traditional programming. relationship, the system will automatically run the code. The transplantation of Small RTOS51 mainly involves configuring three files: OS_CPU.H, OS_CFG.H and CONFIG.H. The OS_CFG.H file is mainly used for operating system reduction and clock configuration.
Define 4 tasks in the CONFIG.H file
#ifndef…. .
…………
#endif
Due to space limitations, the procedures for sending data to microcontrollers 2 and 3 and receiving data from microcontroller 2 are omitted.
4 Conclusion
The use of real-time multi-tasking operating systems makes programming more efficient, maintenance and modifications more convenient, so it is currently widely used, especially in embedded systems. In the application field of low-end 51 microcontrollers, it is also of practical significance to use the idea of real-time multi-tasking operating system and use the Small RTOS multi-tasking platform for system application design and improvement.
The innovation of this article is to apply the idea of Small RTOS multi-tasking in the software design of a specific electronic locker system controlled by a single-chip microcomputer, and introduce the programming method of task communication based on message queues.
Previous article:What are the advantages and disadvantages of the microcontroller at89s52 compared with other microcontrollers?
Next article:Why is the 51 crystal oscillator 11.0592?
Recommended ReadingLatest update time:2024-11-16 09:27
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- [Silicon Labs Development Kit Review] + Key test examples and serial port output examples
- 555 classic circuit diagram: 555 test circuit
- Last day! TI live broadcast with prizes, ACI motor actual control evaluation example, in-depth understanding of TI C2000
- Here is a Tai Chi Bagua diagram of the radio frequency industry
- [Chuanglong TL570x-EVM] Example project running
- How to download KEIL DFP package experience sharing
- Detailed explanation of the flash encryption principle and steps of ESP32, ESP32S2, and ESP-C3 series
- After the stm32 serial port is initialized, the microcontroller does not run automatically, most likely because printf is used
- STMicroelectronics Industry Summit 2020 invites you to attend!
- EEWORLD University----[High Precision Laboratory] Motor Drive: Motor Drive Technology