PICl8F series products are high-end products of Microchip Technology Co., Ltd. of the United States. Users mostly use C language for programming and design. PICC18 compiler of HI-TECH Software Pty. Ltd. has been widely used. Most users use MPLAB integrated environment for programming (see references). There are some problems in the development of some important or large applications. For example, how to write C language library files, how to compile programs with code restrictions, etc. If only traditional programming is used, the desired effect cannot be achieved. The PICC18 command line driver of HI-TECH C compiler described in this article is to solve these problems.
1 HI-TECH C compiler
HI-TECH C compiler provides the standard features of C language, and also adds many extended functions designed to adapt to the specific hardware features of PIC series microcontrollers, and integrates with assembler to provide shared linker and library management tools, so it is widely used.
When compiling a program, there are many operations that can be completed independently by command line driver CLD (command-line driver) or HPD integrated environment driver (now HI-TIDE). In either case, HPD or CLD (menu options under HPD, or command line of CLD) uses the options specified by the programmer to determine which internal application needs to be executed and
what options need to be sent to each application.
C language source programs and assembly language programs provided by the user that are suitable for the target system can be operated by CLD or HPD. When using CLD, use any command of the command line to select the operation mode; when using HPD, type the required files in the Source File List dialog box.
Commonly used source programs can be compiled into a library file, so that the files can be processed more conveniently and accessed more quickly by the compiler. The compiler can directly receive library files like other source files and use the .lib extension to specify the type of file, so library files must be named in this way. When using CLD, use any command of the command line to select the operation mode; or when using HPD, type the required library files in the "Source File List..." dialog box.
In fact, when using the MPLAB IDE integrated environment for development, CLD is used, but it is not noticed in ordinary applications. The following is the result of compiling a C source file in MPLAB IDE:
From the above results, it can be seen that the compiler uses the CLD command, and the two can be unified. Only after understanding CLD can you have a deeper understanding and mastery of the HI-TECHC compiler.
2 PICC18 command line driver
PICC18 is a driver that calls the DOS command line to compile and/or link C language programs. The basic command format of PICC18 is:
PICC18 [option] files [file library]
is usually identified by a dash "——" before the file name, but this is not necessary. Files can be a mixture of source files (C or assembler) and object files. The file library is a list of library names, or an L option source file. Object files and library files are distinguished by PICC18 through file types or extensions, and the types are listed in Table 1.
PICC18 will check each file and complete the appropriate operation. C files will be compiled and assembly files will be assembled. Finally, all object files obtained by compilation or assembly, or explicitly listed on the command line, are linked with the standard runtime code library and any user-specified libraries, unless they are disabled by some option. Only when the source code is referenced, can the library functions be linked into the resulting output file. Using PICC18 as a program with only object files (i.e. no source files), which means that only the linking stage is performed, is a typical usage. Compile several home-written source files as object files using the C option of PICC18, and then call PICC18 with only the generated object files and the appropriate libraries (with appropriate options) to create the final program.
2.1 Long command lines
The PICC18 driver is a 32-bit Windows software that can handle command lines longer than 128 characters. The driver can be called from a DOS batch file, or options can be passed through a command file. When using a batch file, the command line sent to PICC18 must be listed on a single line. In the command file, driver options can be passed on multiple lines, which is achieved by separating the lines with a space bar followed by a backslash "\" and then a carriage return. For example, a command file can contain:
Since no command line instructions are provided, PICC18 will read xyz. cmd to get its command line instructions. The command file can also be read out through the @ symbol.
For example: PICC18@xyz. cmd
2.2 PICC18 compiler options
The compiler settings are mainly based on the generation of ROM code. Table 2 lists several commonly used important options for the PICC18 compiler.
① Define the processor type: -processor.
This option determines which processor is used. For example, use the command line -18c452 to compile PICl8C452. Users can also add their own processors to the compiler. [page]
② Specify ROM offset: -Aaddress.
The -A option is used to specify the base address for the ROM image area. If this option is required to boot such a debugging tool, the debugging tool expects the ROM mapping address to start at a non-zero address. This option affects all ROM-based parts, including reset and interrupt vectors, as well as linker classes that place code and constant data. If the base address is specified to external memory, the -ROM option must be used to specify the available address range of the external memory.
③Optimizer and optimization priority options: -O and -Zg.
-O means calling the optimizer of PICC18, and the -Zg option calls the global priority in the code generation path. This is of great significance for reducing code length and the use of internal RAM. This priority is not as harsh as the transfer priority, but it is very meaningful for reducing code length. Based on an execution function, the global priority can optimize the use of registers. It also uses the most common transfers in the code to avoid unnecessary memory access.
The default level of this option is 1 (lowest priority). The priority can be set to any level from 1 to 9 (9 is the highest priority). The number indicates the degree to which the priority attempts to reduce the code. For PICC18, priorities higher than 3 are usually useless.
After understanding the command line driver, you will have a deeper understanding of the example of compiling C source files in MPLAB IDE in Section 1.
3 Command line driver application
The following introduces some examples of command line drivers in practical applications. Through these examples, you can have a preliminary understanding of the application of command line drivers, and on this basis, you can conduct in-depth study and application of the PICC18 compiler.
3.1 Command line driver compiles source files
In Win2000, perform the following operations: execute the "Start" → "Run" command, and then enter "CMD"; or execute the "Start" → "Programs" → "Accessories" → "Command Prompt" command, and the cmd.exe interface will pop up, as shown in Figure 1. If the current directory in cmd.exe is not in the root directory of the C drive, you need to enter the root directory of the C drive by entering the command "cd c:\". Enter the "cd htsoft\picl8\bin" command to enter the directory where the PICC18.exe software is located (the directory when it is installed by default), as shown in Figure 2.
Input "PICC18-18f458 lzqmwyz.c", which means to use PICC18.exe compiler to compile lzqmwyz.c source file, and the microcontroller model is
18f458 (i.e. PIC18F458 microcontroller). At this time, the required compilation result will be output. In the generated file, lzqmwyz.hex with hex extension is more important. If there is a syntax error in the source program, the error message and the line where it is located will also be displayed.
Note: When operating in the cmd.exe interface, all source files are stored in the same directory of PICC18.exe (the default installation directory of PICC18.exe is c:\HTSOFT\PIC18\Bin). In this article, PICC18 uses this default installation directory. Here lzqmwyz.c is stored in c:\HTSOFT\PIC18\Bin.
(2) -noerrata option
The "-noerrata" option does not enable error correction of the output code, while by default, the compiler's output code will be modified. Some chips do not have a bug table and will not be modified by the compiler. This option can be used by the compiler to prohibit any bug table position. When the bug table changes, it will be safely prohibited. For some chips (such as PIC18F458, etc.), PICC18 limits the code length of the program (generally limited to within 0x4000, that is, within 16KB). This is the reason why some netizens in the PICl8 application forum mentioned above have limited code. Some programmers have written the following prompt program in the program:
If the compiled program code exceeds this range, an error message will be listed.
Figure 3 is the prompt message that appears when the code exceeds the length of Ox4000 when compiling the solar050408.c source file.
When using the -noerrata option, solar050408. c source file, the correct compilation result will be obtained. The command is "PICC18-18f458solar050408.co-zgl-noerrata". In the command, "-o" means calling the optimizer of PICC18, and "-zg1" means that the priority used when calling the optimizer is level 1.
3.2 Library file generation and application
3.2.1 C source file
(1) lzqmwyzl.c source file containing the main function
(2) funcx3.c source file containing the function_X3 function [page]
3.2.2 Generate library file
Use the two files in 3.2.1 to directly create an application, which is often used in daily applications. Here we introduce the method of generating a library file from the funcx3. c source file and then calling it from lzqmwyz. c, that is, making a library file yourself for use.
(1) Generate obj file
Store the funcx3. c source file in the same directory as PICC18. exe, and then in cmd. Enter "PICC18-18f458-C funcx3.c" in cmd.exe. The -C option is to compile several source files into target files (.obj files). Here, the source file of funcx3.c is compiled into the funcx3.obj file, and then the funcx3.obj file can be found in the c:\HTSOFT\PICl8\Bin directory.
(2) Generate lib library file
Enter "libr r fun.lib runcx3.obj" in cmd.exe. The r option is the replacement module. fun.lib is the file name of the library file made by the function funcx3.c, as shown in Figure 4.
3.2.3 Library file application
Figure 5 is a project using the lzqmwyzl.c source file of 3.2.1 and calling the library file fun.1ib made in 3.2.2. At this time, only the lzqmwyzl.c source file and fun. 1ib file, and no longer need to use the funcx3.c source file containing the runetton_X3 function.
Conclusion
The PICC18 compiler command line driver has many applications in practice. Only by understanding it can we have a deeper understanding of the C compiler. Due to the limited space, this article only serves as a starting point and provides a way of thinking for the majority of programmers to refer to; at the same time, it also has corresponding reference value for PICC.
Previous article:Design of Programmable Power Supply Based on PIC Microcontroller
Next article:MCU interrupt C language usage
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
- 【Recruitment】RF Engineer
- November 23 is Fibonacci Day
- I want to know what this module means
- There are endless packaging technologies, which one is the most suitable?
- [Domestic RISC-V Linux Board Fang·Starlight VisionFive Trial Report] Evaluation Summary
- Mastering LED lighting
- An article to help you plan your C language learning journey from entry to advanced
- Xunwei 3399 development board uses ADB commands to transfer files to Android devices
- SensorTile.Box Unboxing 2——What's in the TF card
- How to use stm to make lcd12864 display