Craftsman’s Notes: A Summary of Practical Microcontroller Programming Experience

Craftsman's Notes: A Summary of Practical Microcontroller Programming Experience

Experience One: Using “Software Traps + Program ID” to Handle PC Pointer Jumps

When the CPU is subjected to external interference, sometimes the PC pointer may jump to another segment of the program or to a blank segment. In fact, if the PC pointer jumps to a blank segment, it can be handled relatively easily. By setting up a software trap (intercepting instructions) in the blank segment, the program can be redirected to the initialization segment or the error handling segment. However, if the PC pointer jumps to another segment of the program, what should the system do? Here, I recommend a method—Program ID, with the following approach:

1. First, the program must be modular. Each module (subroutine) performs a single 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. Before returning (RET) from a subroutine, first send the ID number of the current subroutine to the ID register.

5. After returning to the calling program, first check the ID number in the ID register.

If it is correct, continue execution; if incorrect, it indicates that the PC pointer may have jumped incorrectly, and the subroutine did not return as expected. In this case, redirect the program to the initialization segment or the error handling segment.

Craftsman's Notes: A Summary of Practical Microcontroller Programming Experience

This method is like setting up several sentries in the program. Each time a subroutine returns, it must verify the password (ID number) before being allowed to proceed. Coupled with software traps, it can effectively detect most instances of PC pointer jumps. When reaching the error handling segment, whether to perform a cold or warm restart is up to you.

Let’s reveal the essence of program runaway with a line of code! 750102H ;MOV 01H, #02H, if the current PC does not point to 75H, but 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? Who knows! If this runaway continues, it will surely lead to disaster! Reform it:

CLR A ;0C4H

INC A ;04H

MOV R1, A ;0F9H

INC A ;04H

MOV @R1, A ;86H

Each byte of code must not generate jumps or loops, and all are single-byte instructions! Where can it run to? If it runs out, it must come back by itself! “At home” is good for a thousand days! “Jumping out” is difficult in every way! As long as you are accustomed to using accumulators and registers to handle numbers, and eliminate those dangerous codes, although this adds two bytes of “package” to the PC’s “foot”, it cannot run away! “Foot package” === run! Some friends may ask: what if the PC grabs 02H–LJMP and then grabs a far XXH, and then grabs the neighboring YYH, isn’t that useless?

Only those who are nitpicking like ZENYIN would ask such questions! Which bit of the PC is the most active? PC0! To “pull a leg” clearly happens there, as for that PC15 comrade, he sleeps like a dead pig, even a strong interference cannot wake him 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! No more! “It’s not that we can’t do it, but the enemy is too strong!” Conversely, if the enemy only occasionally comes out to stir up trouble under your dictatorship, but when they do, they rush to the high level of the PC, you should ask whether the foundation of your kingdom (hardware) has problems, rather than the ideology (software)! Hardware is fundamental! Software is secondary! Treating both the root and the symptoms creates a strong body, making it invulnerable!

Experience Two: Do Not Trust Software Watchdogs

There has been much discussion about software watchdogs in forums, and I have also reviewed many articles on software watchdogs. Some experts have indeed proposed some skillful methods. However, my advice is: do not trust software watchdogs! In fact, software watchdogs are a form of self-discipline for software. The general idea is to set up a counter, incrementing it in the timing interrupt, and resetting it at appropriate places in the main program. If the program goes out of control, the reset instruction will not be executed, but interrupts occur frequently, leading to counter overflow (the watchdog barks).

However, there is a problem: if interference causes the interrupt to be masked, then the software watchdog will never bark! To address this possibility, some have proposed to repeatedly refresh the interrupt enable flag in the main program to ensure that interrupts are not masked. But if the program jumps into a dead loop and no longer executes the “refresh interrupt enable flag” function, it is still possible to starve the watchdog to death.

Therefore, the viewpoint is: the watchdog must have an independent counter (i.e., hardware watchdog). Fortunately, many chips now provide an internal WDT. These watchdogs come with their own counters. Even if interference causes the program to go out of control, the WDT will still count until overflow. Of course, I do not mean to completely dismiss software watchdogs. After all, whether it is a soft dog or a hard dog, catching mice is a good dog (a dog catching mice—meddling in others’ business?). If any dog training expert has indeed raised a good software watchdog that can guard the door, please bring it out for everyone to see.

Craftsman's Notes: A Summary of Practical Microcontroller Programming Experience

Experience Three: Discussing RAM Redundancy Technology

RAM redundancy refers to:

1. Backing up important data information in two (or more) copies and storing them in different areas of RAM (i.e., non-contiguous addresses).

2. When modifying these data regularly, also update the backups.

3. When interference occurs and is intercepted to 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.

Craftsman's Notes: A Summary of Practical Microcontroller Programming Experience

Note:

1. The theoretical basis for this approach is said to stem from a kind of “probability theory”, that is, the probability of a person getting a swollen face from being hit by his wife is quite high, but if he goes to work with his face covered and finds that every married man in the company has a bruised face, the probability of that is very low. Similarly, the probability of a RAM register’s data being corrupted is quite high, but the probability of multiple non-contiguous RAMs being corrupted simultaneously is very low.

2. A couple of years ago, when I was an apprentice, I used this method once, but the effect was not very good. At that time, I felt that probability theory had failed me? Looking back now, it may have been due to poor timing in backing up. As a result, I backed up data that had already been corrupted. Thus, the recovered data was naturally incorrect.

Experience Four: Discussing Instruction Redundancy Technology

Experience Three: Discussing RAM Redundancy Technology

A friend asked about instruction redundancy. In 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 occurs, this “1” may turn into “0”. The correct approach is to periodically refresh this “1”. Therefore, even if it is occasionally interfered with, it can recover. In addition to I/O port 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 (such as 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 display content has not changed). For CPUs with built-in LCD DRIVERS, you should also periodically refresh the LCD RAM.

2. Setting the interrupt enable flag. Do not think that setting the interrupt in the program initialization segment is sufficient. You should periodically refresh it in the main program to prevent your interrupts from being suspended.

3. Other flag words and parameter registers (including those you define) should also be frequently refreshed.

4. Other places you think need to be refreshed frequently.

Craftsman's Notes: A Summary of Practical Microcontroller Programming Experience

Experience Five: Ten Software Filtering Methods

Experience Three: Discussing RAM Redundancy Technology

Here are ten software filtering methods that I have painstakingly compiled:

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, judge: if the difference between the current value and the last value is <= A, then the current value is valid. If the difference is > A, then the current value is invalid, discard it, and use the last value instead.

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 an odd number), arrange the N sampled values in order, and take the middle value as the valid value.

B. Advantages: Effectively overcomes fluctuations caused by random factors, and has good filtering effects for slowly changing parameters such as temperature and liquid levels.

C. Disadvantages: Not suitable for rapidly changing parameters such as 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 having an average value, with the signal fluctuating around a certain value range.

C. Disadvantages: Not suitable for real-time control requiring fast data calculations, and 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 original data at the front of the queue is discarded (FIFO principle). The arithmetic average of the N data in the queue can yield 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: Good suppression of periodic interference, high smoothness, suitable for systems with high-frequency oscillations.

C. Disadvantages: Low sensitivity, poor suppression of occasional pulse interference, and not easy to eliminate sampling value deviations caused by pulse interference. Not suitable for scenarios with severe pulse interference, and 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, remove one maximum and one minimum value, and then calculate the arithmetic mean of the N-2 data. Selection of N: 3~14.

B. Advantages: Combines the advantages of both filtering methods, can eliminate sampling value deviations caused by occasional pulse interference.

C. Disadvantages: Slower measurement speed, and like 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 data point is sampled, it is first clipped, and then sent to the queue for recursive average filtering.

B. Advantages: Combines the advantages of both filtering methods, can eliminate sampling value deviations caused by occasional pulse interference.

C. Disadvantages: Relatively wasteful of RAM.

7. First-Order Lag Filtering Method

A. Method: Take a=0~1, the current filtering result = (1-a) * current sampled value + a * last filtering result.

B. Advantages: Good suppression of periodic interference, suitable for scenarios with high fluctuation frequencies.

C. Disadvantages: Phase lag, low sensitivity, the degree of lag depends on the size 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 given to the new sampled 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, resulting in poor filtering effects.

9. Debouncing Filtering Method

A. Method: Set a filtering counter to compare each sampled value with the current valid value: if the sampled value = current valid value, then reset the counter; if the sampled value <> current valid value, then increment the counter by 1, and check if the counter >= upper limit N (overflow). If the counter overflows, replace the current valid value with the current sampled value and reset the counter.

B. Advantages: Good filtering effect for slowly changing measured parameters, can avoid repeated on/off fluctuations of the controller or jittering of values on the display near critical values.

C. Disadvantages: Not suitable for rapidly changing parameters; if the value sampled at the time of counter overflow happens to be an interference value, it will treat the interference value as a valid value in the system.

10. Clipping Debouncing Filtering Method

A. Method: Equivalent to “Clipping Filtering Method” + “Debouncing Filtering Method”. First clip, then debounce.

B. Advantages: Inherits the advantages of both “clipping” and “debouncing”, improving on certain defects in the “debouncing filtering method”, avoiding introducing interference values into the system.

C. Disadvantages: Not suitable for rapidly changing parameters.

Craftsman's Notes: A Summary of Practical Microcontroller Programming Experience

The “Learning World” column is officially launched and will be continuously updated, come and follow it! Directly reply with “Microcontroller”, “Embedded Systems”, “Internet of Things”, “Smart Hardware”,“Embedded Engineer”, “CPU”, “GPU”, “FPGA”, “Linux”, “Android”, “ARM”keywords to read past related exciting content at any time!

Craftsman's Notes: A Summary of Practical Microcontroller Programming Experience

Disclaimer: This article is a network reprint, and the copyright belongs to the original author. If there are any copyright issues, please contact us, and we will confirm the copyright based on the copyright certificate you provide and pay the remuneration or delete the content.

Leave a Comment