The two most difficult parts of embedded programming are interrupt and MM (memory manage). Some people may not feel it, because in most cases chip manufacturers have written them for you. But if you work for a chip manufacturer, you must be able to write configuration files yourself. The reason why these two things are difficult is that they have to be written in assembly or C-like, which is a relatively low-level thing. There are external interrupts and internal interrupts. External interrupts have two implementation modes, hardware interrupt mode and software interrupt mode, which are relatively simple and belong to the application level. In comparison, internal interrupts are much more complicated. Internal interrupts are mainly caused by restart, bus error, overflow, check error, etc. Many software developers basically do not write corresponding interrupt service programs because it is too difficult and generally not used. However, once it occurs, it is a fatal error. Therefore, from the perspective of the robustness of the entire system, there must be corresponding ISRs. This is also the advice of Freescale experts. Due to my limited personal level, I will not say more to avoid misleading everyone. Let's talk about the issues that should be paid attention to in embedded programming.
1. Delay
Embedded programming often involves hardware operations, such as ADC, turning on or off a current source, which all take time. Therefore, when we issue these instructions, we cannot get the desired results by reading the register value immediately, and you cannot find the reason. Sometimes the required delay is relatively long, reaching the ms level. Generally, the us level is enough, depending on the clock frequency of each chip, not just the bus clock frequency of the MCU. As for how to write an accurate delay program, please refer to http://www.51hei.com/mcuteach/247.html
2. Variables
Generally speaking, if you are very clear about the scope and life cycle of a variable, you should define a relative variable, such as const, static, etc., which is less prone to errors. It is not recommended to define all variables as global variables, which is more troublesome to manage. Once an error occurs in the program, the destructiveness is also relatively large. The same is true for functions. Global variables and general functions must be declared, so that it is less likely to make mistakes when calling them. In addition, some compilers will not report errors for undeclared functions, but will issue warnings of implicit type conversions when calling them. I will not give examples here. In short, you must be especially careful about this.
3. Macro definition
In the process of programming, you should try to use macro definitions for some specific numbers. This is more intuitive and convenient for future maintenance. Otherwise, after a long time, you will not remember what the number means. Macro definitions do not bring any burden to the program because they have been replaced at compile time, so you can use them as much as possible. It is worth mentioning that macro definitions are not limited to the use of constants. They can define functions. Because they are direct replacements, they avoid stacking and popping, which improves the efficiency of program execution. At the same time, the amount of code is increased, so simpler functions are generally used. It also has a disadvantage that it does not check whether the parameter type is normal during the replacement process, which increases security risks. The solution to this problem is to use an inline function called inline, which inherits the advantages of macro definitions and makes up for its shortcomings. It is the best choice, but this belongs to the category of C++ and has certain difficulties. I will not talk about it here. Interested friends can refer to relevant materials.
4. Floating point operations
Most low-end MCUs do not support floating-point operations, so they are rarely used in actual use. Therefore, in order to reduce costs, floating-point operation modules are generally removed. This brings a problem. What if floating-point operations are needed? Careful friends may find that even MCUs without floating-point operations can still use float or double data types for calculations during simulation debugging, and the results are also very accurate. Why is this? This is because the compiler automatically calls the library function to achieve this, generally through an iterative method, so its execution efficiency is very slow. This method is not recommended, and the "fixed-point" method is usually used to solve this problem. For example, for a 32-bit data, you can assume that its lower 8 bits are decimal places, and then shift calculations, similar to integer operations. This method is more complicated, but it can be very accurate. Another method is to directly magnify 10 to the power of N times for integer calculations, which can produce approximate values. Therefore, in order not to add unnecessary trouble, you should always try to avoid using floating-point operations, which can generally be avoided.
5.watchdog
The most advanced watchdog mechanism I have come across is the triple watchdog. watchdog1 checks the clock frequency. watchdog2 monitors a small section of code, which must be fed once in a relatively short period of time, generally required to be fed between 250us and 650us. watchdog3 monitors a large section of code, which is required to be fed once in a relatively long period of time, generally within 100ms. All three conditions must be met at the same time, which requires a very clear understanding of the code execution process, otherwise it will cause the dog feeding error and restart!
That’s all I have to say for now. A good program is always determined by details, and this must be accumulated over time. Programming is an annoying thing. Occasionally, there will be a little joy of success, but it is a job after all, and there is nothing much to say. Anyway, just learn it first. As the saying goes, it’s good to have a skill.
Here is a question for those who are interested:
Assuming that x and y are both variables of unsigned char type, and the value range of x is (0 to 15), how to implement the following if...else structure with one statement:
If(x<8) y=x+8; else y=x-8;
Previous article:Wireless Video Monitoring System Based on GPRS
Next article:Implementation of Multi-Serial Card Communication in VxWorks Embedded System
Recommended ReadingLatest update time:2024-11-16 21:50
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
- DSP Basics--Fixed-point Decimal Operations
- "Chat and Laugh" has opened another thread to discuss the issue of nuclear energy. I hope everyone can express their opinions.
- Types of EMI filter components and filters
- Thermostat and speed regulator circuit design for electronic equipment
- Use protues8.6 to simulate, SRF04 simulation fails, the code is correct,
- DM648 FVID_exchange failed
- Photoelectronic thermometer circuit diagram
- MM32F031 Development Board Review 11: Simulating Lighting of 0.96-inch OLED
- MicroPython - Python for microcontrollers
- Wireless transmission power quality monitoring system circuit