1 Introduction
With the continuous development of computers, more and more excellent programming ideas have been proposed and put into practice. Fundamental changes have occurred in some aspects. On the other hand, since the birth of the single-chip microcomputer in the 1980s, it has developed at a rapid speed, but due to the limitations of its physical conditions, the programming of the single-chip microcomputer control system is still limited to the empirical model, and the programming ideas of the newly proposed high-level languages are rarely applied. If the continuously developing programming ideas are combined with the widely used single-chip microcomputer control system, it will definitely greatly promote the further development of the single-chip microcomputer control system. Make it more widely used in various aspects.
In the following specific project, the actual situation has some more stringent requirements for the system. According to the general single-threaded control method, it can no longer meet the actual needs. The specific situation is shown in Table 1 (Table 1 shows part of the requirements for the single-chip microcomputer system in the design of the biochemical analyzer)
Table 1
Table 1 shows only 1/6 of the entire system requirements. The following is an explanation of several items in Table 1.
Object M1 starts action 17, that is, first the stepper motor M1 (forward) rotates to search for the photoelectric switch signal PS1; after the signal is found, it executes 'process 1' twice. Process 1: Beat the motor 30 times. As shown in Table 1, the action is executed from segment C to segment K;
object M2, that is, stepper motor M2, etc., executes action 23 after object M1 completes action 17. Objects M3 and M4 start at the same time at the beginning and execute their own actions respectively. Some objects are independent, while others are related to each other.
The whole project needs to control 16 stepper motors, 21 solenoid valves, 3 pumps, and 1 DC motor. The system requires that the total execution time is 6 seconds, and the system requires the stepper motor to beat at its fastest speed-----40us-60us. If you program in a single-threaded way to let one motor turn to the position, and then turn other motors. In this way, 640us---960us are needed for 16 motors to beat, which can no longer meet the system requirements. This requires the microcontroller to control multiple motors in parallel. This way of driving each motor to start at the same time in parallel can put each motor in place at the same time in the shortest time. While rotating the motor, it is also necessary to detect some switch quantities to determine the position of the motor. In order to achieve this goal. The first solution is provided below (this solution is one of the commonly used solutions, but the author does not recommend this solution. Because this solution is too expensive.).
Solution 1: Hardware implementation.
We can use multiple CPUs to divide the control of each motor into different CPUs for execution, and put the non-conflicting motor control processes into one CPU. The structure is shown in Figure 1.
Figure 1 Multi-CPU implementation scheme structure diagram
As shown in the figure, a master CPU is set in the system, whose function is to communicate with the computer and distribute the instructions issued by the computer to the corresponding slave CPUs. More importantly, the master CPU will monitor the operation of the entire parallel control. Because some of the control processes are related to each other, the master CPU must not only control the execution of each thread, but also take into account the communication between threads. Taking Table 1 as an example, the specific approach is as follows:
1. After the start, the master CPU sends a signal to the slave CPU1, and the slave CPU starts the object M1 to perform action 17, and the slave CPU returns a signal to the master CPU. After receiving the signal, the master CPU sets flag 1. When object M1 completes action 17, the slave CPU sends a signal to the master CPU, and the CPU clears flag 1.
2. After the start, the master CPU sends a signal to the slave M2, and the slave CPU starts object M2. The slave CPU continuously queries flag 1 from the master CPU. When flag 1 is cleared, the slave // controls object 2 to perform action 23.
3. After the start, the master CPU sends a signal to the slave CPU, and the slave CPU starts objects M3 and M4. Perform action 25, then open solenoid valve 6, then delay for 1 second, close solenoid valve 6, perform action 24, then perform action 26, and after action 17 of ////1 is completed, start stepper motor 20, perform action 25, and so on.
As mentioned above, the main CPU sets multiple flags to track the running status of each thread. And these flags are used to carry out the communication between related threads. Whenever a thread runs to a place related to other threads, a flag will be set in the main CPU for other related threads to query. Those unrelated threads can run completely independently. Except for the part that needs to detect the flag, the other parts of the related threads can also run independently.
This multi-CPU control method realizes the parallel operation of the single-chip microcomputer. However, the cost of the multi-CPU control solution is almost doubled, and the communication between the multi-CPUs realized on the basis of hardware takes a little longer time. And it is easy to have interference.
Solution 2 (author recommended): Another implementation method is to simulate the operation of multiple CPUs at the software level. This allows for pseudo-parallel processing of single-chip microcomputers. This implementation method draws on the programming method of computer multi-threading.
The idea of multithreaded programming is to assign several tasks or threads to the CPU at the same time. Of course, the computer CPU cannot actually do several things at the same time, but it divides the time among different threads so that each thread can make some progress. If a thread cannot proceed, such as the keyboard input required by the thread has not been obtained, then the work of another thread will be transferred. Usually, the CPU switches between threads very quickly, making people feel as if all threads are being carried out at the same time.
There is a very important link in multi-threaded programming: the communication and control problem between threads
In multithreaded programming, each thread uses code to provide thread behavior and data to supply code operations. Multiple threads process the same code and data at the same time, and different threads may have different codes and data. In fact, the code and data parts are quite independent and can be provided to threads when needed. Therefore, it is often the case that several threads use the same code and data, which will result in the following situation:
When one thread is calling data, another thread may be modifying the data. Then the data called by the previous thread becomes uncertain. This will affect the entire operation result. In order to avoid this problem, communication and control between threads are particularly important in multi-threaded programming. When transplanting the idea of multi-threading to the single-chip control system, this point should be paid special attention to. Because the implementation of system multi-threading operation is still a single-threaded operation from its most basic level; its implementation is ultimately based on the high speed of the computer. It divides the benchmark time of the system operation into many time slices, and assigns each time slice to different threads. In this way, each thread moves forward one step in a benchmark time, and then runs the next benchmark time, and repeats. In this way, from the user level, each thread is synchronized. As long as the speed is fast enough, the division of time slices will not affect the application at the user level, so multi-threaded operation can be realized. In recent years, the speed of single-chip microcomputers has increased significantly, which makes it possible to transplant the idea of multi-threading to single-chip microcomputer control systems.
There are 39 input signals in the whole project; the maximum number of objects to be queried in parallel is 61. The system requires the stepper motor to beat at the fastest speed. The time is about 40us---60us. In order to ensure the stability and flexibility of the stepper motor beat, I chose a timer inside the DSP, and the timing time is 1/3-1/5 of the beat time. It is set to 10us interrupt. This time is the benchmark time for the entire system to run. During this time, the system needs to query all objects and beat the corresponding stepper motor. In some threads, multiple sampling is required. In other words, within this system time, all threads must move forward one step.
Just like a computer, this base time is divided into multiple time slices. Each time slice is assigned to a different thread. In this case, the execution of each thread is intermittent. This is fundamentally different from simulating multi-threading with hardware. In this way, it is necessary to apply the discontinuity of the execution of each thread and ensure the continuity of the operation of each thread. This places high demands on software design, which is also one of the difficulties in the method of simulating multi-threading with software in single-chip control systems. In order to solve this problem, a thread progress indicator can be provided in each thread to mark the running progress of the thread, that is, a variable is used to record each step of the thread; as shown in Figure 2
Figure 2
The system sets the thread progress indicator to guide the continuous operation of the thread, and polls all objects in an interrupt. Its programming structure is as follows:
Clock interrupt:
Thread 1:
Thread progress flag:
1: ;
2: ;
3;
Thread 2:
Thread progress flag:
1: ;
2: ;
. . . . . .
There are many things to pay attention to when simulating multithreading with software.
If a 20-megabyte DSP is used to implement the control function, and the fastest beat speed of the stepper motor is 40us---60us, the following calculation results are obtained: take 1/4, that is, 10us to generate an interrupt, and query 61 objects in the interrupt. The execution time of a single instruction of a 20-megabyte DSP is approximately 50ns. The number of statements that can be executed in one interrupt = 10us/50ns = 200. If it is divided into 61 objects, each object will only receive 3 instructions, which is not enough. Even if a 60-megabyte DSP is selected, there are only 9 instructions. If the flexibility of the beat is sacrificed, 60us is used as an interrupt. Then the number of instructions allocated to each object is only 36, which is barely enough. If there are more objects to be controlled, the flexibility of software programming will be further compressed.
An extended multi-threaded simulation method is proposed below, as shown in Figure 3
Figure 3
s1, s2 are the benchmark time of system operation , M1.1 is the first object in the first object group. M1.2 is the second object in the first object group.
This extended multi-threaded simulation method still divides the system time into multiple time slices. The difference from the above is that several threads (objects) form a thread group and share the same time slice. For example, when the system runs to the first time slice of the first benchmark time, it is occupied by the first thread of the shared time slice. When the system runs to the first time slice of the second benchmark time, it is occupied by the second thread of the shared time slice, and so on. This extended mode can flexibly control the ratio of threads to time used, but this is at the expense of running time.
Conclusion: This paper expands the programming concept of single-chip control system, and transplants the multi-threaded programming, which originally belongs to the computer high-level language programming concept, into the single-chip control system. This method enables the single-chip control system to work in systems that require high system speed, and plays a demonstration role in transplanting other high-level language programming concepts, such as the processing of event handles, into the single-chip control system.
References
[1] Liu Yongxin, ed. Introduction and Improvement of Windows C Programming. Tsinghua University Press, 1999, 6.
[2] Li Chaoqing, ed. Principles of Microcontrollers and Interface Technology, 1994.
Previous article:CANBUS program based on single chip microcomputer (C language)
Next article:MCU communication protocol
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- Transformer coupled push-pull power amplifier circuit
- Hangzhou Dahua Hardware Engineer Interview Questions
- UWB Indoor Positioning Technology Solution
- iTOP-3399 development board Linux system compilation and burning - obtain/install/compile Linux source code
- Questions about writing data from DSP to DATARAM in PRU1
- Please recommend a buck-boost chip
- The weekly evaluation information will be here soon~ Have a happy holiday and happy work~~
- TLP3547 Evaluation Board Comprehensive Evaluation Report
- Crazy Shell AI open source drone PID basics
- What are the main differences between DSP's C language and host's C language?