Five points to remember in the embedded programming of single chip microcomputer

In the process of MCU programming, if a designer can master multiple programming languages ​​at the same time, then this designer is definitely a very talented person. However, it is too difficult to master the three languages ​​of assembly, C language and C++ at the same time. Many beginners have been hitting the wall in one of them, and they are suffering. This article specifically for everyone to organize the opinions of engineers with many years of experience in the field of embedded programming, summarized into a note that can guide the embedded programming experience, interested friends to take a look.

In the embedded programming of MCU, the two most difficult parts are interrupt and MM (memory manage). The reason why some people think it is not difficult is that the chip manufacturer has written it directly in too many cases, but if the designer itself When working for a chip manufacturer, you must write the configuration file yourself.

These two things are more difficult because they are written in assembly or class C. They belong to lower-level things. Interrupts have external interrupts and internal interrupts. External interrupts have two implementation modes, hardware interrupt mode and software interrupt mode. In terms of simplicity, it is at the application level. In contrast, internal interrupts are much more complicated. Internal interrupts are mainly caused by bus faults, overflows, and check errors. Many software developers basically Do not write the corresponding interrupt service program, because it is too difficult and generally not used. But once it happens, it is a fatal error, so it is necessary to have a corresponding ISR from the perspective of the robustness of the whole system. This is also recommended by freescale experts, so let's talk about the issues that embedded programming should pay attention to.

Five points to remember in the embedded programming of single chip microcomputer

Delay

Embedded programming often involves hardware operations such as ADCs, turning a current source on or off, which takes time, so reading the value of the register as soon as these instructions are issued does not yield the desired result. However, it is still impossible to find the reason. Sometimes the delay required is longer, reaching the ms level. In general, the us level is sufficient. According to the clock frequency of each chip, it does not only refer to the bus clock frequency of the MCU.

2. Variables

Generally speaking, if you clearly define the scope and life cycle of a variable, you should define relative variables, such as const, static, etc., which is not easy to make mistakes. It is not recommended to define all variables as global variables, which is more troublesome to manage. Once the program is wrong, the destructiveness is also relatively large, and the function is also the same. Global variables and general functions must be declared, so that it is not easy to make mistakes when calling, and some compilers will not report errors for undeclared functions, but they are called. At the same time, a warning about the implicit conversion of the type will be issued. This is not an example here. In general, this should be especially careful.

3. Macro definition

In the programming process, you should use macro definitions for some specific numbers. This has the advantage of being more intuitive and easy to maintain in the future. Otherwise, if you look at that number for a long time, you can't think of what it means. Macro definition and It doesn't put any burden on the program, because it has been replaced at the time of compilation, so it can be used widely. It is worth mentioning that the macro definition is not limited to the use of constants, it can define functions, because it is a direct replacement, thus avoiding the push and pop, improving the efficiency of program execution, but at the same time increasing the amount of code, so generally With a simpler function, it also has a disadvantage in that the replacement process does not check whether the parameter type is normal, thus increasing the security risk. The solution to this problem is to use an inline function called inline, which inherits the macro. The advantages of the definition, but also make up for its shortcomings, is the best choice, but this belongs to the C++ category, there are certain difficulties, not much to talk about here, interested friends can refer to the relevant information.

4. Floating point arithmetic

Most low-end single-chip microcomputers do not support floating-point operations, so they are rarely used in actual use. Therefore, in order to reduce the cost, the floating-point arithmetic module is generally removed, which brings a problem. What if you want to use floating-point arithmetic? Careful friends may find that even a microcontroller without floating-point arithmetic can still use the data type of float or double in the simulation debugging process, and the result is very accurate, this is Why? This is because the compiler automatically calls the library function to achieve, generally through the iterative method, so its implementation efficiency is very slow, it is not recommended to use this method, and usually uses the "fixed point" method to solve this Problems, such as a 32-bit data, can be assumed that its lower 8 bits are decimal places, then shift calculations, similar to integer operations, this method is more complicated, but can be very accurate, and another method is to directly zoom in 10 The N-th power is calculated by an integer, and an approximation can be obtained, so in order not to increase unnecessary trouble, it should always be Avoid using the amount of floating-point operations, in general, it is also avoided.

5.watch dog

Take the triple watch dog as an example. Watch dog1 checks the clock frequency. Watch dog2 monitors a small piece of code. It must be fed once in a relatively short period of time. Generally, it is required to feed between 250us and 650us. Watch dog3 monitors a large code. It is required to feed once in a relatively long time, usually within 100ms. The three conditions must be met at the same time. This requires very clear execution of the code, or it will cause the dog to make a mistake.

What needs to be emphasized here is that the quality of the program in the embedded programming process of the single-chip microcomputer is often determined by the details. Whether a program is written in a detailed and flexible manner is directly proportional to the accumulation of accumulated knowledge and actual tempering. Although programming is a very boring and boring process, the joy of success can make everyone believe that this effort is worthwhile.

12V Switching Wall Charger

12v switching Wall charger


AC Input : 100V to 240V, DC Output: 12 volt at 2 amp rating . Please refer to the ASIN :B0746GCGQ8 if u need 10 units.

Type : Regulated Switching Power Supply with 2.1mm x 5.5mm plug , center positive
Manufactured with high quality material and built-in protection of over current, over voltage, short circuits .

COMPACT DESIGN and LOW CONSUMPTION makes it ideal for taking around and using at home.

12v wall charger,12v switching adapter,(12V/2A) Switching Mode Power Adapter Wall Charger,12V 2A Power Supply Adapter,12 Volt 2 Amp Power Adapter,12V 3A Power Supply Adapter,12v3a wall charger

Shenzhen Waweis Technology Co., Ltd. , https://www.laptopsasdapter.com

Posted on