Embedded C language-modular programming

Source: Internet
Author: User

When you create a relatively complex project in a project group, it means that you do not work alone. You need to work with your team members to complete the project. This requires the 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 for independent debugging and set aside interfaces for other modules to call. Finally, after the team members write and debug the modules they are responsible for, the project leader can perform combined debugging. In such cases, programs must be modularized. There are many advantages of modularization, not only to facilitate the division of labor, but also to facilitate program debugging, to facilitate the division of program structures, and to increase the readability and portability of programs.

Beginners often do not understand how to implement Modular programming. In fact, it is easy to learn and one of the effective methods to organize a good program structure.

This article will first give a rough look at the modular methods and precautions. Finally, we will take the Keil C compiler, which is the most widely used by beginners, as an example to illustrate the detailed steps of Modular programming.

Modular program design should understand the following overview:

(1) A module is the combination of a. c file and A. H file. The header file (. h) declares the interface of this module;

This article summarizes the modular implementation method and essence: write the code of a function module into a separate one. c file, and then put the interface functions of this module in. h file. for example, if you use LCD, you may write an LCD Driver Module to implement the reality of characters, Chinese characters, and images. It is named led_device.c. the C file can be written as follows:

/*************************************** **********************************

* LCD Driver Module

*

* File: LCD _device.c

* Prepared by: Small bottle caps

* Description: The LCD serial display driver module provides interfaces for character, Chinese character, and image implementation.

* Written at: 2009.07.03

* Version 1.2

**************************************** *********************************/

# Include...

...

// Define variables

Unsigned char value; // global variable

...

// Define a function

// This is the first function in this module. It plays a delayed role and is only called by the function in this module. Therefore, it is modified using the static keyword.

***************** *******/

Static void delay (uint US) // Delay Time

{}

// This is the second function of this module. It must be called in other modules.

*************** ***********

** Function: write characters to the LCD

** Parameter: If dat_comm is set to 1, data is written, and if dat_comm is set to 0, commands are written.

Content is the number or instruction written.

**************************************** **************/

Void wr_ LCD (uchar dat_comm, uchar content)

{}

......

......

/*************************** End files ******** ***************************/

Note: Only these two functions are written here. The scope of the first delay function is within the module, and the second function is required by other modules. The function body is not written here for simplicity.

The module interface is provided in the. h file. In the above example, Write the character function to the LCD: wr_ LCD (uchar dat_comm, uchar content) is an interface function, because other modules will call it. in the H file, this function must be declared as an external function (modified with the extrun keyword), and another delay function: void delay (uint US) is only used in this module (local function, modified with the static keyword), so it does not need to be placed in. in the H file.

The. h file format is as follows:

/*************************************** **************************************

* LCD Driver Module header file

*

* File: LCD _device.h

* Prepared by: Small bottle caps

* Written at: 2010.07.03

* Version 1.0

**************************************** **************************************** */

// Declare global variables

Extern unsigned char value;

// Declare an interface function

Extern void wr_ LCD (uchar dat_comm, uchar content); // write characters to the LCD

......

/*************************** End files ******** ***************************/

Note the following three points:

1. In the Keil compiler, even if the extern keyword is not declared, the compiler will not report an error and the program runs well, but it is not guaranteed to use other compilers. We strongly recommend that you add and develop good programming specifications.

2 .. functions in the C file appear only when other modules are used. in the H file, such as the Local delay function static void delay (uint US), even if. H files are also useless, because other modules do not call it at all, and actually cannot call it (the limitation of the static keyword ).

3. note that the extra points must be added at the end of this sentence. ", I believe many people have encountered this strange compiler error: Error c132: 'xxx': Not in formal parameter list, this error is actually. the semicolon is missing at the end of the H function declaration.

Application of modules: If you need to use the void wr_ LCD (uchar dat_comm, uchar content) function in the LCD menu module LCD _menu.c ), you only need to add the LCD Driver Module header file LCD _device.h to the LCD _menu.c file of the LCD menu module.

/*************************************** ************************************

* LCD menu Module

*

* File: LCD _menu.c

* Prepared by: Small bottle caps

* Description: The LCD menu module provides a maximum of 256 menus, regardless of hardware.

* Written at: 2010.07.03

* Version 1.0

**************************************** **********************************/

# Include "LCD _device.h // contains the LCD Driver header file, which can be found later. in the c file, call the global function in // LCD _device.h and use the global // variable in the LCD Driver (if any ).

...

// Call the write character function to the LCD.

Wr_ LCD (0x01,0x30 );

...

// Assign values to global variables

Value = 0xff;

...

(2) external functions and data provided by a module to other modules must be declared with the extern keyword in the. h file;

The preceding example shows that the external functions and global variables provided by a module to other modules must be in. the header in the file in H is declared with the extern keyword. The following describes the use of global variables. One difficulty in Modular programming (compared with beginners) is the setting of global variables. It is difficult for beginners to figure out how to implement variables shared by modules, the conventional approach is mentioned in this sentence. in the H file, the external data is declared with the extern keyword. For example, the variable value in the preceding example is a global variable. If a module also uses this variable, it is used in the same way as an external function. the C file contains # include "LCD _device.h.

Another way to process global variables between modules is from the embedded operating system uCOS-II, which is particularly difficult to understand and process global variables. However, after learning this method, it will be incredibly useful, this method can be defined only once in the header file. Method:

In the. h header file that defines all global variables (uCOS-II defines all global variables in a. h file:

# Ifdef xxx_globals

# Define xxx_ext

# Else

# Define xxx_ext extern

# Endif

In the. h file, each global variable is prefixed with xxx_ext. Xxx indicates the name of the module.

The. c file of this module has the following definitions:

# Define xxx_globals

# Include "des. H"

When the compiler processes the. c file, it forces xxx_ext (which can be found in the corresponding. h file) to be null (because xxx_globals has been defined ). Therefore, the compiler allocates memory space for each global variable. When the compiler processes other. c files, xxx_global is not defined, and xxx_ext is defined as extern, so that you can call external global variables. To illustrate this concept, see uC/OS _ii.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_ii.h has the following definitions:

# Define OS _globals

# Include "primary des. h"

When the compiler processes ucos_ii.c, it turns the header file into the following, because OS _ext is set to null.

Int32u osidlectr;

Int32u osidlectrrun;

Int32u osidlectrmax;

In this way, the compiler will allocate these global variables to the memory. When the compiler processes other. c files, the header file becomes as follows. Because OS _global is not defined, 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 in any. c file. In this way, you only need to define it once in the. h file.

(3) Functions and global variables in the module must be declared with the static keyword at the beginning of the. c file;

This statement mainly describes the role of the keyword static. Static is a very important keyword. It can constrain functions and variables and pass some information. For example, the above example is used in the LCD driver module. static void delay (uint US), a latency function defined in file C, is decorated with static. On the one hand, it limits the function scope to only play a role in this module, on the other hand, the function will not be called by other modules. The role of this keyword is described in detail below. In C language, the keyword static has three obvious roles:

1. In the function body, a variable declared as static remains unchanged when the function is called.

2. Within a module (but in the external body of the function), a variable declared as static can be accessed by the function used in the module, but not by other functions outside the module. It is a local Global Change

Quantity.

3. In a module, a function declared as static can only be called by other functions in the module. That is, this function is restricted to use within the local scope of the module that declares it.

The first two functions are easy to understand. The last function is the latency function (static void delay (uint US) just mentioned in the example. The localization function has a very good effect.

(4) never define variables in the. h file!

Haha, it seems a bit alarmist, but I don't think many people will define variables in the. h file.

Compare the Code:

Code 1:

/* Module1.h */

Int A = 5;/* define int A */In the. h file of Module 1 */

/* 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 result of the above program is that the integer variable A is defined in Modules 1, 2, and 3. A corresponds to different address elements in different modules, and such a program is never needed in the world. The correct method is:

Code 2:

/* Module1.h */

Extern int A;/* declare int A */In the. h file of Module 1 */

/* Module1. C */

# Include "module1.h"/* include the. h file of Module 1 in Module 1 */

Int A = 5;/* define int A */In the. c file of 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 */

In this way, if modules 1, 2, and 3 operate on a, the corresponding memory unit is the same.

Note:

An embedded system usually includes two types (note that there are two types, not two) modules:

(1) hardware driver module. A specific hardware corresponds to a module;

(2) The division of software functional modules should meet the requirements of low coupling and high cohesion.

The following uses the Keil C compiler as an example to describe the modular programming steps.

The following program is divided into three layers, with a total of seven modules serving as the main program service (they will also call each other ).

The program structure is as follows:

Main program modules and functions:

I. Underlying driver

1. Infrared keyboard: The program operates on the infrared keyboard. Infrared keyboard exclusive timer 0 and External Interrupt 0 to achieve infrared decoding and keyboard key value recognition. The infrared keyboard defines five buttons: Up, down, left, right, and OK.

2. LCD display: The program mainly uses LCD display information. The LCD Driver provides function interfaces for displaying Chinese characters, images, and ASCII codes. Chinese characters can be displayed in full screen or in a single line, and ASCII code can be displayed at any position. Images can also be displayed in full screen or half screen.

Ii. Functional Modules

1. LCD menu program: The Menu program makes human-computer interaction more convenient and easy. The menu depth of this menu program is limited by the ram size. Each added menu consumes 4 more bytes of RAM. The menu program schedules menu functions and refreshes the LCD display.

2. calculator program: add, subtract, multiply, and divide within 65536. overflow occurs when the range is exceeded. When overflow occurs, the LCD displays the error message "error: overflow, this operation is ignored. "-" Is displayed for negative numbers, and the LCD displays "error: Zero division" when the divisor is zero.

3. Memory Program for boot times: read and write the IIC Bus-based EEPROM. After each power-on, the microcontroller writes the boot times to the EEPROM.

4. Serial Port test program: after entering the program, the microcontroller sends the "Hello word!" string to the computer !", Send a number 24 (displayed as a character ). The purpose of writing this program is to easily send strings and variables to the computer for program debugging. Serial port occupies serial port resources and shares timer 1 with frequency measurement program

5. Frequency Measurement: Reuse timer 1, occupy External Interrupt 1, implement 5 ~ 20 KHz frequency measurement.

3. Main Program

The main program initializes the program, displays the LCD menu, monitors the keyboard program, and updates the menu based on the key value.

Steps:

1. Create a project.

2. Click file-new (or click the shortcut icon :) to create a new document.

3. Click file-save (or click the shortcut icon :) to save the newly created document, and enter LCD _device.c (LCD driver module:
LCD _device, which provides interfaces for displaying Chinese characters, characters, and images.) Click OK.

Compile the LCD driver in this document.

4. Click file-new (or click the shortcut icon :) to create a new document.

5. click file-save (or click the shortcut icon :) to save the newly created document, and enter LCD _device.h after the file name (the header file of the LCD driver module, the Module Interface and global variables are defined here ). Click OK. Sort global variables and interface functions in this document. For the effect after the preceding steps, see:

Now, the LCD driver module has been written and can be debugged separately.

6. Repeat the above steps 2 ~ 5. Define the infrared keyboard module: Key. C and key. h.

Menu module: menu. C and menu. h

Serial Communication Module: UART _. C and UART. h

Calculator module: counter. C and counter. h

Frequency measurement module: mea_fre.c and mea_fre.h

Memory Module for boot times: eepram. C and eepram. h

7. Repeat the above steps 2 ~ 3. define the main program main. c

Shows the final effect:

Complete 1 ~ After 7 steps, some Tom habitually clicks the compile button. At this time, there will be two warnings:

* ** Warning L1: unresolved external symbol

* ** Warning L2: reference made to unresolved external

This is because you have compiled program modules, but have not added them to the project.

Solution: In the project workspace box, right-click the source group 1 folder and select add files to group 'source group 1'. In the displayed dialog box, add your. c file.

A long time ago, I had my own experiences with the two warnings. At that time, I was still in college, and there was a large group of friends around me playing together. I wanted to skip classes, skip classes, and go to bed. Now... There's nothing left to think !!!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.