Summary of MCU Programming Test

Publisher:SparklingMoonLatest update time:2013-02-05 Source: 21IC Keywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.

Keywords:MCU Reference address:Summary of MCU Programming Test

Previous article:MCU Learning Plan
Next article:Basic knowledge of single chip microcomputer application programming

Recommended ReadingLatest update time:2024-11-16 23:55

51 single chip microcomputer drives 8*8LED dot matrix screen to display hourglass shape assembly program
ORG 0000H ST: MOV DPTR,#TAB ; Display the first address of the data table LP5:CLR P2.2; 74HC595 output clear SETB P2.2 CLR P2.4 ;Select the first line 000 CLR P2.5 CLR P2.6 MOV A,#00H ; Clear accumulator MOVC A,@A+DPTR; Get display data LCALL DIS ; Send column data subroutine? LCALL DELAY ; Delay
[Microcontroller]
EFM32 microcontrollers add low-energy sensor interface
November 9, 2010, at the Electronica 2010 in Munich and Oslo, Norway – Energy Micro, the energy-efficient microcontroller company, has added a universal low-energy sensor interface to its EFM32 microcontroller product family. The LESENSE function module enables autonomous monitoring of up to 16 external sens
[Analog Electronics]
EFM32 microcontrollers add low-energy sensor interface
Detailed explanation of PIC microcontroller application design skills
The PIC series 8-bit single-chip microcomputer (RISC microcontroller) developed by Microchip Technology Inc. of the United States, especially the microcontroller with built-in second-generation Flash memory (40-year storage life), is unique in terms of rapid application. Due to its ease of use and high reliability,
[Microcontroller]
Detailed explanation of PIC microcontroller application design skills
MCU---HLK-W801 drives touch screen
Background I'm learning lvgl recently. This is an open source embedded image display framework that is sufficient to support some resource-poor microcontrollers to display some professional-looking interfaces, such as the following There is also this Isn't it cool? Let's put Lvgl aside for now. Because it is a u
[Microcontroller]
MCU---HLK-W801 drives touch screen
An embedded video switcher implemented by a single chip microcomputer
With the rapid development of multimedia communication technology, the theoretical research and practical system development of conference television have also developed rapidly. In recent years, China's conference television business has also been promoted and applied at a speed beyond people's imagination. The vid
[Microcontroller]
An embedded video switcher implemented by a single chip microcomputer
Teach you the difference between DSP and MCU
This article briefly introduces the difference between DSP and MCU. 1. Different memory structures The microcontroller uses a von Neumann memory structure, in which there is only one memory space connected to the processor core through a set of buses (an address bus and a data bus). Most DSPs use the H
[Microcontroller]
Based on 51 single chip microcomputer - multi-function stopwatch
Features: 1. Three-digit digital tube display data 00.0~99.9 2. Four data can be recorded in one timing (one data is saved for each press) and stored in EEPROM (if pressed five times, the data of the first press will be lost) 3. The two buttons have the functions of pause, start, store, retrieve and page turning
[Microcontroller]
Design of hardware structure and control system of descending device based on single chip microcomputer control
This paper introduces a hardware structure and control system of a descent control device based on single-chip microcomputer control. The system uses a single-chip microcomputer as the control core of the system and combines photoelectric sensor technology to realize the measurement of the descent speed, thereby achie
[Microcontroller]
Design of hardware structure and control system of descending device based on single chip microcomputer control
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号