KEIL multi-file processing in the same project

Publisher:psi33Latest update time:2015-10-22 Source: eefocusKeywords:KEIL Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
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 in the project directory and rename it to delay1s.h. Because they are all in the same encoding, there will be no garbled characters, and then open it in the project. The second way is to create a new document directly 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. Secondly, we need to write the content of the delay1s.h file, the content is as follows:

#ifndef _DELAY1S_H_
#define _DELAY1S_H_
void delay1s(); //delay function
#endif
   This is the definition of the header file, which declares 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 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, we need to include the header file delay1s.h, and then in the main() function, we also need to include delay1s.h. If we have called led_on() in the main function, then when we encounter delay1s() and led_on() during compilation, delay1s.h will be interpreted twice, and an error will occur. If there is the above preprocessing command, then the _DELAY1S_H_ has been defined the second time, so there will be no problem of repeated definition. This is its role. But note that the header file does not participate in the compilation when the compiler is compiling.
Again, we create a led_on.h, and the code content is as follows:
#ifndef _LED_ON_H_
#define _LED_ON_H_
void led_on(void);//Light flashes
#endif
The role is the same as delay1s.h. If you don't understand, you can look at the above explanation again.
Finally, we complete the three functions we mentioned above.
In the led_on() function, we used the definitions of some registers of the 51 MCU, 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.c file is as follows:
#include
#include “delay1s.h” //Note that there is no semicolon here
void led_on(void)
{
P0=0x00;
delay1s();
P0=0xff;
delay1s();
}
The code of the Main function is as follows:
#include
#include “delay1s.h”

#include “led_on.h”
void main()
{
led_on();
delay1s();
led_on();

}
In this function, in order to explain the definition of the structure #ifndef...#define...#endif again, you can remove this structure from all .h files, and then compile it to see the effect.
At this point, I believe 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 follow the above steps to add it to the project. The rest of the program does not need to be changed. Obviously this is very convenient. In fact, the extern keyword can be used for function declarations. This type is the default in C language, so it is not necessary to write it.

Keywords:KEIL Reference address:KEIL multi-file processing in the same project

Previous article:Playing with MCU, you and I are both rookies (continued 1)
Next article:Common Problems for C Language Beginners

Recommended ReadingLatest update time:2024-11-16 19:31

Instructions for running the stm32f030 program under the keil environment
Let's first look at the normal program running process of STM32: The internal flash memory (FLASH) address of STM32 starts at 0x08000000. Generally, program files are written from this address. In addition, STM32 is a microcontroller based on the Cortex-M3 core. It responds to interrupts through an "interrupt vector
[Microcontroller]
Creation and setting of Keil project files and acquisition of target files
  In addition to the necessary hardware, software is also indispensable in the development of single-chip microcomputers. There are two ways to convert the assembly language source program we write into machine code that can be executed by the CPU. One is manual assembly and the other is machine assembly. Manual assem
[Microcontroller]
How to use C language and assembly language in KEIL C51
1. Use the preprocessing instructions #asm and #endasm to use assembly language. The assembly language written by the user can be followed immediately after #asm and end before #endasm. As shown below:         # asm         /*Assembly source program*/         #endasm     The statements between #asm and #endasm will be
[Microcontroller]
Related settings when KEIL 5 downloads the program to the STM32F103ZET6 chip
1. Click the magic wand pointed by the arrow to open the project settings. (You can also open the magic wand under Project) 2. Under the Device tab, select the chip used in the project. On the right is the relevant information about the selected chip (I use STM32F103ZET6, select STM32F103ZE) 3. The crystal o
[Microcontroller]
Installation and cracking of keil software (can be used for both STM32 and C51)
Teach you how to install and crack Keil software (this article takes Keil4 software as an example) Step 1: Run the mdk.exe software and keep clicking on the default You can modify the path here Step 2: After clicking next, this dialog box will appear. You can enter any number in the first name and email fields.
[Microcontroller]
Installation and cracking of keil software (can be used for both STM32 and C51)
Keil5 installation and 51 MCU introductory program example
1. Installation of keil5 When I was looking for it before, I always thought that the official website was not very convenient to download, and I looked for it elsewhere, which was a very difficult process. Today, I tried the official website and found it very convenient. Go to the official website and click Downloads
[Microcontroller]
Keil5 installation and 51 MCU introductory program example
Use git to manage MCU project files developed by KEIL5
Prerequisites It is convenient to use KEIL5 to develop stm32. Many useless files will be generated in the process of program compilation and connection. For example, after a STM32 project is compiled normally, the size of the entire project is: After using keilkill.bat provided by Zhengdian Atom to delete useless fi
[Microcontroller]
Use git to manage MCU project files developed by KEIL5
KEIL--Generate multiple projects
KEIL can generate multiple projects according to different configurations.   Project-- Manage-- Project Components (or the icon to the right of the project name)   Add a new project in the Project Components-- Project Targets tab   Different projects can also configure different files (Project Components--
[Microcontroller]
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号