Using “Software Traps + Program IDs” to Handle PC Pointer Jumps
When the CPU is disturbed by external factors, sometimes the PC pointer can jump to another part of the program or to a blank segment. In fact, if the PC pointer jumps to a blank segment, it is relatively easy to handle. Just set up a software trap (intercept instruction) in the blank segment to redirect the program to the initialization segment or error handling segment. However, if the PC pointer jumps to another part of the program, what should the system do? Here, I recommend a method — program IDs, with the following approach:
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. Each time a subroutine finishes executing, before returning (RET),
first send the ID number of this subroutine to the ID register.
5. After returning to the upper-level program, first check the ID number in the ID register.
If correct, continue execution; if incorrect, it indicates that the PC pointer may have jumped incorrectly, and the subroutine did not return as expected, at which point the program should be redirected to the initialization segment or error handling segment.
This method is like setting up several sentries in the program; every time a subroutine returns, it must verify the ID number before being allowed to proceed. Combined with software traps, it can effectively detect most occurrences of PC pointer jumps. When reaching the error handling segment, whether to cold start or hot start is up to you.
Here’s a line of code to reveal the essence of program jumps: 750102H; MOV 01H, #02H. If the current PC does not point to 75H, but instead points to 01H or 02H, then the instruction decoder within the 51 will faithfully translate them into AJMP X01H or LJMP XXXXH. And what are XX01H and XXXXH? God knows! If such malignant jumps continue, it would be disastrous! 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 or loops, and they are all single-byte instructions! Where to run off to? If they run away, they must come back themselves! “Home” is good for a thousand days! “Jumping out” is difficult in every way! As long as you are used to using the accumulator and registers to transfer numbers and eliminate those dangerous codes, although it adds two bytes of “package” to the PC’s “foot”, it cannot “run”! “Foot package” === run! Some friends may ask: What if the PC grabs 02H — LJMP and then grabs an old XXH far away, and then grabs the neighboring YYH, isn’t that useless? Only those who are nitpicking like ZENYIN would ask such questions! Who is the most active in the PC? It’s PC0! If “pulling a trick” obviously happens to her, as for that PC15 comrade, she sleeps like a dead pig, even strong interference (雷爆) cannot wake her up? Moreover, if the interference is so strong that even the high bits of the PC are erroneous! Turn off the power! Turn off the power! We won’t do it anymore! “It’s not that we can’t do it, but the enemy is too powerful!” Conversely, if the enemy only occasionally makes trouble under your dictatorship, but once it comes out, it rushes to the high levels of the PC, you should ask whether your kingdom’s foundation (hardware) has problems, rather than the ideology (software)! Hardware is fundamental! Software is the standard! Treating both the root and the symptoms creates a strong body, so it can be invulnerable!
Don’t Trust Software Dogs
There are many discussions about software dogs in the forums. I have also consulted many articles on software dogs. Some masters have indeed proposed some relatively skillful methods. However, my advice is: don’t trust software dogs! In fact, a software dog is a kind of self-discipline behavior of software. The general idea is to set up a counter, increment it in the timing interrupt, and clear it at appropriate places in the main program. If the program goes out of control and the clear command is not executed, but interrupts occur frequently, the counter will overflow (the dog barks). However, there is a problem: what if interference causes the interrupt to be masked? Then the software dog will never bark! — To address this possibility, some have proposed to repeatedly refresh the interrupt enable flag in the main program to ensure that the interrupt is not masked. — But what if the program jumps into a dead loop and no longer executes the “refresh interrupt enable flag” function? The dog may still starve to death.
Therefore, my view is that the watchdog must have an independent counter (i.e., hardware watchdog). Fortunately, many chips now provide internal WDT. These dogs come with their own counters. Even if interference causes the program to go out of control, the WDT will continue counting until it overflows. Of course, I don’t mean to completely dismiss software dogs. After all, whether it’s a soft dog or a hard dog, catching mice is a good dog (dogs catching mice — meddling in others’ business?). If any dog training expert indeed has a good software dog that can guard the door, please bring it out for everyone to see.
RAM Redundancy Technology
RAM redundancy refers to:
1. Backing up important data twice (or more) and storing it in different areas of RAM (i.e., non-contiguous addresses).
2. When modifying these data regularly, also update the backup simultaneously.
3. When interference occurs and is intercepted into the “program error handling segment”,
compare the data with the backup and use a voting method (the minority obeys the majority) to select the correct (or possibly correct?) one.
4. The more backups, the better the effect (of course, you need enough storage space).
5. Only back up the most original data. Intermediate variables (i.e., those that can be derived from the original data) do not need to be backed up.
Note:
1. The theoretical basis for this approach is said to originate from a kind of “probability theory”. For example, the probability of a man getting a swollen face from being hit by his wife is quite high, but if he covers his face and goes to work only to find that every married man’s face in the company is swollen, the probability is quite low. Similarly, the probability of a RAM register’s data being destroyed is quite high, but the probability of multiple non-contiguous RAMs being destroyed simultaneously is quite low.
2. A couple of years ago, when I was an apprentice, I tried this method once, but the effect was not ideal. At that time, I felt that the probability theory had failed me? Looking back now, it may have been due to poor timing in the backup. As a result, I backed up the already destroyed data. Consequently, the restored data was naturally incorrect.
Instruction Redundancy Technology
A friend asked about instruction redundancy earlier. According to my understanding, instruction redundancy is action redundancy. For example, if you want to output a high level at a certain output port to drive an external device, if you only send “1” once, then when interference occurs, this “1” may turn into “0”. The correct approach is to periodically refresh this “1”. Thus, even if it is occasionally disturbed, it can recover. In addition to I/O action redundancy, I strongly recommend that everyone adopt this method in the following areas:
1. LCD display. Sometimes, you may use some dedicated LCD driver chips (like HT1621). The advantage of these chips is that once you send the display data to them, they will continuously scan the LCD automatically. However, do not think that this means you have nothing to do. The correct approach is to remember to periodically refresh the display data (even if the content has not changed). For CPUs with built-in LCD DRIVER, also remember to periodically refresh the LCD RAM.
2. Setting the interrupt enable flag. Do not think that it’s enough to set the interrupt correctly in the program initialization segment. You should periodically refresh it at appropriate places in the main program to prevent your interrupt from being suspended.
3. Other flag words and parameter registers (including those you define) should also be refreshed regularly.
4. Other places you think need to be refreshed frequently.
10 Software Filtering Methods
Here are 10 software filtering methods compiled from my painstaking efforts:
1. Clipping Filtering Method (also known as Program Judgment Filtering Method)
A. Method: Based on experience, determine the maximum allowable deviation between two samples (set as A). Each time a new value is detected, check: if the difference between this value and the last value <= A, then this value is valid. If the difference > A, then this value is invalid, discard it, and replace it with the last value.
B. Advantages: Effectively overcomes pulse interference caused by random factors.
C. Disadvantages: Cannot suppress periodic interference, and has poor smoothness.
2. Median Filtering Method
A. Method: Continuously sample N times (N is odd), arrange the N sampled values in order, and take the middle value as the effective value.
B. Advantages: Effectively overcomes fluctuations caused by random factors and has good filtering effects on slowly changing parameters like temperature and liquid levels.
C. Disadvantages: Not suitable for rapidly changing parameters like flow and speed.
3. Arithmetic Mean Filtering Method
A. Method: Continuously take N sampled values for arithmetic averaging. When N is large: the signal smoothness is high, but sensitivity is low; when N is small: the signal smoothness is low, but sensitivity is high. Selection of N: generally for flow, N=12; for pressure, N=4.
B. Advantages: Suitable for filtering signals with random interference, characterized by an average value, with fluctuations around a certain value range.
C. Disadvantages: Not suitable for measurements with slow speed or real-time control requiring fast data calculations; relatively wasteful of RAM.
4. Recursive Average Filtering Method (also known as Sliding Average Filtering Method)
A. Method: Treat the continuous N sampled values as a queue with a fixed length of N. Each time a new data point is sampled, it is added to the end of the queue, and the data at the front of the queue is discarded (first in, first out). The arithmetic average of the N data in the queue can be calculated to obtain a new filtering result. Selection of N: for flow, N=12; for pressure, N=4; for liquid level, N=4~12; for temperature, N=1~4.
B. Advantages: Has a good suppression effect on periodic interference, high smoothness, and is suitable for systems with high-frequency oscillations.
C. Disadvantages: Low sensitivity, poor suppression of accidental pulse interference, and not suitable for scenarios with severe pulse interference; also relatively wasteful of RAM.
5. Median Average Filtering Method (also known as Anti-Pulse Interference Average Filtering Method)
A. Method: Equivalent to “Median Filtering Method” + “Arithmetic Mean Filtering Method”. Continuously sample N data, discard one maximum and one minimum value, and then calculate the arithmetic average of the N-2 data. Selection of N: 3~14.
B. Advantages: Combines the advantages of both filtering methods, can eliminate deviations caused by accidental pulse interference.
C. Disadvantages: Slower measurement speed, and similar to the arithmetic mean filtering method, relatively wasteful of RAM.
6. Clipping Average Filtering Method
A. Method: Equivalent to “Clipping Filtering Method” + “Recursive Average Filtering Method”. Each time a new sample is taken, it is first clipped, and then sent into the queue for recursive average filtering.
B. Advantages: Combines the advantages of both filtering methods, can eliminate deviations caused by accidental pulse interference.
C. Disadvantages: Relatively wasteful of RAM.
7. First-Order Lag Filtering Method
A. Method: Take a = 0~1, the filtering result = (1-a)*current sample value + a*last filtering result.
B. Advantages: Has a good suppression effect on periodic interference, suitable for scenarios with high fluctuation frequencies.
C. Disadvantages: Phase lag, low sensitivity, the degree of lag depends on the value of a, cannot eliminate interference signals with frequencies higher than half the sampling frequency.
8. Weighted Recursive Average Filtering Method
A. Method: An improvement on the recursive average filtering method, assigning different weights to data at different times. Generally, the closer the data is to the current time, the greater the weight. The greater the weight coefficient assigned 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 objects with small pure lag time constants and long sampling periods, the signal cannot quickly reflect the severity of the current interference, leading to poor filtering effects.
9. Debounce Filtering Method
A. Method: Set up a filtering counter to compare each sampled value with the current valid value: if the sampled value = current valid value, reset the counter; if the sampled value <> current valid value, increment the counter by 1 and check if the counter >= upper limit N (overflow). If the counter overflows, replace the current valid value with this value and reset the counter.
B. Advantages: Has good filtering effects for slowly changing measured parameters, can avoid repeated on/off switching of controllers or jittering of values on displays near critical values.
C. Disadvantages: Not suitable for rapidly changing parameters; if the value sampled at the moment of counter overflow happens to be the interference value, it will treat the interference value as valid.
10. Clipping Debounce Filtering Method
A. Method: Equivalent to “Clipping Filtering Method” + “Debounce Filtering Method”. First clipping, then debouncing.
B. Advantages: Inherits the advantages of both “clipping” and “debounce”, improves certain defects in the “debounce filtering method”, and avoids introducing interference values into the system.
C. Disadvantages: Not suitable for rapidly changing parameters.
IIR Digital Filter
A. Method: Determine the signal bandwidth and filter it. Y(n) = a1*Y(n-1) + a2*Y(n-2) + . + ak*Y(n-k) + b0*X(n) + b1*X(n-1) + b2*X(n-2) + . + bk*X(n-k).
B. Advantages: High-pass, low-pass, band-pass, band-stop arbitrary. Simple design (using MATLAB).
C. Disadvantages: Large computational load.