Modular Programming in KEIL

Publisher:耿高良Latest update time:2016-12-26 Source: eefocusKeywords:KEIL Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

  When using KEIL, we are used to writing what we want to write in a .c file in the order of our own thoughts. This is a very common way of writing. When the program is relatively short, such as dozens of lines or more than a hundred lines, there is no problem. But when the program is very long, for example, if you want to use LCD to display data, there are several LCD-related functions, and then you want to display the temperature on the LCD, then there must be DS18B20-related operations, which have several related functions. If you also want to add the time display function of DS1302, then there are many more functions. In this way, it is normal for a program to have hundreds of lines. For the program you wrote, it may be clearer in your mind and not too messy, but when you give the program you wrote to others to read, others often read it in the clouds, and often don't know what you wrote after reading it.
  If you have written a relatively long program like an electronic clock, you must have downloaded related programs on the Internet and read them. Have you ever felt that other people's programs look very depressed? Haha. Now let's introduce a module writing method in C language in KEIL. This writing method is actually very useful and is often seen in some slightly longer programs. The structure is shown in the figure below:

  Does it look familiar? That's right. In fact, if you have learned the C language for PC and are familiar with compiling multiple files, then this is not a problem, because it is basically the same. If you are an expert and are familiar with it, then please skip this article; if you are not very familiar with it and are a little interested in it, then this article may be helpful to you. If there is anything wrong with this article, please post it in the comments. Or leave me a message on my homepage for communication.
  This tutorial is not easy to explain clearly in the form of text. If it is done with a video, the effect should be much better, but I don't have this condition (I am not good at Mandarin and I am afraid of scaring everyone, haha). It may not be finished in one post, and typing is a very painful thing, so please forgive me for this. Let's start officially.
  Our main purpose is to learn modular writing, so the function is secondary. After getting familiar with this writing, the function is controlled by everyone. Now we will take the control of LED lights as an example.
  So, we first create three .c files, named main.c, delay.c and led_on.c, and try to make the program function visible by looking at the file name when creating the file, so that it is more intuitive and not easy to be confused. Then add all three files to the project. (This is not impossible, right?)
In delay.c, we add the following code:

void delay1s()
{
    unsigned int m,n; for(m=1000;m>0;m--) for(n=20;n>0;n--);
}

 


Of course, the actual delay time of this delay function is not one second, so we don't need to worry about it for now. Just know that it has a delay effect. In the file led_on.c, add the following code:

void led_on()
{
    P0=0x00;
    delay1s();
    P0=0xff;
    delay1s();
}

 


Then we add the following code in the main.c function:

void main()
{
    led_on();
}

 

  The function of this program is very simple, which is to realize the flashing of LED.
  The next question is how to associate these three C files.
  In fact, in a single .c file program, the first thing we do when writing a program is to write #include . If you are a good student, you must have asked why you should write a sentence like this. If you have attended a tutoring class, the teacher must have told you that reg52.h is a header file, and the purpose of this sentence is to include the header file. Of course, this is very correct. You can open reg52.h and take a look at the content inside. It contains some definitions about 51 single-chip microcomputers. If something is missing in this file, you can use the command sfr to define it in the C file. For example, when expanding RAM in STC89C52, a register will be used. You can add it to this file or define it in the C file with sfr. Think about it further. An include command can include a file. Then, can't you include more files by including different header files? Is there a little idea?
  Let's talk about it here first. Next time, let's see how to associate the three 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 in the project directory and then 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 directly 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. 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 need to 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 duplicate definition. This is its function. But please note that the header file does not participate in the compilation when the compiler is compiling.

  Again, we create a led_on.h file, and 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, let’s complete the three functions we talked about last time.

  In the led_on() function, we used the definitions of some registers of the 51 MCU, so we need to include reg52.h, and we used the delay1s() function, so we need to include delay1s.h. Therefore, 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(); //In fact, only the first sentence is enough here, this sentence is unnecessary
    led_on(); //This is also unnecessary}

 

  In this function, in order to explain again the definition of the #ifndef...#define...#endif structure, you can remove this structure from all .h files and then compile it to see the effect.

  I believe that everyone has a general understanding of this modular writing method. If we want to add a new function, such as a running light function, then we only need to add a led_circle.c and led_circle.h, and then add them to the project according to the above steps. The rest of the program does not need to be changed. Obviously, this is very convenient. In fact, the extern keyword can be used in the function declaration. This type is the default in C language, so it is not necessary to write it.

  If there is anything that is not clear, please raise it and we will discuss it together. Since these things are entered manually, there will inevitably be errors. If you find any errors or inappropriateness in this tutorial, please point it out and I will correct it in time to avoid misleading others, hehe.

  Finally, I attach a reference example, which can be used directly on the CEPARK 51 board to see the experimental results. I hope this can be helpful to everyone.


Keywords:KEIL Reference address:Modular Programming in KEIL

Previous article:MCU C language modular programming
Next article:C51 Programming Specifications

Recommended ReadingLatest update time:2024-11-16 13:49

The keil5 project generated by stm32cube crashes when adding a group
Through trial and error, we found a method that can be used: Right-click on New Group and select Manage Progect Item... You can modify the group name and add items.
[Microcontroller]
The keil5 project generated by stm32cube crashes when adding a group
Keil's software simulation and hardware simulation
1. Software Simulation     Keil has a powerful software simulation function. Through software simulation, many problems that will occur can be discovered. Keil simulation can view many hardware-related registers. By observing the changes in these register values, you can know whether the code is running normally. Th
[Microcontroller]
Keil's software simulation and hardware simulation
Keil C51 interrupt program rules
The interrupt function attribute when included in a declaration specifies that the function is an interrupt function. For example, unsigned int interruptcnt; unsigned char second; void timer0 (void) interrupt 1 using 2  {     if (++interruptcnt == 4000) { /* count to 4000 */     second++; /* second counter */      in
[Microcontroller]
Solution to reentrancy problem in keil
In keil c, you can use the keyword reentrant to define, such as: void fun(void) reentrant{} , but you cannot use BOOL variables in the function, because KEIL defaults to positioning all variables at the top of the external RAM, and the external RAM cannot be bit-addressed. In addition, if your external RAM is less th
[Microcontroller]
KEIL for ARM comes with a sample program virtual serial port debugging
1. Install Keil for ARM first, you can refer to my previous blog post Keil uVision4 registration machine download and installation  2. Then double-click as shown below 3. Then use VSPD to virtualize 2 serial ports (com1, com2) 4. Then enter in the Kiel command line  MODE COM1 4800,0,8,1 ASSIGN COM1
[Microcontroller]
KEIL for ARM comes with a sample program virtual serial port debugging
Keil debugging method in actual project development
1. In the debugging state of Keilc, how to observe the operating status of each on-chip peripheral? How to modify their settings? ​   In the debugging state, click on different peripheral option commands under the Peripherals menu to display or hide the observation window of the corresponding peripheral.    
[Microcontroller]
Keil software simulation serial port debugging skills
introduction In the single-chip microcomputer system, the serial port (UART, Universal Asynchronous Receiver and Transmitter Interface) is a very important component. The single-chip microcomputer serial port is usually used to connect to the host computer through the RS232/RS485 level conversion chip to exchan
[Microcontroller]
Keil software simulation serial port debugging skills
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号