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 assembly is rarely used now. Machine assembly is to convert the source program into machine code through assembly software. The assembly software used for MCS-51 single-chip microcomputers includes the early A51. With the continuous development of single-chip microcomputer development technology, from the widespread use of assembly language to the gradual use of high-level language development, the development software of single-chip microcomputers is also constantly developing. Keil software is currently the most popular software for developing MCS-51 series single-chip microcomputers. This can be seen from the fact that various emulator manufacturers have announced full support for Keil in recent years. Keil provides a complete development solution including C compiler, macro assembly, connector, library management and a powerful emulation debugger, and combines these parts through an integrated development environment (uVision). Running Keil software requires a Pentium or higher CPU, 16MB or more RAM, more than 20M free hard disk space, and operating systems such as WIN98, NT, WIN2000, and WINXP. It is very necessary for enthusiasts who use 51 series single-chip microcomputers to master the use of this software. If you use C language programming, then Keil is almost your only choice (currently you can only buy this software in China, and the emulator you buy is likely to only support this software). Even if you do not use C language but only use assembly language for programming, its convenient and easy-to-use integrated environment and powerful software simulation and debugging tools will make you get twice the result with half the effort.
We will learn how to use Keil software through some examples. In this part, we will learn how to input source code, create a project, set up the project in detail, and how to convert source code into target code.
The 89C51 microcontroller is used as the main chip. This microcontroller belongs to the MCS-51 series. It has a 4K FLASH ROM inside, which can be repeatedly erased and written, making it very suitable for experiments. The P1 pin of the 89C51 is connected to 8 LEDs, and the P3.2~P3.4 pins are connected to 4 button switches. Our first task is to make the LEDs connected to the P1 pin light up in sequence.
Figure 1 Simple keyboard and display panel
1. Establishment of Keil Project
First, start the integrated development environment of Keil software. It is assumed here that the reader has correctly installed the software. You can directly double-click the uVision icon on the desktop to start the software.
After UVison is started, there is a project management window on the left side of the program window. This window has 3 tabs, namely Files, Regs, and Books. These three tabs respectively display the file structure of the current project, the values of the CPU registers and some special function registers (only appear during debugging) and the additional description files of the selected CPU. If it is the first time to start Keil, then these three tabs are all empty.
1. Establishment of source file
Use the menu "File->New" or click the new file button on the toolbar to open a new text editing window on the right side of the project window. Enter the following assembly language source program in this window, Example 1:
MOV A,#0FEHMAIN: MOV P1,A RL A LCALL DELAY AJMP MAINDELAY: MOV R7,#255D1: MOV R6,#255DJNZ R6,$ END DJNZ R7,D1RET
Save the file, and note that you must add an extension (the assembly language source program generally uses asm or a51 as the extension). Here, it is assumed that the file is saved as exam1.asm. It should be noted that the source file is a general text file, which does not necessarily need to be written using Keil software. It can be written using any text editor. Moreover, Keil's editor does not support Chinese characters well, so it is recommended to use editing software such as UltraEdit to input the source program.
2. Create project files
In project development, it is not enough to have only a source program. You also need to select a CPU for the project (Keil supports hundreds of CPUs, and the characteristics of these CPUs are not exactly the same), determine the compilation, assembly, and connection parameters, specify the debugging method, and some projects will consist of multiple files. For ease of management and use, Keil uses the concept of project to add these parameter settings and all required files to a project. You can only compile (assemble) and connect a project, not a single source program. Let's build a project step by step.
Click the "Project->New Project..." menu, and a dialog box will appear, asking you to name the project to be created. You can enter a name in the editing box (set to exam1), and no extension is required. Click the "Save" button, and the second dialog box will appear, as shown in Figure 2. This dialog box requires you to select the target CPU (that is, the model of the chip you use). Keil supports many CPUs, and we choose Atmel's 89C51 chip. Click the "+" sign in front of ATMEL to expand the layer, click 89C51, and then click the "OK" button to return to the main interface. At this time, in the file page of the project window, "Target1" appears with a "+" sign in front. Click the "+" sign to expand, and you can see the "Source Group1" of the next layer. At this time, the project is still an empty project, and there are no files in it. You need to manually add the source program you just wrote. Click "Source Group1" to highlight it, and then right-click the mouse to display a drop-down menu, as shown in Figure 3. Select "Add file to Group" Source Group1", a dialog box will appear, asking you to find the source file. Note that the "File Type" below the dialog box defaults to C source file (*.c), that is, the file with C extension, and our file has asm extension, so exam1.asm cannot be found in the list box. To change the file type, click the drop-down list after "File Type" in the dialog box, find and select "Asm Source File (*.a51,*.asm)", then you can find the exam1.asm file in the list box.
Double-click the exam1.asm file to add it to the project. Note that after the file is added to the project, the dialog box does not disappear, waiting for other files to be added. However, beginners often mistakenly think that the operation is unsuccessful and double-click the same file again. At this time, a dialog box as shown in Figure 4 will appear, prompting you that the selected file is already in the list. At this time, you should click "OK" to return to the previous dialog box, and then click "Close" to return to the main interface. After returning, click the plus sign in front of "SourceGroup 1" and you will find that the exam1.asm file is already in it. Double-click the file name to open the source program.
Figure 2 Selecting the target CPU
Figure 3 Adding files
Figure 4 Error of adding files repeatedly
2. Detailed settings of the project
After the project is established, it is necessary to further set up the project to meet the requirements.
Figure 5 Setting the target
First, click Target 1 in the Project window on the left, and then use the menu "Project->Option for target 'target1'" to display the dialog box for project settings. This dialog box is very complicated, with a total of 8 pages. It is not easy to understand all of them. Fortunately, most of the settings can be set to the default values. Set the Target page in the dialog box, as shown in Figure 5. The value after Xtal is the crystal frequency value. The default value is the highest available frequency value of the selected target CPU, which is 24M for the AT89C51 we selected. This value has nothing to do with the final target code generated, and is only used to display the program execution time during software simulation debugging. Correctly setting this value can make the displayed time consistent with the actual time used. Generally, it is set to the same crystal frequency used by your hardware. If you don't need to know the execution time of the program, you can also not set it. Here it is set to 12.
Memory Model is used to set the RAM usage. There are three options: Small means that all variables are in the internal RAM of the microcontroller; Compact means that one page of external expansion RAM can be used, and Large means that all external expansion RAM can be used. Code Model is used to set the use of ROM space. There are also three options: Small mode, which uses less than 2K program space; Compact mode, the code size of a single function cannot exceed 2K, and the entire program can use 64K program space; Large mode, all 64K space can be used. Use on-chip ROM option, confirm whether to use only on-chip ROM (note: selecting this option does not affect the amount of target code generated in the end); Operating option is the operating system selection. Keil provides two operating systems: Rtx tiny and Rtx full. The operating system is another big topic. Usually we do not use any operating system, that is, use the default value of this option: None (no operating system); Off Chip Code memory is used to determine the address range of the system extended ROM, and Off Chip xData memory group is used to determine the address range of the system extended RAM. These options must be determined according to the hardware used. Since this example is a single-chip application and no expansion is performed, none of them are reselected and set according to the default value.
The OutPut page in the Settings dialog box is shown in Figure 6. There are also multiple options here. Among them, Create Hex file is used to generate an executable code file (a HEX format file that can be written into the microcontroller chip by a programmer, and the file extension is .HEX). By default, this option is not selected. If you want to write a chip for hardware experiments, you must select this option. This is something that beginners are prone to neglect, so special attention is here.
Figure 6 Controlling the output
Selecting Debug information will generate debugging information, which is used for debugging. If you need to debug the program, you should select this option. Browse information generates browsing information, which can be viewed using the menu view->Browse. The default value is used here.
The button "Select Folder for objects" is used to select the folder where the final target file is located. By default, it is in the same folder as the project file. Name of Executable is used to specify the name of the final generated target file. By default, it is the same as the name of the project. These two items generally do not need to be changed. The other pages in the project settings dialog box are related to the usage of C51 compilation options, A51 assembly options, BL51 connector connection options, etc. Here, the default values are taken and no changes are made. The following is only a brief introduction to the commonly used options in some related pages.
The Listing tab is used to adjust the generated list file options. After assembly or compilation, a (*.lst) list file will be generated. After linking, a (*.m51) list file will also be generated. This page is used to make detailed adjustments to the content and format of the list file. The more commonly used option is the "Assamble Code" item under "C Compile Listing". Selecting this item can generate the assembly code corresponding to the C language source program in the list file.
The C51 tab is used to control the compilation process of Keil's C51 compiler. The most commonly used one is the "Code Optimization" group, as shown in Figure 7. In this group, Level is the optimization level. When C51 compiles the source program, it can optimize the code up to 9 levels. The default level is 8, which generally does not need to be modified. If there are some problems in the compilation, you can try to lower the optimization level. Emphasis is to select the compilation priority mode. The first item is code quantity optimization (the final generated code quantity is small); the second item is speed priority (the final generated code speed is fast); the third item is default. The default is speed priority, which can be changed as needed. After the setting is completed, press Confirm to return to the main interface, and the project file is established and set.
Figure 7 Code generation control
3. Compile and connect
After setting up the project, you can compile and connect. Select the menu Project->Build target to connect the current project. If the current file has been modified, the software will compile the file first, and then connect to generate the target code; if you select Rebuild All target files, all files in the current project will be recompiled and then connected to ensure that the final target code is the latest, and the Translate... item will only compile the file without connecting. The above operations can also be performed directly through the toolbar buttons. Figure 8 is the toolbar buttons related to compilation and settings, from left to right: Compile, Compile and Connect, Rebuild All, Stop Compilation and Set the Project.
The information during the compilation process will appear in the Build page of the output window. If there is a syntax error in the source program, an error report will appear. Double-click the line to locate the error. After repeatedly modifying the source program, you will eventually get the result shown in Figure 9, which indicates that a file named exam1.hex has been obtained. This file can be read by the programmer and written to the chip. At the same time, some other related files are also generated, which can be used for Keil simulation and debugging. At this time, you can proceed to the next step of debugging.
Figure 9 The result after correct compilation and linking
Previous article:MCU independent buttons and matrix buttons
Next article:Keil's debugging commands, online assembly and breakpoint settings
Recommended ReadingLatest update time:2024-11-16 13:49
- Popular Resources
- Popular amplifiers
- MCU C language programming and Proteus simulation technology (Xu Aijun)
- Single-chip microcomputer C language programming and simulation
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- Single chip microcomputer control technology (Li Shuping, Wang Yan, Zhu Yu, Zhang Xiaoyun)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- [TI recommended course] #PFC power supply design and inductor design calculation#
- [Distributed temperature and humidity acquisition system] + STM32H745I-LWIP program
- Summary: TI LAUNCHXL-CC2650 evaluation report
- [LAUNCHXL-CC2650] Run the SDK built-in routine
- What is GPIO?
- [Liquid Level Sensor Evaluation] LED Liquid Level Indicator
- Power module source knowledge you will understand after reading it - leakage inductance
- Intelligent Temperature Controller Based on FPGA
- Evaluation report summary: New version of Zhongke Bluexun AB32VG1 RISC-V development board
- How to understand these four statements in STM32F103RCT6? The return statement should be declared before the main function, right?