Share your experience of embedded programming

Publisher:咖啡小熊Latest update time:2012-09-04 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Personally, I think you should be invincible in the field of embedded programming if you master three languages: assembly, C, and C++. With limited energy, it is okay to master the first two. If that doesn't work, you must be proficient in C, otherwise you can only be the boss. Haha, I have been in this industry for more than a year, and I have some feelings. I have learned a lot and also taken a lot of detours. I want to share with you here, mainly to help beginners.

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;

Reference address:Share your experience of embedded programming

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

Record 2--s3c2440 DMA operation
one. typedef struct tagDMA {     volatile U32 DISRC; //0x0 DMA initial source register     volatile U32 DISRCC; //0x4 DMA initial source control register     volatile U32 DIDST; //0x8 DMA initial destination register     volatile U32 DIDSTC; //0xc DMA initial destination control register     volatile U32 DCON; //0x1
[Microcontroller]
s3C2440 Memory controller
Hardware Design   The first is the selection of BANK0. If you choose to connect BANK0 to NAND Flash, you need to set the status of pins OM1 and OM0 to 0, which means it is NAND FLASH mode. . NANDFlash mode will automatically The first 4MB of NANDFLASH content is copied to memory. Secondly, the pins of S3C2440 SDRAM
[Microcontroller]
3C2440A interrupt controller and timer
I looked at the non-operating system sample code in the CD-ROM of the development board and tried to simplify it, leaving only the core startup code. Then I wrote a makefile to compile it, burned it to the development board for experiment, and finally succeeded. After the first program was run, it simply turned the fou
[Microcontroller]
ARM11 S3C6410 Series Tutorial 2: Serial Port
  For a microprocessor, the most commonly used and simplest interface is the serial port. It does not require too many pins or too many hardware circuits. If you are not sure, adding a max232 can ensure foolproofness and complete data transmission.   S3C6410 has 187 multiplexed I/O ports, which can be divided into 1
[Microcontroller]
ARM11 S3C6410 Series Tutorial 2: Serial Port
Design of the Minimum System Based on S3C4510B Microprocessor
Introduction: This paper introduces the S3C4510B ARM microprocessor and the minimum system designed based on this processor, and summarizes the selection of peripheral application circuits of this system to facilitate users to design and develop their own systems. ARM (Advanced RISC Machines) can be considered as th
[Microcontroller]
Design of the Minimum System Based on S3C4510B Microprocessor
Nubia C1 True Wireless Bluetooth Earphones Released: 40 Hours of Battery Life
     On January 18, according to the official Weibo of Nubia Mobile, today, Nubia C1, which claims to "define a new benchmark for Bluetooth headsets", was released. The new product is now officially on sale with a launch price of 158 yuan and a first-sale discount price of 138 yuan. If you buy today, you will receive
[Mobile phone portable]
Design of a fast charger for electric bicycle based on AT89C51
1. Introduction Electric bicycles are favored by many users because of their pollution-free, noise-free, light and beautiful features. However, they also have limitations in use, such as the battery running out halfway, and the battery life will gradually shorten as the use time increases. This paper aims to de
[Microcontroller]
Design of Metronome Based on 89C51 Single Chip Microcomputer
1 Hardware Circuit Principle The metronome uses the AT89C51 single-chip microcomputer as the control center, and is composed of an LED display module, an adjustment keyboard module, and a sound output module. Figure 1 is the circuit schematic diagram of the metronome. The AT89C51 single-chip microcomputer produ
[Microcontroller]
Design of Metronome Based on 89C51 Single Chip Microcomputer
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号