Introduction
Traditional microcontroller programs generally use a single-task mechanism. Single-task systems have the advantages of being simple, intuitive, and easy to control. However, since the program can only be executed in sequence and lacks flexibility, interrupt functions can only be used to process some shorter tasks in real time, which is extremely inconvenient to use in more complex applications. The emergence of embedded multi-tasking operating systems solves this problem. In a multitasking system, multiple parallel tasks can be executed at the same time, and tasks can jump to each other. However, while the embedded operating system provides powerful functions, it also brings problems such as large code volume, complex structure, high hardware requirements, difficult development and high cost. In many cases, only simple multi-tasking operations are needed to meet actual needs. The simple multi-tasking mechanism designed in this article only adds a very small amount of C language code, does not require the use of assembly, and does not require any modification to the original program. With big changes, multitasking is possible.
The core of the real-time operating system RTOS is interrupts, which use interrupts to switch tasks. In most RTOS such as μC/OS-II, each task has its own stack, which is used to save some information of the task. Information is transferred between tasks through semaphores, mailboxes, message queues, etc. In many cases, these functions are not needed. It is only necessary to switch the microcontroller to different working states after receiving the control signal. That is, as long as the task is switched, there is no need to save the relevant information of the task. Abandoning these complex functions can make the program structure simpler and easier to use.
Comparison of the two mechanisms in application examples
The following uses an application example to illustrate the idea of this design. To design an intelligent security system, its functions include: performing alarm work when someone intrudes; users can set functions through the keyboard board; the main board can communicate with the management center, and when disasters such as fires and earthquakes occur, the management center can notify user. Its structure is shown in Figure 1. Under normal conditions, the motherboard's CPU continuously scans the status of each sensor. When an abnormal signal from the sensor (someone breaks in) is detected, the CPU enters the intrusion alarm state and performs tasks such as ringing the alarm, dialing the head of the household, and notifying the management center. When a fire or earthquake occurs, the management center sends a serial port code to the motherboard CPU, causing the CPU to enter the disaster alarm state and perform operations such as ringing the alarm bell and making voice alarms. When the user needs to perform function settings, the motherboard CPU can enter the function setting state through the keyboard board. Therefore, the motherboard's CPU has 4 different working states.
Figure 1 Structural diagram of intelligent security system
If a single task mechanism is adopted, the program flow of the motherboard is shown in Figure 2. In the main function, the sensor status is cyclically detected. If there is an abnormality, the alarm function is called. The disaster alarm and function settings are completed in the serial port interrupt. This single-task structure has two disadvantages. First of all, in various abnormal states, the program needs to constantly detect whether a removal signal is received. This requirement is difficult to achieve when the program code is large and there is a lot of execution work. Secondly, it is very difficult to switch between states. In order to achieve modularity, programs written in C language generally have a large number of functions and many nested levels of function calls. It is necessary to jump out from a deeper nest immediately to the main one. Functions are very difficult. The general solution is to use the C51 library functions setjmp() and longjmp() to implement long jumps, but these two functions are powerless inside the interrupt function; or to embed assembly instructions in the C function. Although assembly instructions can be used to achieve long-distance jumps in the program, the debugging process of this method is very cumbersome and the program has poor portability. For designers who are accustomed to programming with C51 and do not want to use assembly, this part of the program is a difficult problem.
Figure 2 Single task mechanism program flow
Program structure to implement multi-tasking mechanism
This article provides a method to implement a highly portable multi-tasking program without using assembly instructions at all. The program flow is shown in Figure 3.
Figure 3 Multi-task structure program flow
实现这个多任务机制的完整源代码如下:
word idata PC_Value, SP_Value; //储存中断返回点、SP初值的全局变量
byte idata Ctrl_Code; //控制任务切换的全局变量,在中断函数里被赋值
void main()
{
Initial(); //初始化函数,与程序结构无关
SP_Value=SP; //获取SP的初始值
PC_Value=Get_Next_PC(); //获取下一条指令的地址
EA=1; //获取PC、SP初值后再开中断保证稳定性
if(Ctrl_Code!=0)
SP=SP_Value; //重置堆栈指针,防止堆栈溢出
switch( Ctrl_Code) //任务入口地址,即中断的返回点
{
case 1: goto TASK1;
case 2: goto TASK2;
case 3: goto TASK3;
default: break;
}
TASK1: for( ; ; )
{ //任务1代码 }
TASK2: for( ; ; )
{ //任务2代码 }
TASK3: for( ; ; )
{ //任务2代码}
}
word Get_Next_PC(void); //获取下一条指令的地址
{
?word address;
?address=*((unsigned char *)SP); //PC的高字节
?address <<= 8;
?address+=*((unsigned char *)(SP-1)); //PC的低字节
?return address+4; //查看反汇编代码,计算所得
}
void Chuan_Kou_Interrupt(void) interrupt 4 using 0
{
byte a1,a2;
a1=a1*a2;
*((unsigned char *)(SP-5))=PC_Value>>8;
*((unsigned char *)(SP-6))=PC_Value & 0x00ff;
{
//接收串口代码并根据代码修改Ctrl_Code的值
//其他操作
}
}
Task Scheduling Principle and Implementation
The overall idea of the program is to place several infinite loops in the main function as the task framework, that is, each task is an infinite loop, and interrupts are used for task switching. Take the security system just mentioned as an example. Since the motherboard, keyboard, and management center communicate through the serial port, the serial port is an ideal interrupt source for triggering task switching. The program sets a general entrance for all tasks and places it in the main function. Every time the serial port interrupt returns, it must first pass through this general entrance. The value of the task control variable (global variable) is checked at the general entrance. The task control variable has been interrupted in the serial port. is assigned a value, and its value determines which task to switch to.
In the design, the normal state, intrusion alarm state, crisis alarm state, and function setting state can be regarded as task 1, task 2, task 3, and task 4 respectively. The motherboard CPU usually works in the normal state, that is, task 1; when the serial port receives the crisis code from the management center, set Ctrl_Code = 3 in the serial port interrupt function. After the interrupt returns, it will switch to task 3; similarly, the function setting code of the keyboard is received After that, it will switch to task 4; since intrusion detection is the responsibility of the motherboard CPU itself, if an intrusion is detected and needs to be switched to the intrusion alarm state, a serial port interrupt can be generated through keyboard relay, that is, a serial port data is sent to the keyboard and a request is made. Keyboard echo. In this way, switching of various states is achieved.
Three key issues need to be solved to implement task scheduling:
① Obtain the program address of the task entry point. Since the value of the program counter PC cannot be directly obtained and modified using C language, the PC value will be pushed onto the stack when calling a function. Using this feature, the entry address can be obtained from the stack by calling the Get_Next_PC function before the task entry. In Get_Next_PC, SP is the stack pointer, and 4 is added to the obtained PC value to get the task entry address, because looking at the disassembly window, it can be seen that passing the function return value to the global variable PC_Value requires two 2-byte long mov instructions.
② Modify the interrupt return address. The operation of modifying the interrupt return address is similar to obtaining the PC value, both by modifying the contents of the stack. However, due to the characteristics of the compiler itself, when entering an interrupt, in addition to pushing the return address onto the stack, the compiler will also calculate the changes made by itself and the functions it calls to the registers ACC, B, DPH, DPL, PSW, R0 ~ R7 , and push the registers it thinks have been changed onto the stack for protection. If the stack structure changes as the contents of the interrupt function change, there is no way to calculate the position of the interrupt return address on the stack. The solution is to add the keyword using 0 when defining the interrupt function to tell the compiler that the interrupt function and the functions it calls will use register group 0, so that the working registers R0~R7 will not be saved. ACC, PSW, DPH, and DPL have been used when operating PC_Value. Define two variables a1 and b1 at the beginning of the interrupt function and multiply them so that the B register is also pushed onto the stack, so that the stack structure is fixed.
③Prevent stack overflow. Since the compiler will push the current address into the stack when calling a function and pop it out when returning, when task switching or interruption occurs multiple times during the function call, the stack will eventually overflow because it can only be entered but not exited. This is unacceptable. Therefore, the SP value should be saved immediately after initialization at the beginning of the main function, and then the SP value should be restored to the initial value after each task switch. This can effectively prevent stack overflow.
Conclusion
Based on the above comparison and analysis, it can be seen that this method of implementing a multi-task mechanism has the following advantages: compared with programs using a single-task mechanism, its structure is simple and clear, and easy to control; it uses interrupts and stacks to achieve long-term processing of task switching. Jumping does not require the use of assembly language at all, and has strong portability; the added code amount is minimal, the real-time performance is good, and program development time is saved.
The method introduced above has been tested and applied to several actual projects, including intelligent community security systems, automotive CAN bus control systems, etc., and has achieved good results. As long as slight modifications are made according to the specific hardware and compilation environment, it can also be applied to other microcontroller systems.
References
1. Zhang Peiren. Principles and Applications of MCS-51 Microcontroller Programming Based on C Language. Beijing: Tsinghua University Press, 2003.1.
2. Hu Dake et al. Embedded Development Guide based on Microcontroller 8051. Beijing: Electronic Industry Press, 2003.1
Previous article:Application of EM78 series microcontroller in infrared remote control system
Next article:Principle and application of microprocessor real-time clock chip MM58167B
Recommended ReadingLatest update time:2024-11-16 16:24
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
- EEWORLD University ---- Basic Electronic Components
- Operational amplifier classification and selection
- Can stm32f103 collect a sine wave signal with an amplitude of 200mv and a frequency of 1KH?
- EEWORLD University - How to use switching chargers to design safe and reliable products with long standby time and powered by micro batteries
- What is the function of the TCP datagram header data offset parameter? I have read a lot of information but have not seen its usage.
- Why can't I download the program on my LPC?
- How to find related product models based on silk screen on TI official website
- Tesla exposed a low-level vulnerability: Use Raspberry Pi to DIY car keys, unlocking in just 90 seconds
- Instructions for using 86 box modbus
- Xinhua Dictionary of Power Supply--A brief review of the book "Basics of Power Supply Design"