Experience 1: Use "software trap + program password" to deal with the flying of PC pointer
When the CPU is disturbed by external factors, the PC pointer may sometimes fly to another program segment or jump to a blank segment. In fact, if the PC pointer flies to a blank segment, it is easy to handle. Just set up a software trap (intercept instruction) in the blank segment to intercept the program to the initialization segment or program error handling segment. However, if the PC pointer flies to another program segment, what should the system do? Xiaojiang recommends a method here - program password, the idea is as follows:
1. First, the program must be modular. Each module (subroutine) performs one function. Each module has only one exit (RET).
2. Set up a module (subroutine) ID register.
3. Assign a unique ID number to each subroutine.
4. Whenever a subroutine is finished, before returning (RET),
First, send the ID number of this subroutine into the ID register.
5. After returning to the parent program, first determine the ID number in the ID register.
If it is correct, the execution continues; if it is incorrect, it means that the PC pointer may have jumped to the wrong position and the subroutine did not return as expected. At this time, the program will be intercepted to the initialization segment or the program error handling segment.
Extended reading: How to become a programming expert
This method is like setting up several sentries in the program. Each time a subroutine is called and returns, it must be checked with a password (ID number) and released after verification. Combined with software traps, most PC pointer flying phenomena can be detected. When it comes to the program error handling section, it is up to you to kill or scratch (cold start or hot start).
Only one piece of code is used to reveal the essence of program running! 750102H; MOV 01H, #02H. If the current PC does not point to 75H, but to 01H or 02H, then the instruction decoder in 51 will faithfully translate them into AJMP X01H or LJMP XXXXH. And what is XX01H XXXXH? God knows! If this vicious running continues, it will be dead! Reform it:
CLR A ;0C4H
INC A ;04H
MOV R1,A ;0F9H
INC A ;04H
MOV @R1,A ;86H
Each byte of code cannot generate jumps and loops, and all are single-byte instructions! Where to run? If you run out, you must come back by yourself! Staying at home for a thousand days is good! "Jumping out" is difficult! In this way, as long as you are used to using the accumulator and register to count up and dump all the dangerous codes, although two bytes of "package" are added to the "foot" of PC, it is not easy to "run"! "Foot package" ==== run! Some friends will ask: What if PC is caught as 02H--LJMP We have already caught XXH who is too far away. Isn’t it useless to catch YYH next door? Only ZENYIN, who is obsessed with trivial matters, would raise such questions! Which PC is the most active? PC0! Obviously, the "twisting" will happen to her. As for comrade PC15, he is sleeping like a dead pig. Even the thunder (strong interference) can't wake him up? In addition, if the interference is so strong that the high-level PCs make mistakes! Turn off the power! Turn off the power! I won't do it! "It's not that we can't do it, but that the enemy is too strong"! On the other hand, if the enemy is under your dictatorship, it only comes out to make trouble occasionally, but once it comes out, it rushes to the top of the PC, then you have to ask if there is a problem with the foundation (hardware) of your kingdom? Rather than the ideology (software)! Hardware is the foundation! Software is the symptom! Treating both the symptoms and the root causes will build a strong body, so that you can be immune to all poisons!
Experience 2: Don’t trust software dogs
There are many discussions about software dogs on the forum. The craftsman has also read many articles about software dogs. Some masters have indeed proposed some more skillful methods. However, the craftsman's advice is: Don't trust software dogs easily! In fact, software dogs are equivalent to a kind of self-discipline behavior of software. The general idea is to set up a counter, add 1 to it in the timing interrupt, and clear it to zero in the appropriate place of the main program. If the program is out of control, the clear instruction is not executed, but the interrupt occurs frequently, then the counter overflows (the dog barks). But there is a problem here: if the interrupt is blocked due to interference, the software dog will never bark! - In response to this possibility, some people propose to repeatedly refresh the interrupt enable flag in the main program to ensure that the interrupt is not blocked. - But if the program flies into a dead loop and no longer executes the function of "refreshing the interrupt enable flag", it is still possible to starve the dog to death. [page]
Therefore, the craftsman's point of view is: the watchdog must have an independent counter. (That is, a hardware watchdog) Fortunately, many chips now provide internal WDT. This kind of dog has its own counter. Even if the interference causes the program to go out of control, the WDT will still create a constant count until it overflows. Of course, the craftsman does not mean to kill all software dogs with one stick. After all, whether it is a soft dog or a hard dog, it is a good dog if it catches mice (a dog catching mice - meddling in other people's business?). If any dog training expert has indeed raised a good software dog that can guard the door, please bring it out for everyone to see.
Experience 3: RAM Redundancy Technology
The so-called RAM redundancy is:
1. Back up 2 (or more) copies of important data and store them in different areas of RAM (meaning the addresses are not connected).
2. When these data are modified normally, the backup is also updated.
3. When interference occurs and is intercepted in the "Program Error Handling Segment",
Compare the data with the backup and select the correct (or potentially correct?) one by voting (majority rule).
4. The more backups you have, the better the results. (Of course, you have to have enough storage space).
5. Only back up the most original data. Intermediate variables (those that can be re-derived from the original data) do not need to be backed up.
Note:
1. The theoretical basis of this idea is said to be derived from a kind of "probability theory", that is, the probability of a man being beaten by his wife is very high, but if he covers his face and goes to work, he finds that every married man in the company has a blue face, the probability is very small. Similarly, the probability of a RAM register data being destroyed is very high, but the probability of multiple RAMs with unconnected addresses being destroyed at the same time is very small.
2. Two years ago, when I was an apprentice, I used this method once, but the effect was not ideal. At that time, I felt that the probability theory might have failed me? Now thinking back, it might be that I chose the wrong time for backup. As a result, the destroyed data was backed up again. In this way, the restored data is naturally wrong.
Experience 4: Talking about instruction redundancy technology
A friend asked about instruction redundancy. According to my understanding, instruction redundancy is action redundancy. For example, if you want to output a high level on a certain output port to drive an external device, if you only send "1" once, then when interference comes, this "1" may become "0". The correct way to deal with it is to refresh this "1" regularly. Then, even if it is accidentally interfered with, it can recover. In addition to the redundancy of I/O port actions, I strongly recommend that you adopt this method in the following aspects:
1. LCD display. Sometimes, you may use some dedicated LCD driver chips (such as HT1621). This chip has an advantage, that is, as long as you send the display data to it, it will automatically scan the LCD continuously. However, you must not think that you have nothing to do. The correct way to deal with it is to remember to refresh the display data regularly (even if the display content has not changed). For CPUs with built-in LCD DRIVER, the LCD RAM should also be refreshed regularly.
2. Setting the interrupt enable flag. Don't assume that you are OK just by setting the interrupt in the program initialization section. You should refresh it regularly in the appropriate place in the main program to prevent your interrupt from being hung.
3. Remember to refresh other flag words and parameter registers (including those you define yourself) frequently.
4. Other places that you think need to be refreshed repeatedly.
Experience 5: 10 software filtering methods
Here are 10 software filtering methods that the craftsmen have painstakingly compiled:
1. Limiting filter method (also known as program judgment filter method)
A. Method: Based on experience, determine the maximum deviation value allowed between two samples (set to A). Each time a new value is detected, make a judgment: if the difference between the current value and the previous value is <= A, then the current value is valid. If the difference between the current value and the previous value is > A, then the current value is invalid, and the current value is discarded and replaced by the previous value.
B. Advantages: Can effectively overcome pulse interference caused by accidental factors.
C. Disadvantages: Unable to suppress periodic interference and poor smoothness.
2. Median filter method
A. Method: Take samples N times continuously (N is an odd number), arrange the N sample values in order of size, and take the middle value as the effective value of this time.
B. Advantages: It can effectively overcome the fluctuation interference caused by accidental factors, and has good
It has a good filtering effect on the measured parameters with slow bit changes.
C. Disadvantages: Not suitable for rapidly changing parameters such as flow rate and speed.
3. Arithmetic mean filtering method
A. Method: Take N sampling values continuously and perform arithmetic averaging. When the N value is large: the signal smoothness is high, but the sensitivity is low; when the N value is small: the signal smoothness is low, but the sensitivity is high. Selection of N value: general flow, N=12; pressure: N=4
B. Advantages: It is suitable for filtering signals with random interference. The characteristic of such signals is that they have an average value and the signal fluctuates around a certain value range.
C. Disadvantages: It is not applicable to real-time control with slow measurement speed or fast data calculation speed, and it wastes RAM.
4. Recursive average filtering method (also known as sliding average filtering method)
A. Method: Consider N consecutive sample values as a queue, the length of the queue is fixed at N, each time a new data is sampled, it is put at the end of the queue, and the original data at the head of the queue is discarded. (First-in-first-out principle), perform arithmetic average calculation on the N data in the queue, and a new filtering result can be obtained. Selection of N value: flow, N=12; pressure: N=4; liquid level, N=4~12; temperature, N=1~4
B. Advantages: It has good suppression effect on periodic interference, high smoothness, and is suitable for high-frequency oscillation systems. [page]
C. Disadvantages: Low sensitivity, poor suppression of occasional pulse interference, difficult to eliminate sampling value deviation caused by pulse interference, not suitable for occasions with severe pulse interference, relatively wasteful of RAM
5. Median average filtering method (also known as pulse interference prevention average filtering method)
A. Method: Equivalent to "median filter method" + "arithmetic mean filter method". Continuously sample N data, remove a maximum value and a minimum value, and then calculate the arithmetic mean of N-2 data. N value selection: 3~14
B. Advantages: It combines the advantages of two filtering methods and can eliminate the sampling value deviation caused by occasional pulse interference.
C. Disadvantages: The measurement speed is slow and, like the arithmetic average filtering method, it wastes RAM.
6. Limiting average filtering method
A. Method: It is equivalent to "limited filtering method" + "recursive averaging filtering method". Each time the new data is sampled, it is first limited and then sent to the queue for recursive averaging filtering.
B. Advantages: It combines the advantages of two filtering methods and can eliminate the sampling value deviation caused by occasional pulse interference.
C. Disadvantages: wastes RAM.
7. First-order lag filtering method
A. Method: Take a=0~1, this filtering result
Result = (1-a)*this sampling value + a*last filtering result.
B. Advantages: It has good suppression effect on periodic interference and is suitable for occasions with high fluctuation frequency.
C. Disadvantages: Phase lag, low sensitivity, the degree of lag depends on the value of a, and interference signals whose filtering frequency is higher than 1/2 of the sampling frequency cannot be eliminated.
8. Weighted recursive average filtering method
A. Method: It is an improvement on the recursive average filtering method, that is, different weights are given to data at different times. Usually, the closer the data is to the current time, the greater the weight is. The greater the weight coefficient given to the new sample value, the higher the sensitivity, but the lower the signal smoothness.
B. Advantages: Suitable for objects with large pure lag time constants and systems with short sampling periods.
C. Disadvantages: For pure lag time constants that are small, sampling periods that are long, and signals that change slowly, they cannot quickly reflect the severity of the current interference to the system, and the filtering effect is poor.
9. Anti-jitter filtering method
A. Method: Set a filter counter to compare each sampling value with the current effective value: if the sampling value = the current effective value, the counter is cleared; if the sampling value <> the current effective value, the counter is +1, and it is determined whether the counter is >= the upper limit N (overflow); if the counter overflows, the current value replaces the current effective value and the counter is cleared.
B. Advantages: It has a good filtering effect for slowly changing measured parameters, which can avoid repeated on/off jumps of the controller or numerical jitter on the display near the critical value.
C. Disadvantages: It is not suitable for rapidly changing parameters. If the value sampled when the counter overflows happens to be an interference value, the interference value will be imported into the system as a valid value.
10. Limiting and de-jittering filtering method
A. Method: It is equivalent to "limiting filter method" + "anti-jitter filter method". Limit the amplitude first, then eliminate the jitter.
B. Advantages: It inherits the advantages of "limiting" and "de-jittering" and improves some defects in the "de-jittering filtering method" to avoid introducing interference values into the system.
C. Disadvantages: Not suitable for rapidly changing parameters.
IIR Digital Filters
A. Method: Determine the signal bandwidth and filter it. Y(n) = a1*Y(n-1) + a2*Y(n-2) + . + ak*Y(nk) + b0*X(n) + b1*X(n-1) + b2*X(n-2) + . + bk*X(nk).
B. Advantages: High pass, low pass, band pass, band stop are arbitrary. Simple design (using matlab)
C. Disadvantages: large amount of calculation.
Previous article:MCU Learning Plan
Next article:Basic knowledge of single chip microcomputer application programming
Recommended ReadingLatest update time:2024-11-16 23:55
- Popular Resources
- Popular amplifiers
- Wireless Sensor Network Technology and Applications (Edited by Mou Si, Yin Hong, and Su Xing)
- Modern Electronic Technology Training Course (Edited by Yao Youfeng)
- Modern arc welding power supply and its control
- Small AC Servo Motor Control Circuit Design (by Masaru Ishijima; translated by Xue Liang and Zhu Jianjun, by Masaru Ishijima, Xue Liang, and Zhu Jianjun)
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
- CH224 Test
- Recruiting Procast part-time teachers
- [National Technology N32G457 Review] 6 GPIO + I2C key interrupt + SSD1306 display
- [Mil MYD-YA15XC-T Review] + Using mjpg-streamer to build a network camera
- TI's multi-core heterogeneous SOC platform AM57XX
- 【Xianji HPM6750 Review】Interim (Final) Summary
- Shortlist | 2020-2021 ON Semiconductor and Avnet IoT Creative Design Competition
- FPGA_100 Days Journey_Key Debounce.pdf
- EEWORLD University Hall----Inverter Principle and Application Harbin Institute of Technology
- CSM Code Security Module FAQ