KEIL MDK本身并没有输出BIN文件的功能,在output栏中只能输出HEX文件:

RealView MDK生成BIN文件的方法_嵌入式

我们想要生成BIN文件的话需要使用小工具转换,但是每次编译完之后要手动调用一次转换挺麻烦,我们可以使用MDK的脚本执行来实现,打开User栏:

RealView MDK生成BIN文件的方法_嵌入式_02

可以看到有三个框,分别是Run User Program Before Compilation of a C/C++ File、Run User Program Before Build/Rebuild、Run User Program After Build/Rebuild。我们需要在Build完成之后进行HEX转BIN的操作,所以我们再第三个框中操作。下载一个hex2bin工具,这是一个命令行工具,不能是Windows GUI程序:

RealView MDK生成BIN文件的方法_嵌入式_03

然后在Run User Program After Build/Rebuild中勾选Run #1并填写“hex2bin.exe ..\obj\Main.hex”,表示Build完成之后执行这条指令。

RealView MDK生成BIN文件的方法_嵌入式_04

测试一下效果:

RealView MDK生成BIN文件的方法_嵌入式_05

在obj文件夹中就有了Main.bin文件了。

说明一下我下载的这个hex2bin.exe工具跟其他的工具可能有差别,这个工具生成的bin文件是从有效的地址开始的,例如我的程序是从0x08002800开始的,其他的工具生成的bin文件是从0x08000000开始的,0x08000000到0x08002800中间填充0x00。而我下载的工具是直接从0x08002800开始的。我不知道其他的工具是怎么样的,反正我遇到的两个工具是不一样的。无所谓,HEX文件的格式很简单,大不了我自己写一个HEX转BIN的工具。

 

另外我发现一个更方便的方式将KEIL生成的HEX文件转成bin文件,使用的是KEIL自带的工具,在KEIL的安装目录下:E:\Keil\ARM\ARMCC\bin\fromelf.exe,这个文件就是自带的转换工具,如何使用呢,我们可以使用命令行执行看看工具的帮助说明:

C:\Users\tangquan>E:\Keil\ARM\ARMCC\bin/fromelf.exe --help
ARM FromELF, 5.03 [Build 24] [MDK-ARM Standard]

ARM image conversion utility
fromelf [options] input_file

Options:
       --help         display this help screen
       --vsn          display version information
       --output file  the output file. (defaults to stdout for -text format)
       --nodebug      do not put debug areas in the output image
       --nolinkview   do not put sections in the output image

Binary Output Formats:
       --bin          Plain Binary
       --m32          Motorola 32 bit Hex
       --i32          Intel 32 bit Hex
       --vhx          Byte Oriented Hex format

       --base addr    Optionally set base address for m32,i32

Output Formats Requiring Debug Information
       --fieldoffsets Assembly Language Description of Structures/Classes
       --expandarrays Arrays inside and outside structures are expanded

Other Output Formats:
       --elf         ELF
       --text        Text Information

                Flags for Text Information
                -v          verbose
                -a          print data addresses (For images built with debug)
                -c          disassemble code
                -d          print contents of data section
                -e          print exception tables
                -g          print debug tables
                -r          print relocation information
                -s          print symbol table
                -t          print string table
                -y          print dynamic segment contents
                -z          print code and data size information

Software supplied by: ARM Limited

可以看到这个工具的功能十分强大,除了可以将HEX转成BIN文件,还能将BIN文件转成HEX文件,还有其它格式可以选择。我们要转换成BIN文件可以这样写命令:

E:\Keil\ARM\ARMCC\bin\fromelf.exe --bin -o ../OBJ/test.bin ../OBJ/STM32F030Demo.axf

把这个命令替换上面的Run User Program After Build/Rebuild中的命令即可。编译完成之后就会生产test.bin和STM32F030Demo.axf文件。这个自带的转换文件转换得到的BIN文件和上面的hex2bin.exe工具文件转换出来的BIN文件的内容是一样的,直接从程序开始的地方开始的。