Article From:


What is modular programming? That is, you can divide your learning program into modules to facilitate modification and transplantation. In the aspect of microcontroller and embedded, programming has powerful portability, which can be regarded as the top priority. If you write some small programs like LED digital tube,

Modular programming is a big problem. But in the team to do the project, need different people to be responsible for a different section of the program, at this time the need for modular programming, in the personal compilation of the program also have to use, because of good modification, think of your code dozens of lines are still good, if

How about hundreds of thousands of lines? So here, we can see that modular programming is almost all big programs to do, otherwise how to modify? Especially the flexible language of the single chip microcomputer.(It is suggested that the students with the basis of the C language look at it.

      First, I want to talk about the key words associated with modular programming in advance.



   typedef(Type definition)



#ifndef __**_H_  //If there is a **.h file, just.

#define __**_H_  //If you don’t find a **.h file


#endif  //Otherwise it’s over

         Ha ha, first look at the warm-up, we know that the C language has.cFiles and.H files,.cThe file is the program we write. The.H file is a library file, when writing the C language program, #include “***.h” or include< ***.h> it is the meaning of finding the library file path, just starting to find the different starting point,

For example, the printf output function has this function in the stdio reservoir, and we are just using it. I want to express that the library files are stored in the library to make it convenient for us to write the function of the program (also the program, but packaged in the form of functions for us).

By The WaycodethisA keyword, such as int code arr[]={}, means that the space occupied by the I variable is placed in the ROM, that is, read only memory, so that the dynamic data can have more space speed and faster CPU. The memory of the single chip microcomputer is veryValuable, RAM’s capacity is compared to ROMSmall, and RAM and CPU hook, in fact, the performance of our mobile phone is very good with the RAM has a great relationship, we say that the running memory is RAM, too professional small editor will not, we know that code can save the SCM memory.

Key words are linked to memory. C language is also hard to remember in memory. We master basic usage to compile C51, OK?

       Want to understand in depthstaticThe point of this link

static The function in the program is that when you add static in front of a variable or function, it means that the function or variable can only be used in this C file, equivalent to a lock to lock them.

           Want to understand in depthexternThe point of this link

typedefAnd #defineThe definition of two macros is very common. Later, it will create a library for typedef conveniently.

   For example, you want to define data types now

     It can be like thistypedef  int   P;(Bonus number)

                    #define  P  int(No symbols are allowed.)

We can use int to define variables directly in the back, P i=0 (int i=0); we use macro definitions to give this data a new name P; a lot of the big programs in C51;

  typedef unsigned int  uInt8;      typedef unsigned char uChar16; It means replacing the type of unsigned int with the new uInt8 name.

Next, start modularized programming, try to follow the steps, first create a folder. 



On the inside

Four folders are set up,.C files are placed in C folders,.H files are placed in H folders, main.c main function files are placed in main folders, and some project files are placed in project folders;

It might be a little dizzy. Yes, I’ll understand it after all. Open the new project of keil software.

 Put it in the project folder; get the name pro, of course, pick it up.

After that, C and H files were created, and a C file corresponds to a H file.



 Named delay.c delay.h led.h led.c main.c, respectively, to the C and H main folder previously established.


 main.cThe main C file is placed in the main folder alone

All the saving effects are like this

A C file corresponds to a h file. The name is the same, so it’s easy to find. Now you can first program in the C file, or you can set up some things in advance. This sequence is all right. Let me set it first.

Point this; pop out

You can label 1 as a large folder, label 2 as a subfile, and number 3 as a C file corresponding to the subfolder.Here I take these names, then I go to the C file.

The blue bottom width represents the imported directory under the selected directory.

Next is the same step to import the corresponding C file, and click OK after completion.

After completion, it is such a layout effect.

The problem comes, so many C files, we want to tell the software to the compiler, or we can not find H is the library file, not modularized programming, we need to take the #include “reg52.h” for the software, and now we want to do it.

Make #include “” these, so give the path,

The third is to set the crystal oscillator frequency, which is the same as the crystal frequency of the program. Fourth is to check the survival HEX file.

Fifth is a map of the path

At this box point 6,

Point 7 establish x to delete

After 7, there are 8 points out of the box, 8 add the H folder on the folder, and then OK.

There are two warnings after compiling because two H folders are useless, then programming.


There is also a special note that the name of #include “**.h” must be the same as the name of the H folder.

Yes, the delayh file format is like this; the function name is inside all; in fact, extern is added before void, but the keil5 software is added before the default, so it can not be added; and that is the addition of #ifndef after the #ifndef.__DELAY_H_  The red part is a name that can be casually repeated, but no matter what name it is, it has to be the same as the following #define.

LEDThe C program refers to the Delay_Ms function.

So next

The library wrapped in delay is included in the H of LEDSo now, how many libraries are there in #include “led.h”? 52 of the Lord has delay.

The main program, main, is compiled.

Zero error warning

Did you see?The arrows are those H files that are stacked up, so now I have a little understanding of the meaning of these formats and the strength of modular programming.

Practice a few times, deepen understanding, and learn here that readers can go online to see other modular programs, there are many techniques I do not say, the foundation can slowly understand the knowledge of others, and then

Do it, and finally form your own programming style.









































Leave a Reply

Your email address will not be published. Required fields are marked *