How to create multiple C files and precautions for 51 single chip microcomputer KEIL

Publisher:小星星龙猫Latest update time:2015-09-07 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
1How to create multiple C files


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;     Declaration in B: extern 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

Reference address:How to create multiple C files and precautions for 51 single chip microcomputer KEIL

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

MCS-51 single-chip computer instruction system "read-modify-write" mode instructions
The 51 MCU has four 8-bit parallel interfaces. Due to the characteristics of the internal structure, the parallel interface has different capabilities when outputting 0 and 1. When outputting 0, the capability is strong, but when outputting 1, the capability is very poor. For the P0 port, an external pull-up resistor
[Microcontroller]
How to send a group of Chinese characters from 51 MCU to PC
Recently, I saw a friend asking on the Internet how to send a string of Chinese characters from a microcontroller to a computer. In fact, this question is not difficult. Haha. Just set the relevant parameters better, and then define the Chinese characters as a group of characters and send them to the computer. In orde
[Microcontroller]
How to send a group of Chinese characters from 51 MCU to PC
The descriptive symbols of operands in the instruction system of the MCS-51 series of single-chip microcomputers
The instruction system of the MCS-51 series microcontrollers has a total of 111 instructions, which are divided into five categories: data transfer; (29) arithmetic operations; (24) logical operations; (24) control transfer; (17) bit operations. (17) The descriptors of operands in the instructions: Rn
[Microcontroller]
Two-way communication between 51 microcontrollers (Proteus simulation)
Specific function implementation: Microcontroller A can control the LED lights of microcontroller B through buttons, and microcontroller B can increase the number of the digital tube connected to microcontroller A by one through buttons. Devices used: AT89C51, buttons, resistors, 4 LED lights, 2 MAX232 chips, COM po
[Microcontroller]
Design of intelligent enteral nutrition infusion pump based on 89C51RD2 microcontroller and embedded core
With the continuous deepening of the application of various electronic systems in various fields, the requirements for the electronic systems themselves are becoming higher and higher, especially for the reliability of control system software design, real-time response and other aspects of performance. . The programmi
[Microcontroller]
Design of intelligent enteral nutrition infusion pump based on 89C51RD2 microcontroller and embedded core
Mini2440 bare metal development: Keil development environment construction
Mini2440 bare metal development: Keil development environment construction I have worked on STM32 for a while, and also worked on uboot and Linux drivers, but I feel that these are not systematic and unsystematic. I feel that what I learned is very miscellaneous, and there is no record, so I decided to stick to blog
[Microcontroller]
Mini2440 bare metal development: Keil development environment construction
Design of infrared remote control decoding program based on 51 single chip microcomputer
describe The following is a decoding program for the infrared remote control that comes with the 51 experiment board. It can read out the key value of each button on the infrared remote control and display it through the 8 LEDs of the P1 port on the experiment board. When the decoding is successful, it can also emit
[Microcontroller]
Design of infrared remote control decoding program based on 51 single chip microcomputer
51 MCU Development Principles
1. Try to use the variable type that takes up the least code space, such as unsigned character type and bit operation (8051, as an 8-bit processor, provides bit operation support, and most operation instructions are 8-bit or bit operation. It is usually more efficient to write code with small data types) 2. Use unsign
[Microcontroller]
Latest Microcontroller Articles
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号