How to avoid too many global variables and confusion when developing microcontrollers using C language

Publisher:乐基儿vosLatest update time:2022-12-15 Source: zhihu Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.


Reference address:How to avoid too many global variables and confusion when developing microcontrollers using C language

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

Latest Microcontroller Articles
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号