How Keil5 creates a 51 microcontroller multi-module project

  • Keil5 How to create a 51 microcontroller multi-module project
    • Premise basis
    • Create project
    • Create module
      • Create multi-module directories and files
        • Create module folder
        • Add main source file
        • Add public public module
        • Add new modules to the app
      • Create group
        • Click the Project Management Entry button
        • Create corresponding groups based on previous modules
      • Add module files to group
      • Write code
        • public module
        • timer0 module
      • Configure project parameters
        • Configuration header file path
        • Configure project target output
      • Build project
    • Analysis and Summary

How to create a 51 microcontroller multi-file project in Keil5

Basic premise

  1. Keil5 software downloaded
  2. Understand C language project construction and basic syntax
  3. Learn about microcontroller project development

Create project

  1. Open the file explorer, create an empty folder in advance, and customize the file name.

  2. Open Keil5, click Project on the menu bar and select New uVision Project

  1. In the pop-up file explorer, select and enter the pre-created folder in Step 1, and then enter the name of the project below. It can be customized, but usually use the universal Project name, for example: project, template

  2. In the pop-up dialog box, enter the chip search keyword and select the corresponding chip type from the search results. The author uses the STC89C52 chip. Readers need to choose their own development board. chip type, and finally click the ok button

  3. Click No in the pop-up dialog box to complete the project creation.

  4. Open the directory where the project is located. The newly created directory structure is as follows

Create module

First of all, we need to understand a concept, group. Group is a basic unit of Keil5 project management. Groups are just the directory structure that Keil5 shows to us, not the real directory structure.

Part 6 of the previous section of the article shows the real directory structure of the newly created project, but what we observe in the software is the structure of group.

According to the picture above, we can see that Keil5 automatically created a group Source Group1 for us

To create a multi-file project, we need to create multiple folders and save different module codes to different folders, which is a traditional C language project.

Create multi-module directories and files

Because the real directory structure cannot be displayed in Keil5, we need to create multi-module directories and related codes in File Explorer.

The following creates common modules:

Create module folder

Create three empty folders under the project path, with the names: user, app and public.

  • The user module is mainly used to write the main function;
  • The app module is mainly used to store functional modules of microcontroller peripherals;
  • The public module is mainly used to store public modules;

Readers can also divide modules according to their own projects.
The creation effect is as follows:

Add main source file

Enter the user folder, create a new text file, and rename it to the main.c file. The effect is as follows:

Add public public module

Enter the public folder and create the public.c and public.h files. The effect is as follows:

Add new modules to the app

Enter the app folder and create a new folder timer, then enter the timer folder and create timer0.h and timer0 .c file, the effect is as follows:

Note: This is just a demonstration of adding a new module, readers can add their own modules

Create group

The concept of group was introduced before. Group is a logical structure of keil project, not the structure of our project file. However, the construction of the keil project takes groups as the basic unit. After building the relevant modules above, we need to build groups based on the modules.

Click the project management entry button

Create corresponding groups based on previous modules

First modify the name of the default group, double-click to modify it to app

After clicking the new button to create a new group, change the name to public

Then continue to create a new group, the final effect is as follows:

At the same time, the project structure on the left has also changed:

Add module files to the group

There are two ways to add files to the group, one is to create a new file, and the other is to select an existing file.
Click on the group you want to add files to, then right-click to see the menu:

Since we have already created the file before, select New file to add to group.

Then add public.c, public.h, timer0.h and timer0.c to the corresponding groups .
Remember to select the following file type when adding the .h file:

The final project results are as follows:

Write code

publicmodule

The public module mainly saves some common header files, aliases and functions. You can directly reference public.h when writing other modules in the future.

public.h file:

#ifndef __PUBLIC_H__
#define __PUBLIC_H__

#include <REGX52.h>

//Define common data type aliases
typedef unsigned int u16;
typedef unsigned char u8;

//Define common delay functions
void delay_10us(u16 ten_us);
void delay_1ms(u16 ten_us);
#endif

public.c file

#include <intrins.h>
#include "public.h"

/****************************************************** ****************************
* Function name: delay_10us
* Function: Delay function, when ten_us=1, the delay is about 10us, @12.000MHz
* Input: ten_us
* Output: None
*************************************************** *******************************/
void delay_10us(u16 ten_us)
{<!-- -->
    unsigned char i;
while(ten_us--)
    {<!-- -->
        _nop_();
        _nop_();
        i = 27;
        while (--i);
    }
}

/****************************************************** ****************************
* Function name: delay_1ms
* Function: Delay function, when ten_us=1, the delay is approximately 1ms, @12.000MHz
* Input: ten_us
* Output: None
*************************************************** *******************************/
void delay_1ms(u16 ten_us) //@12.000MHz
{<!-- -->
unsigned char i, j;

while (ten_us--)
    {<!-- -->
        i = 12;
        j = 169;
        do
        {<!-- -->
            while (--j);
        } while (--i);
    }
}
timer0module

This module is a timer module, and the code is as follows (readers can add their own timer module code):

timer0.c file

#include "public.h"

//The initialization value of the register corresponding to the timer trigger frequency, in order: every: 1ms, 100us, trigger once
// static unsigned char timer_init_value [][2] = {<!-- -->{0x18, 0xFC}, {0x9C, 0xFF}};

/**
* @brief Timer 0 is initialized. The default is to generate an interrupt every 1ms. The initial value of the timer can be modified.
* @param none
* @retval None
*/
void timer_0_init(void)
{<!-- -->
    //Only modify the last four digits, the first four digits represent Timer1
    TMOD &= 0xF0;
    TMOD |= 0x01;

    //Set the timer initial value
    TL0 = 0x18;
    TH0 = 0xFC;

    // Set the overflow flag to be valid
    TF0 = 0;

    // Use TR0 to control the timer
    TR0 = 1;

    //Interrupt switch and priority
    ET0 = 1;
    EA = 1;
    PT0 = 0;
}

/*Timer interrupt function template, the default is to perform operations every second
void Timer0_Routine() interrupt 1
{
static unsigned int T0Count;
TL0 = 0x18; //Set the initial value of the timing
TH0 = 0xFC; //Set the initial value of the timing
T0Count + + ;
if(T0Count >= 1000)
{
T0Count=0;
\t\t
}
}
*/

timer0.h file

#ifndef __TIMER_H__
#define __TIMER_H__

void timer_0_init(void);
#endif

Configure project parameters

Configuration header file path

Click the Magic Wand, select C51 in the pop-up dialog box, and then click the include button:

Then add the directories of the header files public.h and timer0.h required for the project in the pop-up dialog box.

Then select the directory where the header file is located.
The final effect is as follows:

Configure project target output

Click Magic Wand, then click output, and then check Generate HEX file.

The project generates the output folder for us by default, which is the Objects directory after we create a new project.

Build project

Click the build button, and then you can see the output of the Console.

At this point, the multi-module project framework has been built. If you want to add new modules, just follow the above operations.

Analysis and summary

After the above build operation, you can feel that the project construction of Keil is a little different from the normal C language project, that is, the logical structure and file structure of the project are inconsistent. The reason for this is because keil has a project’s xml configuration file, which is the project.uvproj when creating a new project.

project.uvproj is essentially a xml file. This file configures all the information of the project, including the group structure, include path and other information.

We use vscode to open the file (any software that can open xml files will do) and check the end of the file:

It can be found that the group information is configured in this file, and all our operations above have corresponding configuration information.
This article will not continue to explain the configuration file in detail. If you are interested, you can search for information online.

Since keil does not support the code completion function, the author will publish an article in the next issue where vscode replaces keil’s editing!
I believe smart readers have already figured out how to use vscode to edit keil projects!

? 2023 [email protected] All Rights Reserved.
This article is an original article by the blogger and follows the CC 4.0 BY-SA copyright agreement. Please attach the original source link and this statement when reprinting.