生成HEX文件
- 格式:doc
- 大小:40.50 KB
- 文档页数:7
Keil的使用方法打开keil程序,在Project菜单下选择New Project
选择好保存的路径和当前的文件名之后,点击保存
然后会弹出如下窗口
在这里你需要选择芯片的类型,下面以A T89C51为例,选择好之后点击确定
这时会弹出一个窗口,让你选择是否加载“标准启动文件”,初学者不用加载,选择“否”
下面我们要建立一个C或者Ams文件,以C文件为例,点击File菜单下的New,创建一个新的Text1文档,在里面输入需要写的程序,下面以一段代码为例,
选择File菜单下的Save按钮保存之后,会出现以下对话框
输入文件名(由于默认是的保存类型是All File,而我们要的是C类型的文件,所以要输入后缀为.c的文件名)
点击保存,如果操作正确,程序中的关键字会变成蓝色
点击资源菜单下的“+”号
右键Source Group 1,选择Add File to Group ‘Source Group 1’这个选项
然后选中刚才保存的C类型文件,点击Add
然后点击Close,完成文件的添加。
下一步要进行工程设置,选择Project菜单下的Options for Target ‘Target 1’
会出现如下对话框
录进单片机用的。
如下图,然后点击确定
进行编译之后,就能生成一个hex文件了。
IAR A VR怎么生成HEX文件啊IAR同时输出多个文件的说明,在IAR225E(30天限时版)中通过。
操作步骤:1、在Project->Options->XLINK->Include中观察该Project使用了哪个XCL文件。
2、在该XLC文件中加入以下两行:-Ointel-extended,(CODE)=.hex-Ointel-extended,(XDA TA)=.eep3、在Project->Options->XLINK->Output->Format->Other中选择UBROF6格式输出。
以下为MAP文件的部分内容,生成了与Project名称相同的三个文件:test.dbg:兼容于A VR STUDIO的调试文件(也可以任意指定文件名)test.hex:INTEL HEX格式的程序代码文件test.eep:INTEL HEX格式的EEPROM初始化数据文件。
############################################################################## ### ## IAR Universal Linker V4.53F/WIN ## ## Link time = 20/Jan/2001 14:49:35 ## Target CPU = A90 ## List file = "D:\IAR Systems\user_A vr\Debug\List\test.map" ## Output file 1 = "D:\IAR Systems\user_A vr\Debug\Exe\test.dbg" ## Format: ubrof6 ## UBROF version 6.0.0 ## Output file 2 = "D:\IAR Systems\user_A vr\Debug\Exe\test.hex" ## Format: intel-extended, variant: -y(CODE) ## Output file 3 = "D:\IAR Systems\user_A vr\Debug\Exe\test.eep" ## Format: intel-extended, variant: -y(XDA TA) #注意:1、可以修改IAR Embedded Workbench安装位置中所有cfg?s.xcl cfg?l.xcl cfg?t.xcl文件,这样就不需每次修改了,对Project->Options->XLINK->Output->Format中其余两个选项没有影响。
IAR编译ZStack-CC2530,生成HEX文件完全配置IAR编译ZStack-CC2530为可下载运行的HEX文件的正确配置:1. 正确配置输出文件格式:菜单选择Project-Options-Linker-Output-Format,选择Other。
右边的Output下拉框 选intel-extended,Format variant 下拉框选None,Module-local下拉框选Include all2.还是在菜单Project-Options-Linker-Output标签中,勾上Override default选项,把编辑框中的文件名的后缀改为hex以上两步都是大多数人熟知的,下面这一步是针对大型程序编译下载所必须的,也是大部分写zstack教程的人所没有提到的。
3. 找到f8w2530.xcl文件,并打开。
(这个文件在"Projects/zstack/Tools/CC2530DB/"目录下,也可以通过IAR编 译环境的左侧Workspace窗口点开Tools文件夹看到)在f8w2530.xcl文件中找到两行被注释掉的语句://-M(CODE)[(_CODEBANK_START+_FIRST_BANK_ADDR)-(_CODEBANK_END+_FIRST_B ANK_ADDR)]*///_NR_OF_BANKS+_FIRST_BANK_ADDR=0x8000把这两行前面的"//"去掉,保存,重新编译,OK!(注: 去掉这两行的"//"后在编译输出成hex格式时没有问题,但在debug模式下编译会提示警告:Warning[w69]: Address translation (-M, -b# or -b@) has no effect on the output format 'debug'. The output file will be generated but noaddress translation will be performed. 不过并不会影响debug调试的使用。
1. 关于hex文件的产生方法:在STVD下如何产生HEX文件,在STVD的Project -> Setting -> Post-Build Commands 栏内添加: chex -fi -o $(OutputPath)$(T argetSName).hex $(OutputPath)$(TargetSName).sm82. 将memory models选择mods或modsl应用注意:使用的是STVD自动生成的stm8_interrupt_vector.c文件时,在使用modsl模式编译时,必须将extern void _stext(); /* startup routine */ 这句改为extern @near void _stext(); /* startup routine */ 需要增加@near 关键字。
程序大于32k后,要把const数据,main还有中断函数等定位到32k(地址64k)以内,不然程序会跑飞,而且编译链接的时候也不会报错。
3. 仿真调试利用STM8小板上的3个LED灯,了解仿真环境。
利用建好的环境,修改main.c文件,修改后的如下#i nclude "STM8S207R.h"void main(void){_asm("sim");PD_DDR |= 0x0D;PD_CR1 |= 0x0D;PD_CR2 = 0x00;_asm("nop");PD_ODR |=0x01;_asm("nop");PD_ODR |=0x04;_asm("nop");PD_ODR |=0x08;_asm("nop");PD_ODR &=~0x0D;_asm("nop");_asm("rim");while(1);}把 STM8S207R.h 文件放在建好的工程目录下,与main.c文件在用一个目录下。
在keil中编写完程序后,要将程序通过烧录器烧写到单片机中,这时就需要在keil中产生一个hex格式的文件,只有通过这种格式,烧录器才能将程序烧写到单片机中。
hex文件格式是Intel公司提出的按地址排列的数据信息,数据宽度为字节,所有数据使用16进制数字表示, 常用来保存单片机或其他处理器的目标程序代码。
它保存物理程序存储区中的目标代码映象。
一般的编程器都支持这种格式。
如何才能产生这种格式的文件呢?
在keil中对其进行如下设置,便能产生hex文件:
点击菜单“Option for Tagret '...'” ---> 选择“Output”页--->钩选“Create HEX File”--->重编译
如果程序无误,在Project的当前目录即会产生HEX 文件。
生成HEX文件的MATLAB程序ROM初始化文件既可以是*.mif也可以是*.hex文件,但是如果工程需要在Modelsim中进行仿真的话,则必须生成*.hex文件对ROM进行初始化。
一般简单且有规律的初始化数据可以用Excel先生成mif文件再在综合软件中转化为hex文件,但如果所需信号要叠加各种各样的噪声时。
Excel则不能胜任,此时我们用MATLAB生成所需初始化数据,在使用下述程序则很容易生成HEX初始化文件。
程序先介绍hex文件的结构,后给出matlab程序,且我做了详细的注释,希望对大家有所帮助。
% __Created by He Yapeng.@School of Electronic Engineering and Photoelectricity Technology at Nanjing University of Science and Technology%--Email:***************.');% Intel HEX文件是由一行行符合Intel HEX文件格式的文本所构成的ASCII文本文件。
在Intel HEX文件中,% 每一行包含一个HEX记录。
这些记录由对应机器语言码和/或常量数据的十六进制编码数字组成。
Intel HEX文件通常用于传输将被存于ROM或者EPROM中的程序和数据。
大多数EPROM编程器或模拟器使用Intel HEX文件。
% 记录格式% Intel HEX由任意数量的十六进制记录组成。
每个记录包含5个域,它们按以下格式排列:% :llaaaatt[dd...]cc% 每一组字母对应一个不同的域,每一个字母对应一个十六进制编码的数字。
每一个域由至少两个十六进制编码数字组成,它们构成一个字节,就像以下描述的那样:% : 每个Intel HEX记录都由冒号开头.% ll 是数据长度域,它代表记录当中数据字节(dd)的数量.% aaaa 是地址域,它代表记录当中数据的起始地址.% tt 是代表HEX记录类型的域,它可能是以下数据当中的一个:% 00 –数据记录% 01 –文件结束记录% 02 –扩展段地址记录% 04 –扩展线性地址记录% dd 是数据域,它代表一个字节的数据.一个记录可以有许多数据字节.记录当中数据字节的数量必须和数据长度域(ll)中指定的数字相符.% cc 是校验和域,它表示这个记录的校验和.校验和的计算是通过将记录当中所有十六进制编码数字对的值相加,以256为模进行以下补足.%% 数据记录% Intel HEX文件由任意数量以回车换行符结束的数据记录组成.数据记录外观如下:% :10246200464C5549442050524F46494C4500464C33% 其中:% 10 是这个记录当中数据字节的数量.% 2462 是数据将被下载到存储器当中的地址.% 00 是记录类型(数据记录)% 464C…464C是数据.% 33 是这个记录的校验和.%% 扩展线性地址记录(HEX386)% 扩展线性地址记录也叫作32位地址记录或HEX386记录.这些记录包含数据地址的高16位.扩展线性地址记录总是有两个数据字节,外观如下:% :02000004FFFFFC% 其中:% 02 是这个记录当中数据字节的数量.% 0000 是地址域,对于扩展线性地址记录,这个域总是0000.% 04 是记录类型 04(扩展线性地址记录)% FFFF 是地址的高16位.% FC 是这个记录的校验和,计算方法如下:% 01h + NOT(02h + 00h + 00h + 04h + FFh + FFh).% 当一个扩展线性地址记录被读取,存储于数据域的扩展线性地址被保存,它被应用于从Intel HEX文件读取来的随后的记录.线性地址保持有效,直到它被另外一个扩展地址记录所改变.% 通过把记录当中的地址域与被移位的来自扩展线性地址记录的地址数据相加获得数据记录的绝对存储器地址.% 以下的例子演示了这个过程..% 来自数据记录地址域的地址 2462% 扩展线性地址记录的数据域 + FFFF% ------------% 绝对存储器地址 FFFF2462%% 扩展段地址记录(HEX86)% 扩展段地址记录也叫HEX86记录,它包括4-19位数据地址段.扩展段地址记录总是有两个数据字节,外观如下:% :020*********EA% 其中:% 02 是记录当中数据字节的数量.% 0000 是地址域.对于扩展段地址记录,这个域总是0000.% 02 是记录类型 02(扩展段地址记录)% 1200 是地址段.% EA 是这个记录的校验和,计算方法如下:% 01h + NOT(02h + 00h + 00h + 02h + 12h + 00h).% 当一个扩展段地址记录被读取,存储于数据域的扩展段地址被保存,它被应用于从Intel HEX文件读取来的随后的记录.段地址保持有效,直到它被另外一个扩展地址记录所改变.% 通过把记录当中的地址域与被移位的来自扩展段地址记录的地址数据相加获得数据记录的绝对存储器地址.% 以下的例子演示了这个过程..% 来自数据记录地址域的地址 2462% 扩展段地址记录数据域 + 1200% ---------% 绝对存储器地址 00014462%% 文件结束(EOF)记录% Intel HEX文件必须以文件结束(EOF)记录结束.这个记录的记录类型域的值必须是01.EOF 记录外观总是如下:% :00000001FF% 其中:% 00 是记录当中数据字节的数量.% 0000 是数据被下载到存储器当中的地址.在文件结束记录当中地址是没有意义被忽略的.0000h是典型的地址.% 01 是记录类型 01(文件结束记录)% FF 是这个记录的校验和,计算方法如下:% 01h + NOT(00h + 00h + 00h + 01h).%% Intel HEX文件例子:% 下面是一个完整的Intel HEX文件的例子:% :10001300AC12AD13AE10AF1112002F8E0E8F0F2244% :10000300E50B250DF509E50A350CF5081200132259% :03000000020023D8% :0C002300787FE4F6D8FD7581130200031D% :10002F00EFF88DF0A4FFEDC5F0CEA42EFEEC88F016% :04003F00A42EFE22CB% :00000001FF%%%% 附:英文原文%% QUESTION% What is the Intel HEX file format?% ANSWER% The Intel HEX file is an ASCII text file with lines of text that follow the % Intel HEX file format. Each line in an Intel HEX file contains one HEX record. % These records are made up of hexadecimal numbers that represent machine% language code and/or constant data. Intel HEX files are often used to transfer % the program and data that would be stored in a ROM or EPROM. Most EPROM% programmers or emulators can use Intel HEX files.% Record Format% An Intel HEX file is composed of any number of HEX records. Each record is made % up of five fields that are arranged in the following format:% :llaaaatt[dd...]cc% Each group of letters corresponds to a different field, and each letter% represents a single hexadecimal digit. Each field is composed of at least two % hexadecimal digits-which make up a byte-as described below:% : is the colon that starts every Intel HEX record.% ll is the record-length field that represents the number of data bytes (dd) in % the record.% aaaa is the address field that represents the starting address for subsequent % data in the record.% tt is the field that represents the HEX record type, which may be one of the % following:% 00 - data record% 01 - end-of-file record% 02 - extended segment address record% 04 - extended linear address record% dd is a data field that represents one byte of data. A record may have multiple % data bytes. The number of data bytes in the record must match the number% specified by the ll field.% cc is the checksum field that represents the checksum of the record. The% checksum is calculated by summing the values of all hexadecimal digit pairs in % the record modulo 256 and taking the two's complement.% Data Records% The Intel HEX file is made up of any number of data records that are terminated % with a carriage return and a linefeed. Data records appear as follows:% :10246200464C5549442050524F46494C4500464C33% where:% 10 is the number of data bytes in the record.% 2462 is the address where the data are to be located in memory.% 00 is the record type 00 (a data record).% 464C...464C is the data.% 33 is the checksum of the record.% Extended Linear Address Records (HEX386)% Extended linear address records are also known as 32-bit address records and % HEX386 records. These records contain the upper 16 bits (bits 16-31) of the % data address. The extended linear address record always has two data bytes and % appears as follows:% :02000004FFFFFC% where:% 02 is the number of data bytes in the record.% 0000 is the address field. For the extended linear address record, this field % is always 0000.% 04 is the record type 04 (an extended linear address record).% FFFF is the upper 16 bits of the address.% FC is the checksum of the record and is calculated as% 01h + NOT(02h + 00h + 00h + 04h + FFh + FFh).% When an extended linear address record is read, the extended linear address % stored in the data field is saved and is applied to subsequent records read % from the Intel HEX file. The linear address remains effective until changed by % another extended address record.% The absolute-memory address of a data record is obtained by adding the address % field in the record to the shifted address data from the extended linear% address record. The following example illustrates this process..% Address from the data record's address field 2462% Extended linear address record data field FFFF% --------% Absolute-memory address FFFF2462% Extended Segment Address Records (HEX86)% Extended segment address records-also known as HEX86 records-contain bits 4-19 % of the data address segment. The extended segment address record always has two % data bytes and appears as follows:% :020*********EA% where:% 02 is the number of data bytes in the record.% 0000 is the address field. For the extended segment address record, this field % is always 0000.% 02 is the record type 02 (an extended segment address record).% 1200 is the segment of the address.% EA is the checksum of the record and is calculated as% 01h + NOT(02h + 00h + 00h + 02h + 12h + 00h).% When an extended segment address record is read, the extended segment address % stored in the data field is saved and is applied to subsequent records read % from the Intel HEX file. The segment address remains effective until changed by % another extended address record.% The absolute-memory address of a data record is obtained by adding the address % field in the record to the shifted-address data from the extended segment% address record. The following example illustrates this process.% Address from the data record's address field 2462% Extended segment address record data field 1200% --------% Absolute memory address 00014462% End-of-File (EOF) Records% An Intel HEX file must end with an end-of-file (EOF) record. This record must % have the value 01 in the record type field. An EOF record always appears as % follows:% :00000001FF% where:% 00 is the number of data bytes in the record.% 0000 is the address where the data are to be located in memory. The address in % end-of-file records is meaningless and is ignored. An address of 0000h is% typical.% 01 is the record type 01 (an end-of-file record).% FF is the checksum of the record and is calculated as% 01h + NOT(00h + 00h + 00h + 01h).% Example Intel HEX File% Following is an example of a complete Intel HEX file:% :10001300AC12AD13AE10AF1112002F8E0E8F0F2244% :10000300E50B250DF509E50A350CF5081200132259% :03000000020023D8% :0C002300787FE4F6D8FD7581130200031D% :10002F00EFF88DF0A4FFEDC5F0CEA42EFEEC88F016% :04003F00A42EFE22CB% :00000001FFclear ;clc;data=1:16;%data need to be writen into the mif filefh=fopen('E:Hex1.hex','w');l_data=length(data);for j=1:l_dataadr=dec2hex(j-1,4);%4位十六进制表示的地址data_hex=dec2hex(data(j),4);%4位十六进制表示的数据data_H=floor(data(j)/2^8);%数据十六进制高两位data_L=data(j)-data_H*2^8;%数据十六进制低两位adr_H=floor((j-1)/2^8);%地址十六进制高两位adr_L=(j-1)-adr_H*2^8;%地址十六进制低两位idetify_word=mod(2^8-mod(2+adr_H+adr_L+data_H+data_L,2^8),2^8);%十进制表示的校验位,外围mod函数是以防里面的mod函数结果为0idetify_word_hex=dec2hex(idetify_word,2);%十六进制表示的校验位fprintf(fh,[':02',adr,'00',data_hex,idetify_word_hex,' ']);%产生HEX码(end-1:end)endfprintf(fh,':00000001FF');fclose(fh);。
如何输出HEX文件?方法一最好用)在配置文件后面加入以下代码,便可输出HEX文件,A90文件与HEX文件一样,SLISP都能识别.如图Override default 目录下的那个文件中最后面加入以下代码// Output File-Ointel-extended,(XDATA)=.eep //产生eeprom文件-Ointel-extended,(CODE)=.A90 //产生烧写文件-Ointel-extended,(CODE)=.hex //产生烧写文件或者不在配置文件后面加,直接在如下图的地方加入命令方法二)Output 选择zax-i这个方法会出现一点小问题,如果有__eeprom 则会出下面的错误提示,__flash 没事。
Error[e133]: The output format intel-standard cannot handle multiple address spaces. Use format variants (-y -O) to specify which address space is wanted (.hex 和.eep不能同时放到一个文件中,如果没有.eep文件生成就不会报错,可以正常使用了)中断向量的使用IAR中定义中断函数的格式是/////////////////////////////////#pragma vector=中断向量__interrupt void 中断服务程序(void){//中断处理程序}/////////////////////////////////////中断的初始化要另外加入代码,可在主程序内加入。
如下是各个中断函数的定义。
//中断定义#include <iom16.h>#pragma vector=INT0_vect__interrupt void INT0_Server(void){}#pragma vector=INT1_vect__interrupt void INT1_Server(void){}#pragma vector=TIMER2_COMP_vect__interrupt void TIMER2_COMP_Server(void){}#pragma vector=TIMER2_OVF_vect__interrupt void TIMER2_OVF_Server(void){}#pragma vector=TIMER1_CAPT_vect__interrupt void TIMER1_CAPT_Server(void){}#pragma vector=TIMER1_COMPA_vect__interrupt void TIMER1_COMPA_Server(void){}#pragma vector=TIMER1_COMPB_vect__interrupt void TIMER1_COMPB_Server(void){}#pragma vector=TIMER1_OVF_vect__interrupt void TIMER1_OVF_Server(void){}#pragma vector=TIMER0_OVF_vect__interrupt void TIMER0_OVF_Server(void) {}#pragma vector=SPI_STC_vect__interrupt void SPI_STC_Server(void){}#pragma vector=USART_RXC_vect__interrupt void USART_RXC_Server(void) {}#pragma vector=USART_UDRE_vect__interrupt void USART_UDRE_Server(void) {}#pragma vector=USART_TXC_vect__interrupt void USART_TXC_Server(void) {}#pragma vector=ADC_vect__interrupt void ADC_Server(void){}#pragma vector=EE_RDY_vect__interrupt void EE_RDY_Server(void){}#pragma vector=ANA_COMP_vect__interrupt void ANA_COMP_Server(void) {}#pragma vector=TWI_vect__interrupt void TWI_Server(void){}#pragma vector=INT2_vect__interrupt void INT2_Server(void){}#pragma vector=TIMER0_COMP_vect__interrupt void TIMER0_COMP_Server(void) {}#pragma vector=SPM_RDY_vect__interrupt void SPM_RDY_Server(void){}如何把常数字符串定义在flash 空间?法一:unsigned char __flash temptab[] = {1,2,3,4,5};法二:__flash unsigned char temptab[] = {1,2,3,4,5};法三:#pragma type_attribute=__flashunsigned char temptab[]={1,2,3,4,5};法四:const unsigned char temptab[]={1,2,3,4,5};注:第三种方式用#pragma说明后,下面的定义的变量将都在FLASH空间了,用于定义一批FLASH变量,但实际上一般只能作为常量使用了.心得1图中CSTACK的大小调整大小可在编译后的632 bytes of DATA memory (+ 22 absolute ) 中看到。
用keil生成hex文件
1、建立mcs51源文件,选择new project建立管理项目
2、选择被仿真的51器件
3、建立源文件
输入汇编语言源程序
4、点击如图图标,对源程序进行编译、链接
在下面的图形中,会显示是否有语法错误,如有着调入源程序改正,如没有则可进行下一步,生成扩展名为“hex”的二进制文件。
5、生成“hex”文件
1)鼠标指向项目文件夹
2)压下鼠标右件,选择如图中的选项,进入
3)在下图中选择,“output”
将Creat HEX File打勾建立“hex”文件,在Name of Executable 给可执行文件取名,在Select Folder for Objects 选择存放路径,然后确定。
4)点击图标再次进行汇编,汇编后即可得到扩展名为“hex”的可执行二进制文件。
KEIL怎样生成HEX文件和相关设置学习51单片机的朋友面对着全英文的KEIL肯定会有这样的疑问“KEIL如何生成HEX文件?”,“生成的HEX文件在哪里?”下面就简单介绍一下生成HEX 的方法工具/原料∙keil uv2 uv3∙winXP新建项目1. 1先打开KEIL软件,点击“project”菜单2. 2点击“new project”新建一个项目。
如果不新建项目是没法生成HEX文件的3. 3在接下来的对话框中,选一个目录,然后为这个项目起一个名字。
名字可以随便起,这里以“测试项目”为例4. 4接下来出现一个对话框,要为项目选一个CPU。
点击左侧的树状目录滚动条,点开“ATMEL”选中“AT89C51”,单击确定5. 5这个对话框问是否把启动文件加进项目,选“是”6. 6要生成HEX文件还要设置一下。
单击下图中的设置图标7.7切换到“output”选项卡,勾选下图中“create HEX file”。
如果要改变HEX 文件的保存位置,可以点左上角的“select flolder for objects”选一个新的文件夹完成设置后选确定END添加源文件并生成HEX文件1. 1接下来添加源文件,源文件可以是用C语言或者是汇编语言,现在以C语言为例。
在下图的图标点右键2. 2在弹出的菜单中点击“add files to group source group 1”3. 3选中要加入的C文件,C文件可以是从别处复制过来,也可以自己写4. 4点“add”后可以看到C文件出现在左侧的目录中。
这时要点击“close”关闭对话框5. 5源程序添加进来后可以点击图中编译图标生成HEX文件6. 6然后在底下的状态框中可以看到结果,出现下图表示HEX文件已经生成了7.7打开文件夹可以看到,HEX文件已经生成了END注意事项第1步:打开Keil uVision2/3第2步:新建工程project->new project将弹出creat new project框,写入工程名称,例如test。
用keil生成hex文件
1、建立mcs51源文件,选择new project建立管理项目
2、选择被仿真的51器件
3、建立源文件
输入汇编语言源程序
4、点击如图图标,对源程序进行编译、链接
在下面的图形中,会显示是否有语法错误,如有着调入源程序改正,如没有则可进行下一步,生成扩展名为“hex”的二进制文件。
5、生成“hex”文件
1)鼠标指向项目文件夹
2)压下鼠标右件,选择如图中的选项,进入
3)在下图中选择,“output”
将Creat HEX File打勾建立“hex”文件,在Name of Executable 给可执行文件取名,在Select Folder for Objects 选择存放路径,然后确定。
4)点击图标再次进行汇编,汇编后即可得到扩展名为“hex”的可执行二进制文件。
生成HEX文件的MATLAB程序ROM初始化文件既可以是*.mif也可以是*.hex文件,但是如果工程需要在Modelsim中进行仿真的话,则必须生成*.hex文件对ROM进行初始化。
一般简单且有规律的初始化数据可以用Excel先生成mif文件再在综合软件中转化为hex文件,但如果所需信号要叠加各种各样的噪声时。
Excel则不能胜任,此时我们用MATLAB生成所需初始化数据,在使用下述程序则很容易生成HEX初始化文件。
程序先介绍hex文件的结构,后给出matlab程序,且我做了详细的注释,希望对大家有所帮助。
% __Created by He Yapeng.@School of Electronic Engineering and Photoelectricity Technology at Nanjing University of Science and Technology% --Email:lwkj0920@. ');% Intel HEX文件是由一行行符合Intel HEX文件格式的文本所构成的ASCII文本文件。
在Intel HEX文件中,% 每一行包含一个HEX记录。
这些记录由对应机器语言码和/或常量数据的十六进制编码数字组成。
Intel HEX文件通常用于传输将被存于ROM或者EPROM中的程序和数据。
大多数EPROM编程器或模拟器使用Intel HEX文件。
% 记录格式% Intel HEX由任意数量的十六进制记录组成。
每个记录包含5个域,它们按以下格式排列:% :llaaaatt[dd...]cc% 每一组字母对应一个不同的域,每一个字母对应一个十六进制编码的数字。
每一个域由至少两个十六进制编码数字组成,它们构成一个字节,就像以下描述的那样:% : 每个Intel HEX记录都由冒号开头.% ll 是数据长度域,它代表记录当中数据字节(dd)的数量.% aaaa 是地址域,它代表记录当中数据的起始地址.% tt 是代表HEX记录类型的域,它可能是以下数据当中的一个:% 00 –数据记录% 01 –文件结束记录% 02 –扩展段地址记录% 04 –扩展线性地址记录% dd 是数据域,它代表一个字节的数据.一个记录可以有许多数据字节.记录当中数据字节的数量必须和数据长度域(ll)中指定的数字相符.% cc 是校验和域,它表示这个记录的校验和.校验和的计算是通过将记录当中所有十六进制编码数字对的值相加,以256为模进行以下补足.%% 数据记录% Intel HEX文件由任意数量以回车换行符结束的数据记录组成.数据记录外观如下:% :10246200464C5549442050524F46494C4500464C33% 其中:% 10 是这个记录当中数据字节的数量.% 2462 是数据将被下载到存储器当中的地址.% 00 是记录类型(数据记录)% 464C…464C是数据.% 33 是这个记录的校验和.%% 扩展线性地址记录(HEX386)% 扩展线性地址记录也叫作32位地址记录或HEX386记录.这些记录包含数据地址的高16位.扩展线性地址记录总是有两个数据字节,外观如下:% :02000004FFFFFC% 其中:% 02 是这个记录当中数据字节的数量.% 0000 是地址域,对于扩展线性地址记录,这个域总是0000.% 04 是记录类型 04(扩展线性地址记录)% FFFF 是地址的高16位.% FC 是这个记录的校验和,计算方法如下:% 01h + NOT(02h + 00h + 00h + 04h + FFh + FFh).% 当一个扩展线性地址记录被读取,存储于数据域的扩展线性地址被保存,它被应用于从Intel HEX文件读取来的随后的记录.线性地址保持有效,直到它被另外一个扩展地址记录所改变.% 通过把记录当中的地址域与被移位的来自扩展线性地址记录的地址数据相加获得数据记录的绝对存储器地址.% 以下的例子演示了这个过程..% 来自数据记录地址域的地址 2462% 扩展线性地址记录的数据域 + FFFF% ------------% 绝对存储器地址 FFFF2462%% 扩展段地址记录(HEX86)% 扩展段地址记录也叫HEX86记录,它包括4-19位数据地址段.扩展段地址记录总是有两个数据字节,外观如下:% :020*********EA% 其中:% 02 是记录当中数据字节的数量.% 0000 是地址域.对于扩展段地址记录,这个域总是0000.% 02 是记录类型 02(扩展段地址记录)% 1200 是地址段.% EA 是这个记录的校验和,计算方法如下:% 01h + NOT(02h + 00h + 00h + 02h + 12h + 00h).% 当一个扩展段地址记录被读取,存储于数据域的扩展段地址被保存,它被应用于从Intel HEX文件读取来的随后的记录.段地址保持有效,直到它被另外一个扩展地址记录所改变.% 通过把记录当中的地址域与被移位的来自扩展段地址记录的地址数据相加获得数据记录的绝对存储器地址.% 以下的例子演示了这个过程..% 来自数据记录地址域的地址 2462% 扩展段地址记录数据域 + 1200% ---------% 绝对存储器地址 00014462%% 文件结束(EOF)记录% Intel HEX文件必须以文件结束(EOF)记录结束.这个记录的记录类型域的值必须是01.EOF 记录外观总是如下:% :00000001FF% 其中:% 00 是记录当中数据字节的数量.% 0000 是数据被下载到存储器当中的地址.在文件结束记录当中地址是没有意义被忽略的.0000h是典型的地址.% 01 是记录类型 01(文件结束记录)% FF 是这个记录的校验和,计算方法如下:% 01h + NOT(00h + 00h + 00h + 01h).%% Intel HEX文件例子:% 下面是一个完整的Intel HEX文件的例子:% :10001300AC12AD13AE10AF1112002F8E0E8F0F2244% :10000300E50B250DF509E50A350CF5081200132259% :03000000020023D8% :0C002300787FE4F6D8FD7581130200031D% :10002F00EFF88DF0A4FFEDC5F0CEA42EFEEC88F016% :04003F00A42EFE22CB% :00000001FF%%%% 附:英文原文%% QUESTION% What is the Intel HEX file format?% ANSWER% The Intel HEX file is an ASCII text file with lines of text that follow the % Intel HEX file format. Each line in an Intel HEX file contains one HEX record. % These records are made up of hexadecimal numbers that represent machine% language code and/or constant data. Intel HEX files are often used to transfer % the program and data that would be stored in a ROM or EPROM. Most EPROM% programmers or emulators can use Intel HEX files.% Record Format% An Intel HEX file is composed of any number of HEX records. Each record is made % up of five fields that are arranged in the following format:% :llaaaatt[dd...]cc% Each group of letters corresponds to a different field, and each letter% represents a single hexadecimal digit. Each field is composed of at least two % hexadecimal digits-which make up a byte-as described below:% : is the colon that starts every Intel HEX record.% ll is the record-length field that represents the number of data bytes (dd) in % the record.% aaaa is the address field that represents the starting address for subsequent % data in the record.% tt is the field that represents the HEX record type, which may be one of the % following:% 00 - data record% 01 - end-of-file record% 02 - extended segment address record% 04 - extended linear address record% dd is a data field that represents one byte of data. A record may have multiple % data bytes. The number of data bytes in the record must match the number% specified by the ll field.% cc is the checksum field that represents the checksum of the record. The% checksum is calculated by summing the values of all hexadecimal digit pairs in % the record modulo 256 and taking the two's complement.% Data Records% The Intel HEX file is made up of any number of data records that are terminated % with a carriage return and a linefeed. Data records appear as follows:% :10246200464C5549442050524F46494C4500464C33% where:% 10 is the number of data bytes in the record.% 2462 is the address where the data are to be located in memory.% 00 is the record type 00 (a data record).% 464C...464C is the data.% 33 is the checksum of the record.% Extended Linear Address Records (HEX386)% Extended linear address records are also known as 32-bit address records and % HEX386 records. These records contain the upper 16 bits (bits 16-31) of the % data address. The extended linear address record always has two data bytes and % appears as follows:% :02000004FFFFFC% where:% 02 is the number of data bytes in the record.% 0000 is the address field. For the extended linear address record, this field % is always 0000.% 04 is the record type 04 (an extended linear address record).% FFFF is the upper 16 bits of the address.% FC is the checksum of the record and is calculated as% 01h + NOT(02h + 00h + 00h + 04h + FFh + FFh).% When an extended linear address record is read, the extended linear address % stored in the data field is saved and is applied to subsequent records read % from the Intel HEX file. The linear address remains effective until changed by % another extended address record.% The absolute-memory address of a data record is obtained by adding the address % field in the record to the shifted address data from the extended linear% address record. The following example illustrates this process..% Address from the data record's address field 2462% Extended linear address record data field FFFF% --------% Absolute-memory address FFFF2462% Extended Segment Address Records (HEX86)% Extended segment address records-also known as HEX86 records-contain bits 4-19 % of the data address segment. The extended segment address record always has two % data bytes and appears as follows:% :020*********EA% where:% 02 is the number of data bytes in the record.% 0000 is the address field. For the extended segment address record, this field % is always 0000.% 02 is the record type 02 (an extended segment address record).% 1200 is the segment of the address.% EA is the checksum of the record and is calculated as% 01h + NOT(02h + 00h + 00h + 02h + 12h + 00h).% When an extended segment address record is read, the extended segment address % stored in the data field is saved and is applied to subsequent records read % from the Intel HEX file. The segment address remains effective until changed by % another extended address record.% The absolute-memory address of a data record is obtained by adding the address % field in the record to the shifted-address data from the extended segment% address record. The following example illustrates this process.% Address from the data record's address field 2462% Extended segment address record data field 1200% --------% Absolute memory address 00014462% End-of-File (EOF) Records% An Intel HEX file must end with an end-of-file (EOF) record. This record must % have the value 01 in the record type field. An EOF record always appears as % follows:% :00000001FF% where:% 00 is the number of data bytes in the record.% 0000 is the address where the data are to be located in memory. The address in % end-of-file records is meaningless and is ignored. An address of 0000h is% typical.% 01 is the record type 01 (an end-of-file record).% FF is the checksum of the record and is calculated as% 01h + NOT(00h + 00h + 00h + 01h).% Example Intel HEX File% Following is an example of a complete Intel HEX file:% :10001300AC12AD13AE10AF1112002F8E0E8F0F2244% :10000300E50B250DF509E50A350CF5081200132259% :03000000020023D8% :0C002300787FE4F6D8FD7581130200031D% :10002F00EFF88DF0A4FFEDC5F0CEA42EFEEC88F016% :04003F00A42EFE22CB% :00000001FFclear ;clc;data=1:16;%data need to be writen into the mif filefh=fopen('E:Hex1.hex','w');l_data=length(data);for j=1:l_dataadr=dec2hex(j-1,4);%4位十六进制表示的地址data_hex=dec2hex(data(j),4);%4位十六进制表示的数据data_H=floor(data(j)/2^8);%数据十六进制高两位data_L=data(j)-data_H*2^8;%数据十六进制低两位adr_H=floor((j-1)/2^8);%地址十六进制高两位adr_L=(j-1)-adr_H*2^8;%地址十六进制低两位idetify_word=mod(2^8-mod(2+adr_H+adr_L+data_H+data_L,2^8),2^8);%十进制表示的校验位,外围mod函数是以防里面的mod函数结果为0idetify_word_hex=dec2hex(idetify_word,2);%十六进制表示的校验位fprintf(fh,[':02',adr,'00',data_hex,idetify_word_hex,' ']);%产生HEX码(end-1:end)endfprintf(fh,':00000001FF');fclose(fh);。