The growth experience and goals of every Siege Lion are always similar.
When I first started learning this technology, I hoped that I could independently make various smart products.
With this ability, you will be more competitive and more profitable, and will not be abandoned in an increasingly smart era.
Unfortunately, not everyone makes it to the finish line.
Many people learn but fail to learn, and end up regretting giving up.
Although some people took many detours, they survived with amazing perseverance.
There are too many bottlenecks to break through along the way, each of which may determine whether you are the chosen one or the eliminated one.
For engineers who are new to the industry, there is a bottleneck that is very difficult to break through.
That's how to make the programs you write more professional.
For a long time, although I could write functions by myself, I always felt that my programs were written in a mess.
The picture above is a program I wrote after just one year of developing microcontrollers. I wonder if you can see any problems.
Let me analyze it for you:
1. The entire project program is written in the main function
2. Too many global variables
3. No modular thinking
It was the program logic of this project that was so adjusted that I almost collapsed.
In fact, the product is not complicated, it is just a control panel for a solar water heater.
However, once combined, the functions will conflict with each other severely, causing problems here and there.
If a problem occurs, all functions must be retested.
If you haven't experienced this problem, you won't understand the importance of program architecture.
The quality of program architecture will affect all aspects of the entire program.
For example, global variables, I also realized this problem later.
If there are too many global variables, the program will become uncontrollable if it becomes large.
The first is to avoid duplication of global variable names. The second is that if any variable is not commented, it will be clean after one month.
Especially when you define global variables for the entire project function together, it is simply a disaster.
However, it is definitely impossible not to use global variables .
It just needs to be used reasonably . At this time, the experience of the engineer will be tested.
How did I do it?
Take, for example, our Wuji microcontroller programming IoT gateway course project.
I adopted the thinking of modular programming and divided the overall architecture into hardware layer and application layer.
Generally speaking, there is a middle layer, such as parsing some protocols. There is not much code in the middle layer of the project, and I simplified it.
The global variables of each functional module are defined in their respective .c files.
Compared with the solar water heater control panel program I made, although the number of global variables may not have changed, it is obvious that the modular writing method is clearer.
Of course, this is not just as simple as making the code look cleaner, but also has the advantages of strong functional scalability and portability.
Strong scalability sounds like a professional term, and many novices may not quite understand what it means.
Just imagine, the product function code is finally completed, the test is no problem, and it is delivered to the customer for testing.
After the customer tested it, they said they wanted to change the function, and they changed it back and forth 7 or 8 times. Did you tear them up and have it all?
This is normal. The customer is a novice about technology, and he doesn’t know how much you need to pay behind his words.
Experienced engineers start by learning how to be lazy. No matter how urgent the project is, you will be awesome if you still have free time after finishing it.
This is why code scalability is important.
Let’s talk about portability next.
Portability is relative to the microcontroller (hardware platform).
For example, I used to do this project on an STM32 microcontroller. Now that the price of the chip has increased, my boss asked me to replace it with GD32.
At this time, the portability of your program will be tested.
Programs written by experienced engineers generally only need to change the peripheral interface of the hardware layer, and the product logic function code of the application layer basically does not need to be changed.
And the novice may have to rewrite the entire code...
The problem of a global variable seems simple, but if you want to solve it, you still have to think from the perspective of the entire program architecture.
If you are still far from this stage, there is a more convenient method.
Just use structures .
Use object-oriented thinking to uniformly define similar variables into structures.
For example, time is divided into years, months, days, weeks, hours, minutes, and seconds.
If you use separate global variables, it will be more fragmented and difficult to manage.
In this case, it is more suitable to use a structure, because these are parameters of the "time" object.
There are many similar ones. For example, GPIO is also an object, and its parameters include port number, pin number, input mode, output mode, frequency, etc.
You can take a look at the STM32 firmware library, which is a very typical object-oriented programming thinking.
Previous article:Why don’t the masters of microcontrollers like to deal with novices?
Next article:Design of a smart community trash can compression system
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
- How to turn on the internal enable of Panasonic servo drive?
- What is the rigidity setting of Panasonic servo drive?
- How to change the inertia ratio of Panasonic servo drive
- What is the inertia ratio of the servo motor?
- Is it better for the motor to have a large or small moment of inertia?
- What is the difference between low inertia and high inertia of servo motors?
- In your spare time, we can exercise together and supervise each other.
- Application of resettable fuse
- 【Evaluation of SGP40】-SGP40 temperature and humidity test
- When choosing crystal oscillators, should we choose imported or domestic ones?
- Does anyone know how to write a DLL for a card reader/writer? Is there any relevant information?
- Still using the burning method to test fabric composition? Now there is a new method.
- How to pair MCU with FPGA to improve system efficiency?
- [National Technology N32G457 Review] 1. Brief review of the first power-on
- The pain of vias, isn’t it just a window opened in the via, how can it cause a short circuit?
- About TAS5805M Stereo Class-D Amplifier