A multi-tasking mechanism and application based on C51

Publisher:WhisperingSoulLatest update time:2006-11-28 Source: 电子设计应用Keywords:program Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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

Keywords:program Reference address:A multi-tasking mechanism and application based on C51

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

Synopsys Announces Acquisition of Code Dx
Synopsys Announces Acquisition of Code Dx Expanding Application Security Portfolio to Meet Changing Market Needs As modern technology continues to develop, hackers are also looking for opportunities to attack new targets. In order to prevent problems before they occur, security te
[Embedded]
Synopsys Announces Acquisition of Code Dx
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号