Modular programming of embedded C language

Source: Internet
Author: User

reproduced in the original: http://blog.csdn.net/zhzht19861011/article/details/5974945, thank you very much.


Beginners often do not know how to modular programming, in fact, it is simple and easy to learn, but also the organization of good program structure is one of the effective methods.

This article will first about the modular approach and considerations, and finally to the beginner's most widely used Keil C compiler as an example, give the detailed steps of modular programming.

Modular programming should understand the following overview:

(1) The module is a combination of a. c file and a. h file, and the header file (. h) is a declaration of the module interface;

This article summarizes the modular implementation of the method and the essence: the code of a functional module to write a separate. c file, and then put the module's interface function in the. h file. For example: If you use LCD display, then you may write a liquid crystal driver module, to achieve the character, character and image of the reality, named: LED _DEVICE.C, the module's. c file can generally be written as:

[CPP]  View plain  copy  print? /*************************************************************************  *  LCD Drive module   *  *   text   pieces: lcd_device.c  *    Write   people:  small caps   *  Description: LCD serial Display driver module, provide characters, Implementation interface   *  writing time: 2009.07.03  *  Edition  :1.2  ********************************** of Chinese characters and images /   #include  ...   ...  //define variable     unsigned char value;//global variable    ...  //Definition function   //This is the first function of this module, which acts as a delay, function calls for this module only, so use the static keyword modifier   /******************** delay subroutine ************************/   static  void delay  (uint us)  //delay time   {}  //This is the second function of this module, To invoke   /********************* in other modules **************************  **  function: Write characters to LCD   * *   parameter:dat_comm  1 writes the data, for 0 writes the instruction   content  for the written number or the instruction  /   void wr_lcd  (Uchar dat_comm, uchar content)    {}   ......   ......  /*****************************  end files***********************************/  

Note: Only these two functions are written here, and the first delay function is scoped to the module, and the second is required by other modules. To simplify, the function body is not written here.

The interface of the module is given in the. h file. In the example above, write the character function to the LCD: WR_LCD (uchar Dat_comm,uchar content) is an interface function because other modules call it, This function must be declared as an external function in the. h file (decorated with the Extrun keyword). Another delay function: void delay (UINT us) is only used in this module (local function, decorated with the static keyword), so it does not need to be placed in the. h file.

The. h file format is as follows:

[CPP] view plain copy print? /***************************************************************************** * LCD Driver Module Header file * * File: Lcd_device.h * Series Writer: Small caps * Written time: 2010.07.03 * Version: 1.0 ********************************************************************************* ///DECLARE global variable extern unsigned char value;//declare interface function extern void Wr_lcd (Uchar dat_comm,uchar content); Write characters to LCD .../***************************** end files***********************************/

note here three points:

1. In the Keil compiler, the extern keyword, even if not declared, does not make an error, and the program works well, but it does not guarantee the use of other compilers. We strongly recommend that you develop a good programming standard.

2.. The functions in the C file appear in the. h file only when other modules are in use, like the local delay function static void delay (UINT us), even if it occurs in the. h file, because the other modules do not call it at all, and are not actually able to call it ( The constraint of the static keyword).

3. Note that the end of this sentence must be added with a semicolon ";", I believe that many students have encountered this strange compiler error: Error C132: ' xxxx ': not in formal parameter list, this error is actually. h's function declaration last missing semicolon's sake.

Module Application : If you need to use the lcd_device.c function void Wr_lcd (Uchar Dat_comm,uchar content) in the LCD menu module LCD_MENU.C, Simply add the header file of the LCD driver module to the lcd_menu.c file of the LCD menu module LCD_DEVICE.H.

[CPP] view plain copy print?

Related Article

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.