How to develop single-chip microcomputer in C language to avoid too much confusion of global variables

The growth experience of each siege lion is 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 earn more money, and you will not be abandoned in an increasingly intelligent era.

Unfortunately, not everyone can make it to the finish line.

Many people learn to learn but can't learn, and finally give up with regret

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 eliminated.

For engineers who have just entered the industry, there is a bottleneck that is very difficult to break through.

That is how to make the programs you write more professional.

For a long time, although I could write the functions myself, I always felt that my programs were written in a mess.

The picture above is a program written by me just engaged in the development of single-chip microcomputer for one year. I don’t know 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 is the program logic of this project, and I am about to collapse when it is transferred.

In fact, the product is not complicated, it is a control panel of a solar water heater.

However, once they are combined, the functions will conflict with each other severely, causing problems to change here and there.

Once a problem occurs, all functions must be tested again.

If you haven't experienced this kind of problem, you won't understand the importance of program architecture.

The quality of the program architecture will affect all aspects of the entire program.

For example, global variables, I also realized this problem later.

That is, there are too many global variables, and the program will be out of control when it is too large.

The first one is to avoid duplication of global variable names, and the second one is that if any variable is not commented, it will be clean after one month.

Especially when you define the global variables of the entire project function together, it is a disaster.

However, it is certainly not possible without global variables .

Just to use it reasonably , this time will test the experience of engineers.

how do i do it

Take the course project of our Wuji MCU programming IoT gateway as an example.

I adopted the thinking of modular programming, and divided the hardware layer and application layer from the overall architecture.

Generally speaking, there is an intermediate layer, such as parsing some protocols. The code in the middle layer of the project is not much, which is simplified by me.

The global variables of each function module are defined in their own .c files.

Compared with the solar water heater control board 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 as simple as making the code look cleaner, but also has the advantages of strong functional scalability and strong portability.

Strong scalability sounds like a technical term, and many novices may not 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 test, the customer said that he wanted to change the function, and changed it back and forth 7 or 8 times. Did you tear up all of them?

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 to be able to be lazy, no matter how urgent the project is, it is awesome if you still have free time after finishing the project.

This is where code extensibility matters.

Now let's talk about portability.

Portability is relative to MCU (hardware platform).

For example, I used to do this project on the STM32 single-chip microcomputer. Now that the chip price has increased, the boss asked to replace it with GD32.

This time will test the portability of your program.

Programs written by experienced engineers generally only need to change the peripheral interfaces of the hardware layer, and the product logic function codes of the application layer basically do not need to be changed.

And Caiji 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 the structure .

Use object-oriented thinking to uniformly define variables of the same type as structures.

For example, time is divided into year, month, day, week, hour, minute, and second.

If you use the form of a separate global variable, it is more fragmented and more difficult to manage.

In this case, it is more suitable to use a structure, because these are all parameters of the "time" object.

There are many similar ones. For example, GPIO is also an object. The parameters include port number, pin number, input mode, output mode, frequency, etc.

You can look at the STM32 firmware library, which is a typical object-oriented programming thinking.

Guess you like

Origin blog.csdn.net/weixin_43982452/article/details/123237402