Recently done a project, asked to write a script file to compile the SCM source files. At that time, I was wondering, the Keil platform to write a single-chip source program is not strong enough, why write a bat script alone to compile the source program??? After the great God consulted, the original great God wrote a smart home control program, debugging for different appliances are required to modify different macro definitions (for any home appliances control with a macro definition to represent), so every time you debug to get into the Keil compiler, is a waste of time, Again, because sometimes the change does not have to open the IED in the case, do not want to open Keil every time to compile. Therefore, it is necessary to write a script file to achieve the compilation of the source program, and according to the different input can choose different macro definitions to compile, so do not need to run Keil every time.
The Write bat script has to understand its command statement first. Because the bat script file is a series of DOS commands in a certain order of the collection as an executable text file, the text file can complete the specified function as required, its extension is bat or cmd. (Tip: You can forcibly terminate the execution of a batch by pressing the CTRL + C key combination.) ), so below I only list the commands I use to explain:
Common commands:
1.REM and:: 2.ECHO and @ 3.PAUSE 4.GOTO and: 5.if...else ... 6.ERRORLEVEL 7.SET 8.FOR 9.%
1.REM and::
REM is a comment command that is typically used to annotate a program, and the content behind the command is not executed but can be echoed.
:: It also acts as a comment, and is more concise and effective, unlike REM: The post-lines character is not echoed at execution time, regardless of whether you open the command-line echo state with Echo on.
2.ECHO and @
The @ character closes the command echo before it is placed in the command, regardless of whether ECHO is open at this time. The common functions of the echo command are as follows:
(1) Turn on echo or turn off echo feature
Format: Echo [{On|off}]
If you want to turn off the display of the ECHO off/on itself, add "@" to the command line.
(2) Output prompt information
Format: ECHO message content
(3) Output a blank line, which is equivalent to output a carriage return
Format: ECHO.
Note that the "." In the command line To follow Echo, there must be no spaces in the middle.
(4) Creation of new documents or additions to documents
Format: ECHO file contents > file name
ECHO file contents >> file name
where >, >> redirects the symbol, indicating that the content is output to the specified file.
3.PAUSE
Pause command, run this command will display "Press any key to continue", press any key after the suspension state, such as to display other prompts, can be used with ECHO, for example: Echo Other tips & Pause > nul
4.GOTO and:
Goto is a jump instruction, jump to: The beginning of the label, if the tag match, then execute the label after the command, the name of the label can be random, but preferably a meaningful string, goto statement is based on: and tags to find the next run position.
5.if...else ...
Conditional statement, according to the judgment value to execute the relevant command, mainly in the following form:
IF [NOT] ERRORLEVEL number command
IF [NOT] string1==string2 command
IF [NOT] EXIST filename command
Where command commands can be a combination of multiple commands, you can also nest use conditions or loop commands in a combo command.
6.ERRORLEVEL
The command executes the return value, which is used to determine the command execution state, the default value is 0, the General Command execution error, or the warning causes the ERRORLEVEL value to be 1, 2, 3, depending on the level at which the error occurred.
7.SET
Display, set, or delete environment variables in the following format:
SET [variable=[string]]
variable Specify environment variables
string Specifies a series of string
8.FOR
Loop commands to assign to environment variables in the basic format:
for%%variable in (set) do command [command-parameters]
%%variable Specify a single letter representing the parameter to be replaced
set Specify one or more files, you can use wildcards
command Specify the commands to execute on each file
command-parameters Specify parameters or command-line switches
for loop with parameters there are many uses, you can use the "for/? > file Name" Statement outputs the for help document to the specified file for easy viewing.
9.%
The batch variable bootstrapper, which can be used as a reference parameter for the batch process. Reference variables are used with%var%, calling program external parameters with% 1 to%9, and so on.
%0 The batch file itself, including the full path and extension
The first parameter of%1
%9 nineth parameter
%* all parameters starting from the first argument
The parameter%0 can be used to invoke the special functions of the batch itself, in order to achieve the purpose of the batch itself loop, or to copy the file itself and so on.
The source file process for compiling a microcontroller with the command line is to compile the source file to generate obj, then link obj, and then convert to a hex file. This will be OK, very simple.
51.exe compile C file, BL51. EXE link obj, OH51. EXE to convert the file into hex.
installation directory \c51.exe c51test.c
installation directory \bl51.exe c51test.obj to Test
installation directory \oh51.exe Test
Note: Don't forget to copy your head files to the \c51\inc Library directory, which is handy for later use.
To run the C51 compiler and tools in the bat file, you must manually create the following environment variables:
set path= installation directory \c51\bin;%path%
SET dir= source program directory ...
set c51inc= installation directory \c51\inc
set c51lib= installation directory \c51\lib
C51 has a lot of control commands, which allows you to select a macro definition:
The C51 compiler provides a number of control commands to control compilation, a control command consisting of one or more letters or numbers, after the file name to be compiled, Control commands are divided into three categories: source file control, target control, and list control. Detailed control commands and their descriptions can be found in the Help documentation for the installation directory \c51\hlp\c51.chm (or c51.pdf). The description of the Define,debug,incdir,warninglevel control command is as follows:
DEFINE
Abbreviation: DF
Default value: None
Parameters: One or more names that conform to the C language conventions, separated by commas.
Description: Defines a preprocessing flag on the compile command line.
DEBUG
Abbreviation: DB
Default value: No debug information is generated
Parameters: None
Description: Instructs the compiler to include debugging information in the obj file.
Incdir
Abbreviation: None
Default value: None
Parameters: The path to the files included in the include in the source file
Description: Specifies the location of the files included in the compiler include, and the compiler receives the most512 path declarations, if multiple path declarations are specified, the path names must be separated by semicolons.
WarningLevel
Abbreviation: WL
Default value: WarningLevel (2)
Parameter: A value between 0-2
Description: Some compiler warnings are ignored by specifying the level of warning.
The concept is all explained, then not much to say, directly look at bat's source program as follows, the program can be extended:
:: Close Echo@echo off::clean screencls:: SET c51inc=c:\keil\c51\inc\atmel\; C:\Keil\C51\INC:: Set C51lib=c:\keil\c51\libset path for path set C51lib=lib of Binpath=bin:: Set source program Directory and SOURCE filenameset dirname=bat File directory set Status=0set sourcefile=%~nx1set objname=%~n1::set the macro defined constant Valuesset valueone= "0" set valuetwo= "1" Set valuethree= "P0^0":: Set the macro defined Aliasset Aliasone=aset Aliastwo=bset Aliasthree=led::d ispaly This programme are to do the C51 programme Automateecho this programme are to do the C51 program Me automate::if input not correctif "%1" = = "" Goto inputerrif "%1" = = "/?" goto Inputerrif "%1" = = "Help" goto Inputerrif not " %~x1 "= =". C "goto inputerrif"%2 "= =" "(goto inputerrtwo) Else if not"%2 "= =" 0 "(If not"%2 "= =" 1 "(goto inputerrtwo)) if "%3" = "" "(Goto inputerrtwo) Else if not"%3 "= =" 0 "(If not"%3 "= =" 1 "(goto inputerrtwo)) if"%4 "= =" "(Goto Inputerrtwo) else if not "%4" = = "0" (If not "%4" = = "1" (goto inputerrtwo)) @echo Go to workplaced:cd%dirname% @echo Clean has existed filegoto clean:valuejudgeif "%2" = = "1" (if "%3" = = "1" (if "%4" = = "1" (Goto Defineone) Else g Oto definetwo) Else (if "%4" = = "1" (goto definethree) Else goto definefour)) Else if "%3" = = "1" (if "%4" = = "1" (Goto Definefi ve) Else Goto definesix) Else (if "%4" = = "1" (goto Defineseven) Else goto defineeight): Linkhex::else link the. obj file%bin Path%bl51.exe%c51lib%c51s.lib,%objname%.obj to%objname%if errorlevel 1 goto linkerr@echo Link success...::generate. Hex file%binpath%oh51.exe%objname%if errorlevel 1 goto hexerr@echo HEX Generate success...set status=1goto Judge:defineo Ne@echo building%objname%.c to%objname%.obj::build c51 programmer%binpath%c51.exe%sourcefile% DF (%aliasone%= '% Valueone% ',%aliastwo%= '%valuetwo% ',%aliasthree%= '%valuethree% ') DB incdir (External library path) WL (1):: If error, pause to see Errorif ERRORLEVEL 1 goto builderr@echo Building Success ... @echo. @echo macro define Sequence:111goto Linkhex:definetwo@echo Buil Ding%objname%.c to%objname%.obj::builD c51 programmer%binpath%c51.exe%sourcefile% DF (%aliasone%= '%valueone% ',%aliastwo%= '%valuetwo% ') DB INCDIR (External library path) WL (1):: If error, pause to see Errorif errorlevel 1 goto builderr@echo Building Success ... @echo. @echo macro Define sequence: 110goto Linkhex:definethree@echo Building%objname%.c to%objname%.obj::build c51 programmer%binpath%c51.exe% Sourcefile% DF (%aliasone%= '%valueone% ',%aliasthree%= '%valuethree% ') DB incdir (External library path) WL (1):: If error, pause to see Erro RIF errorlevel 1 goto builderr@echo Building Success ... @echo. @echo macro Define Sequence:101goto Linkhex:definefour@echo Building%objname%.c to%objname%.obj::build c51 programmer%binpath%c51.exe%sourcefile% DF (%aliasone%= '%valueone% ') DB Incdir (External library path) WL (1):: If error, pause to see Errorif errorlevel 1 goto builderr@echo Building Success ... @echo. @echo MA Cro define Sequence:100goto Linkhex:definefive@echo building%objname%.c to%objname%.obj::build c51 Programmer%binpath %c51.exe%sourcefile% DF (%aliastwo%= '%valuetwo% ',%aliasthree%= '%valuethree% ') DB incdir (External library path) WL (1):: If error, pause to see Errorif errorlevel 1 goto Builderr@ech O Building Success ... @echo. @echo macro define Sequence:011goto Linkhex:definesix@echo building%objname%.c to%objname%. Obj::build c51 programmer%binpath%c51.exe%sourcefile% DF (%aliastwo%= '%valuetwo% ') DB incdir (External library path) WL (1):: If error, PA Use-see Errorif errorlevel 1 goto builderr@echo Building Success ... @echo. @echo macro Define Sequence:010goto Linkhex:d Efineseven@echo building%objname%.c to%objname%.obj::build c51 programmer%binpath%c51.exe%sourcefile% DF (% aliasthree%= '%valuethree% ') DB incdir (External library path) WL (1):: If error, pause to see Errorif errorlevel 1 goto builderr@echo Build ing Success @echo. @echo macro define Sequence:001goto Linkhex:defineeight@echo building%objname%.c to%objname%.obj: : Build c51 programmer%binpath%c51.exe%sourcefile% DB incdir (External library path) WL (1):: If error, pause to see Errorif errorlevel 1 g Oto Builderr@echo Building Success...@eCho @echo macro define Sequence:000goto linkhex:cleanif exist "%objname%.lst" del "%objname%.lst" if exist "%objname%.m51 "Del"%objname%.m51 "if Exist"%objname%.obj "del"%objname%.obj "if Exist"%objname% "del"%objname% "if%status% = = 0 ( If exist "%objname%.hex" del "%objname%.hex"::D Elay,unit is S ping-n 3 127.0.0.0>nul goto valuejudge): Judgeif% status% = = 1 goto hexok@echo Hex generate Fail!pausegoto Returndir:builderr@echo sorry!%o Bjname%.obj Generate Fail...pausegoto Returndir:linkerr@echo sorry! Link Fail...pausegoto Returndir:hexerr@echo sorry! HEX Generate Fail...pausegoto Returndir:inputerrecho Please enter correct C51 file Nameecho usage:[bat file name] [C51 fil e name].c [value1 (0 or 1)] [value2 (0 or 1)] [Value3 (0 or 1)]pausegoto Returndir:inputerrtwoecho Please enter correct C51 D Efine valueecho usage:[bat file name] [C51 file name].c [value1 (0 or 1)] [value2 (0 or 1)] [Value3 (0 or 1)]pausegoto Return Dir:hexok@echo. @echo Ok!%o Bjname%.hex have Generated...pausegoto Returndir:returndirc: @echo on
To run the bat file, enter the command format:
BAT filename source program name. c [1 or 0] [1 or 0] [1 or 0]
%0 %%%3%4
%0 is the bat file itself and%1 is the source file to be compiled, with the suffix. C must not be less,% 2,% 3,%4 to select a macro-defined statement, only 0 or 1,0 indicates that the corresponding macro definition statement is not required, and 1 means that the corresponding macro definition statement is selected. The aliases and constants of a macro definition statement are set through the SET statement:
SET varone= "0"; SET vartwo= "1"; SET varthree= "P0^0"
SET aliasone=a; SET aliastwo=b; SET aliasthree=led
If you want to modify the aliases and constants in the macro definition, simply modify the characters to the right of the equals sign in the set.
Resources:
1.http://www.cnblogs.com/glaivelee/archive/2009/10/07/1578737.html
2.http://www.cnblogs.com/gleam/archive/2012/02/14/2350990.html
Personal achievements, reproduced please specify the source!!!
BAT script to compile the single-chip microcomputer program