"Introduction
When we do a relatively complex project in a project team, it means that you no longer work alone. Instead, they work together with team members, which requires team members to be responsible for part of the project. For example, you may only be responsible for communication or display.
At this time, you should write your own program as a module, debug it separately, and leave the interface for other modules to call. Finally, after the team members have written and debugged the modules they are responsible for, the project leader will conduct combined debugging. Such occasions require that the program must be modular. Modularization has many advantages, not only for the convenience of division of labor, but also for the debugging of the program, the division of program structure, and the readability and portability of the program.
What to say
Beginners often don't understand how to program modularity. In fact, it is easy to learn, and it is one of the effective methods of well-organized program structure. This paper will first talk about the modular methods and precautions, and finally give the detailed steps of modular programming by taking keil c compiler, which is widely used by beginners, as an example. Modular programming should understand the following overview:
A module is a combination of a. C file and an. H file. The header file (. H) is the declaration of the module interface;
This article summarizes the implementation method and essence of Modularization: write the code of a function module into a. C file separately, and then put the interface function of the module in the. H file. For example: if you use liquid crystal display, you may write a liquid crystal driver module to realize the reality of characters, Chinese characters and images, named led_ Device. C, the. C file of the module can be roughly written as:
#Include... / / define the variable unsigned char value// Global variables... / / define functions. / / this is the first function in this module, which plays the role of delaying. It is only used for function calls in this module, so the static keyword is used to modify the / *******************************************************************************************, In other modules, the function of calling / xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Comm writes data for 1, instruction for 0, and content is the number written or instruction ******************************************************************************_ lcd (uchar dat_ Comm, uchar content) {}............ / ************************************************************************* / Note: only these two functions are written here. The scope of the first delay function is within the module, and the second is required by other modules. For simplicity, the function body is not written here. The module interface is given in the. H file. In the above example, the character function: WR is written to the LCD_ lcd (uchar dat_ Comm, uchar content) is an interface function. Because other modules will call it, this function must be declared as an external function in the. H file (modified with the extrun keyword). Another delay function: void delay (uint US) is only used in this module (local function, modified with the static keyword). Therefore, it does not need to be placed in the. H file H the file format is as follows: / / declare the global variable extern unsigned char value// Declare interface function extern void wr_ lcd (uchar dat_ comm,uchar content); // Write characters to LCD... / *************************************************************************************
In keil compiler, even if the keyword extern is not declared, the compiler will not report an error, and the program runs well, but it is not guaranteed to use other compilers. It is strongly recommended to develop good programming practices.
The functions in the. C file will appear in the. H file only when other modules are used. For example, the local delay function static void delay (uint US) is useless even if it appears in the. H file, because other modules do not call it at all, and can not actually call it (the restrictive effect of the static keyword).
Be sure to add a semicolon at the end of this sentence, I believe many students have encountered this strange compiler error: error c132: 'XXXX': not in formal parameter list. This error is actually due to the lack of semicolon at the end of. H's function declaration.
Module application: if necessary, LCD in LCD menu module_ Using LCD driver module LCD in menu. C_ Function void WR in device. C_ lcd (uchar dat_ Comm, uchar content), just on the LCD of the LCD menu module_ Add the header file LCD of the LCD driver module to the menu. C file_ Device. H is enough
#include“lcd_ Device.h / / contains the LCD driver header file, and then you can call //lcd_ in the.C file. The global function in device. H uses the global / / variable (if any) in the LCD driver// Call the write character to LCD function WR_ lcd (0x01,0x30); … // Assign value = 0xff; to global variable
The external functions and data provided by a module to be called by other modules shall be declared with the extern keyword in the. H file;
This sentence has been reflected in the above example, that is, the external functions and global variables provided by a module to other modules need to be declared with the extern keyword in the. H file. The following focuses on the use of global variables. A difficulty in using modular programming (compared with novices) is the setting of global variables. Beginners often find it difficult to figure out how to realize the variables shared by modules and modules. The conventional practice is mentioned in this sentence. The external data in the. H file is declared with the keyword extern. For example, the variable value in the above example is a global variable. If a module also uses this variable, just include #include "LCD" in the module. C file used, just like using external functions_ Device. H ". Another method to deal with global variables between modules comes from the embedded operating system uCOS-II. This operating system has a special method to deal with global variables, which is also difficult to understand, but it has infinite magic after learning. This method only needs to be defined once in the header file. The method is: in the. H header file that defines all global variables (uCOS-II defines all global variables in one. H file):
#ifdef xxx_ GLOBALS #define xxx_ EXT #else #define xxx_ XXX is added to every global variable in the EXT extern #endif. H file_ Prefix of ext. XXX stands for the name of the module. The. C file of the module has the following definitions: #define XXX_ Globals #include "" includes. H "" when the compiler processes. C files, it forces XXX_ Ext (found in the corresponding. H file) is empty, (because XXX_ Globals (already defined). So the compiler allocates memory space to each global variable, and when the compiler processes other. C files, XXX_ Global has no definition, XXX_ Ext is defined as extern so that users can call external global variables. To illustrate this concept, see UC / OS_ 2. H, which includes the following definitions: #ifdef OS_ GLOBALS #define OS_ EXT #else #define OS_ EXT extern #endif OS_ EXT INT32U OSIdleCtr; OS_ EXT INT32U OSIdleCtrRun; OS_ EXT INT32U OSIdleCtrMax; At the same time, UCOS_ 2. H has the following definitions: #define OS_ Globals #include "includes. H" when the compiler processes UCOS_ 2. C, it makes the header file as follows, because OS_ Ext is set to null. INT32U OSIdleCtr; INT32U OSIdleCtrRun; INT32U OSIdleCtrMax; In this way, the compiler allocates these global variables in memory. When the compiler processes other.C files, the header file becomes the following because of OS_ Global has no definition, so OS_ Ext is defined as extern. extern INT32U OSIdleCtr; extern INT32U OSIdleCtrRun; extern INT32U OSIdleCtrMax; In this case, no memory allocation is generated, and these variables can be used by any. C file. In this way, you only need to define it once in the. H file.
The functions and global variables in the module shall be declared with the static keyword at the beginning of the. C file;
This sentence mainly describes the function of the keyword static. Static is a very important keyword. It can make some constraints on functions and variables and pass some information. For example, the delay function static void delay (uint US) defined in the. C file of the LCD driver module in the above example is decorated with static. On the one hand, it limits the scope of the function and only works in this module. On the other hand, it also conveys the message that this function will not be called by other modules. The following describes the function of this keyword in detail. In C language, the keyword static has three obvious functions:
In the function body, a variable declared static maintains its value during the call of the function.
In the module (but outside the function), a variable declared as static can be accessed by the function used in the module, but cannot be accessed by other functions outside the module. It is a local global variable.
In modules, the first mock exam is called static function, and can only be called by other functions in this module. That is, this function is restricted to the local scope of the module that declares it.
The first two are easy to understand. The last function is the delay function (static void delay (uint US)) mentioned in the example just now. The localization function plays a very good role.
Never define variables in. H files!
Compare code 1:
/*module1.h*/ int a = 5; /* Define int a * / / * module1. C * / #include "" module1. H "" / * include the. H file of module 1 in module 1 * / * module2. C * / #include "" module1. H "" / * include the. H file of module 1 in module 2 * / / * module3. C * / #include "" module1. H "" / * include the. H file of module 1 in module 3 * / the results of the above procedures are in modules 1, 2 The integer variable a is defined in 3. A corresponds to different address elements in different modules. Such a program is never needed in this world. The correct approach is: Code 2: / * module1. H * / extern int a/* Declare int a * / / * module1. C * / #include "" module1. H "" / * in the. H file of module 1 * / int a = 5 in module 1/* In the. C file of module 1, define int a * / / * module2. C * / #include "" module1. H "" / * include the. H file of module 1 in module 2 * / * module3. C * / #include "" module1. H "" / * include the. H file of module 1 in module 3 * / so that if modules 1, 2 and 3 operate a, they correspond to the same memory unit. Note: an embedded system usually includes two types (Note: two types, not two) modules:
Hardware driver module, a specific hardware corresponds to a module;
The division of software function modules shall meet the requirements of low coupling and high cohesion., Read the full text, the original title: how important is modular programming?
Source: [micro signal: GH]_ C472c2199c88, WeChat official account: embedded ARM] welcome to add attention! Please indicate the source of the article“
Our other product: