Keil5MDK creates C51 project
1 Overview
The previous article introduced the installation of Keil5MDK and C51 tools. This article introduces the use of the tools. First, it introduces how to create a 51 microcontroller project, write a demo program, compile it, and burn it to the microcontroller.
The address of the first installation tool article: https://blog.csdn.net/m0_38039437/article/details/134599013
2. Create 51 microcontroller project
2.1.Create project
1. Create a project.
Open the Keil tool and click Project
Select.New uVision Project
2. Enter the project name
, create a folder to store the project, and name the folder with the project name.
Enter the created project folder and enter the project name
3. Select the microcontroller model used in the project.
I am using the STC12C2052 microcontroller, which is domestically produced. The Keil software is foreign, so there is no such model. Here you can choose AT89C55, which is a common model for 51 microcontrollers.
4. Initialize the microcontroller.
Copy the 8051 startup code to the project and initialize it when the microcontroller starts. You can choose any one here, it won’t make any difference, this time the choice is.
5. The project creation is completed.
The project directory we created is displayed on the left side of the window.
View the project directory to display the created files
2.2.Create files
1.Create a new file
and click File
SelectNew
2. Save the file.
Select the save path under the project path C51PJT
and customize the file name. The suffix must be .c
, it is a C language file.
3. Add files to the project
Right Source Group1
-click and select Add Existing File to Group 'Source Group1'
, which in Chinese means to add existing files to Source Group1
the group.
Select led.c
the file, click Add
the Add button, and click Close
to close the window.
led.c
The file has been added to the project
4. Set the compilation output format
Click Target魔法棒log图标
, click Output
, select Create HEX File
Click OK
to close the window
5. Set the encoding format.
In order to avoid garbled Chinese characters when writing code, set the encoding format below to support Chinese.
Click 设置
the icon, select Editor
, and Encoding
select UTF-8
the encoding format in
3. Develop Demo program
1. Program code
A demo program for LED flashing is provided below. Copy the code to led.c
a file.
/*************************************************************
* 程序名 * 点亮LED灯
* 编写人 * Bruce
* 日 期 * 2023-11-24
/*************************************************************/
/*************************************************************
* 头文件定义 *
/*************************************************************/
#include <AT89X52.h>
/*************************************************************
* IO定义 *
/*************************************************************/
sbit LED = P1 ^ 7; //定义P1.7为LED控制口,低电平使能
/*************************************************************
* 毫秒级延时函数 *
调用函数必须给延时函数一个0~65535的延时值对应0MS到65535MS
/*************************************************************/
void Delay (unsigned int a){
//需要输入变量值0~65535
unsigned int i;
while( --a != 0){
//i 从0加到600,CPU大概就耗时1毫秒
for(i = 0; i < 600; i++); //空指令循环
}
}
/*************************************************************
* 主函数 *
实验板上连接到单片机上的LED闪烁程序
/*************************************************************/
void main (void){
//
while(1){
//无限循环以下程序
LED = ~LED; //取LED相反状态
Delay(250); //修改这里的数值看看会有什么变化
} //(0~65535之间)
}
Click the compile button. If there is no error message in the compilation output column, it means that the file is compiled successfully.
2.Circuit principle
Insert the positive pole of the LED light into the 20th pin VCC of the microcontroller, and the negative pole into the 19-pin IO port
3.Experimental results
1. Open the STC-ISP tool, select the compiled c.hex
file, and burn it to the microcontroller