Modular Programming of MCU

Publisher:码梦小子Latest update time:2015-04-30 Source: 51heiKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Many MCUs produced now have expanded the 8051 core, such as internal watchdog, ADC conversion, EEPROM, PWM and other functions. When programming control programs, if you need to use these expansion modules, the program size will generally become larger and the text length will be longer. It is not very convenient when debugging the program. The better method is to perform modular programming according to the function of the program segment.

Of course, when using a general single-chip microcomputer for programming, encapsulating the program segments that achieve the same purpose and performing modular programming will also be very helpful for program maintenance and debugging.

1. When using C language for programming, you should develop good programming writing habits.

The general format at the beginning of the program is:

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

File name: File name

Author:

Version: Version number

Description: Functional description

Data: Date

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

2、{}

   Press the Tab key so that the program hierarchy is clearer.

3. Modular programming

     Encapsulate the program segment that completes a specific function, such as delay(WOED n), eeprom();, etc.

Specific steps are as follows:

 1. Create a header file

Click "New" twice in the KEIL software to create two new documents. Save them as a .C file (source file) and a .H file (header file). The .C and .H files should have the same name, and the name should be meaningful, and it is best to reflect the function of the file. For example, create a delay program, delay.c; delay.h.

2. Anti-duplicate inclusion processing

Add the following code to the .H file:

#ifndef xxx 如 #ifndef __DELAY_H__

#define  xxx    如     #define __DELAY_H__

……

#endif

3. Code encapsulation

Encapsulate the code that needs to be modularized into functions or macro definitions.

Functions must be declared in the header file.

The function body is placed in the .C file.

Macro definitions that need to be called externally are placed in the .H header file.

Try to use as few or no global variables as possible. Global declarations must be placed in .C files. When you need to call external global variables, you need to re-declare them using extern in the .H file.

4. Use source files

In the KEIL software project, add the .C file to be called. If the .C source file calls the macro definition or function in the .H file, you must add code to the .C source file to include the .H file.

5. The functions, macro definitions, and global variables in the included header files can be called freely in the .C files.


The details will be sorted out when time permits.

Keywords:MCU Reference address:Modular Programming of MCU

Previous article:MCU dynamic digital tube display hello assembly program
Next article:Detailed explanation of MCS-51 MCU assembly instructions

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号