First, we need a new document. There are two ways to create this document (taking the delay1s function as an example). The first way is to create a delay1s.txt file in the project directory and then rename it to delay1s.h. Because they are all in the same encoding, there will be no confusion.
The second method is to create a new document in the project, and then save it as delay1s.h. If you need to add a lot of files, it is recommended to use the first method. This is my personal suggestion.
Next, we need to write the content of the delay1s.h file, which is as follows:
#ifndef _DELAY1S_H_
#define _DELAY1S_H_
void delay1s(); //delay function
#endif
This is the definition of the header file, which is used to declare the delay1s() function. If we need to use the delay1s() function in other functions, an error will occur during compilation if it is not declared in advance. For #ifndef...#define...
#endif; This structure roughly means that if a string is not defined (macro definition), then we define it and then execute the following statements. If it is already defined, then skip and do not execute any statements.
Why do we use such a definition method? For example, in the led_on() function, we call the delay1s() function, and then in the main() function, we also call the delay() function. Then, in the led_on() function, I have to include the header file
delay1s.h, and then include delay1s.h in the main() function. If we call led_on() in the main function, then when we encounter delay1s() and led_on() during compilation, delay1s.h will be interpreted twice, so
error. If the above preprocessing command is used, then _DELAY1S_H_ will have been defined the second time, so there will be no duplicate definition problem. This is its function. But please note that the header file is not referenced when the compiler is compiling.
And compile.
Again, we create a led_on.h, the code content is as follows:
#ifndef _LED_ON_H_
#define _LED_ON_H_
void led_on(); // light flashes
#endif
The function is the same as delay1s.h. If you don’t understand, you can look at the explanation above again.
Finally, complete the three functions we mentioned last time.
In the led_on() function, we used the definitions of some registers of the 51 microcontroller, so we have to include reg52.h, and we used the delay1s() function, so we have to include delay1s.h, so the code of the led_on() function is as follows:
#include
#include “delay1s.h” //Note that there is no semicolon here
void led_on()
{
P0=0x00;
delay1s();
P0=0xff;
delay1s();
}
The code of the Main function is as follows:
#include
#include “delay1s.h”
void main()
{
led_on();
delay1s(); //Actually, only the first sentence is needed here, this sentence is unnecessary
led_on(); //This is also unnecessary
}
In this function, in order to explain the definition of the #ifndef...#define...#endif structure again, you can remove this structure in all .h files, and then compile and see the effect.
I believe that everyone has a general understanding of this modular writing method. If we want to add new functions, such as adding a running light function, then we only need to add a led_circle.c and led_circle.h, and then
Then follow the above steps to add it to the project, and the rest of the program does not need to be changed. Obviously this is very convenient. In fact, the function declaration can use the extern keyword, which is the default type in C language, so you don't need to write it.
2. Notes
When you define a variable in a C file (named A), but want to call it in another C file (named B), you need to declare it in the B file, indicating that you have defined it.
For example:
Definition in A: unsigned char flag;
Note that you need to add the type of the variable in B, otherwise there may be problems.
h file, only function declarations and global variables should be placed in it, nothing else
Previous article:Serial communication between Labview and C51 microcontroller
Next article:How to access external RAM in C51
Recommended ReadingLatest update time:2024-11-15 10:29
- Popular Resources
- Popular amplifiers
- MCU C language programming and Proteus simulation technology (Xu Aijun)
- Single-chip microcomputer C language programming and simulation
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- Fundamentals and Applications of Single Chip Microcomputers (Edited by Zhang Liguang and Chen Zhongxiao)
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
- How to turn on the internal enable of Panasonic servo drive?
- What is the rigidity setting of Panasonic servo drive?
- How to change the inertia ratio of Panasonic servo drive
- What is the inertia ratio of the servo motor?
- Is it better for the motor to have a large or small moment of inertia?
- What is the difference between low inertia and high inertia of servo motors?
- Features of Storage Products for Embedded and Industrial Applications
- 【CH579M-R1】Drive OLED to prepare for the project
- [Repost] Only these 8 tips are needed to restore the circuit board to the circuit diagram
- EEWORLD University Hall----Principles of Operating Systems by Chen Xiangqun, Peking University
- Anxinke NB-IoT module evaluation-Shangdian
- After more than two months of hard work, it is finally finished. Haha
- Live Review: Renesas Electronics R-Car Advanced Driver Assistance System Solution
- TI CC2531 USB Evaluation Module Kit
- Lantern with capacitive touch support
- TI's active equalization solution