hello everyone , I am boundless .

Recently, several small partners reported that they felt very messy writing programs , I don't know how to plan , Write whatever you think , Global variables fly everywhere , There are many problems with more code .

And if you write your own program without comments , After a few months, I found I couldn't understand it .

The growth process of an engineer , Always surprisingly similar , I used to be like everyone else , I've always wanted to solve the problem of how to write programs better .

There are too many global variables to manage , It looks like a small problem , There are many things involved in solving the problem , Otherwise, it's better for you to add comments directly .

Do you really want to use variables , You can't save it , You can only avoid chaos in other ways , For example, some programming skills and thinking .

After the baptism of countless projects , My personal understanding is : Is a program well written , Mainly look at the program algorithm and program architecture .

Let's talk about the algorithm first , The algorithm is not just needed .

Many novices think that the program should be written well , It has something to do with the level of English and mathematics .

Today I'll give you a bottom line , Not at all , At most, good mathematical algorithms can do better , Most product algorithms are not complex , How complex can a single chip microcomputer do ?

Many algorithms , Difficult in front-end formula analysis , No matter how complicated the project is , Finally, what is embodied in the program is addition, subtraction, multiplication and division , Simple operations such as and or .

for instance :

Please give the result that can be calculated to be equal to 8 Formula of .

Different people , The calculated formula may be different , For example, the following formulas can be realized .

formula 1:(1+1)*(2+2);

formula 2: 1<<3

Obviously , formula 2 Higher computational efficiency , It is mainly reflected in fewer assembly instructions , The machine cycle is naturally shorter , So the algorithm is better .

In fact, the real algorithm is much more complex than this , This is just an example .

A formula like this , Usually we calculate it first in a small notebook , Finally, it is converted to a formula such as addition, subtraction, multiplication and division, which is written in a program .

Most cases , As long as the real-time requirements of the product are not particularly harsh , formula 1 And formula 2 You can't see any difference .

Not every industry's products need algorithms , The algorithm of each industry is certainly different .

Even if you're bad at math , It doesn't matter , You find someone who is good at math , Tell him what you want .

Let him add, subtract, multiply and divide , And , or , Shift operation helps you calculate an optimal solution formula , You can bring it into the program and use it directly .

So when we make products , Don't excessively pursue the efficiency of program execution , As long as it can meet the needs , Not just needed .

Studying algorithms is a waste of time , Versatility is not strong , Anyway, the cost performance is very low .

Now let's talk about the program architecture , This is very practical and versatile .

And it can be said that without understanding the architecture, large-scale projects can never be done , Not bad , But I can't do it !

Maybe many people have been engineers for several years , Has hit a bottleneck , Always wanted to improve , And there's nothing I can do .

for instance , There are too many variables , There are many functions , The program is always messy , Put together a pile BUG.

This function is good , Affect other functions , Changed other functions , This function doesn't work again .

Finally, the code is not easy to integrate to realize the complete product function , Neither BUG Yes .

The boss who got a thousand dollars told you to change the function again , In the first few years of R & D , I'm most afraid of adding functions , Change function .

Even if it's just LED Change to flash per second 1 second , Or add such a small function as a button , If the architecture is not good , It could take you a week or more .

What is the program architecture ?

I think it is a mature programming thinking , It is a summary of experience , such as RTOS It belongs to a program architecture ,STM32 The firmware library is also a program architecture .

Different people , The program architecture is different , There are big ones and small ones , The most important thing is enough .

However, there are many global variables, which leads to the problem of program chaos , It can be solved by the modular programming characteristics of program architecture .

How to do it ?

1. It is distinguished by different levels and different files

Usually writing STM32 Level item , I will be divided into two layers : Hardware layer and application layer .

The hardware layer is mainly responsible for the configuration of relevant peripherals and some function drivers of MCU .

Take our endless MCU programming wifi Take the alarm host project as an example .

The hardware layer has critical management , horn PWM drive ,EEPROM storage , Key detection ,LED Special effect function ,OLED Screen drive , Wireless data soft decoding , Timer matrix , Serial port data sending and receiving these functions .

Each function is separate .c and .h file , In this way, we can better distinguish and manage the codes of different functional modules .

If you write all this in one .c In the document , That involves a lot of functions and global variables , It's chaotic , It's not convenient to find .

2. I usually put the global variables of different functional modules , Array definition to corresponding .c In the document .

After this definition , As long as you don't do it extern statement , other .c Files can't access your variables or arrays , To a certain extent, it plays a protective role .

Another thing is , If you're in a different place .c The file defines the same variable name , The compiler usually reports an error .

There is another way , It can solve the problem of duplicate names , Just use static key word .

This clearly tells the compiler , I limit the scope of these arrays to this .c file , The same goes for variables , After this modification, you can do something else .c Files can also define variables with the same name , The two are independent of each other .

Another important detail is , For functions that do not need to be called externally , I use both static Keywords modification .

This enhances readability , Convenient for later maintenance , You can see which functions are used in this document at a glance , What are the external interfaces .

Technology