专栏中心

EEPW首页 > 专栏 > Intel hex 文件格式(z)

Intel hex 文件格式(z)

发布人:mayer 时间:2009-08-07 来源:工程师 发布文章
 

 

Intel hex 文件常用来保存单片机或其他处理器的目标程序代码。它保存物理程序存储区中的目标代码映象。一般的编程器都支持这种格式。
    Intel hex 文件全部由可打印的ASCII字符组成(可以用记事本打开),如下例所示:

:2000000012014c75a800e4f508f509780a7a78e4f608dafcd283fcfded240af9a7050dbd81        

:2000200000010ced2488ec34ff50edc283e4fcfded240af9e76d7013ed33e43c700d0dbd2a        

:2000400000010ced2488ec34ff50e50509e50970020508e50924a8e50834fd50aee4f50874

    Intel hex 由一条或多条记录组成,每条记录都由一个冒号“:”打头,其格式如下:

         :CCAAAARR...ZZ

    其中:

CC

    本条记录中的数据字节数

AAAA

    本条记录中的数据在存储区中的起始地址

RR

    记录类型:

00 数据记录 (data record)
01 结束记录 (end record)
02 段记录 (paragraph record)
03 转移地址记录 (transfer address record)

...

    数据域

ZZ

    数据域校验和

    Intel hex文件记录中的数字都是16进制格式,两个16进制数字代表一个字节。CC域是数据域中的实际字节数,地址、记录类型和校验和域没有计算在内。校验和是取记录中从数据字节计数域(CC)到数据域(...)最后一个字节的所有字节总和的2的补码。

----------------------------------------------------------------------------

// 这是用C做的 生成HEX文件的程序/这是个正弦ROM表生成程序。直接在VC里运行。

#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#define PI 3.1415926

void main(void)
{
 signed char W_R=0;
    signed char num="2";
 signed short int address;
 signed short int address_h;
 signed short int address_l;
 signed short int data_h;
 signed short int data_l;
    double data;
 signed char crc;
 FILE *fp;
 fp=fopen("C:\\sinwave.hex","w");
 //fprintf(fp,"sin[1024]=");
 for(address=0;address<1024;address++)//10=取的点数
 {
  fprintf(fp,":");
  data=(sin(2*PI*address/(1024-1))+1)*512;//9=分的段数
        data_h=((signed short int)data&0xff00)>>8;//gao 8 wei
  data_l=(signed short int)data&0x00ff;//di 8 wei
        address_h=((signed short int)address&0xff00)>>8;//gao 8 wei
  address_l=(signed short int)address&0x00ff;//di 8 wei 
  crc=~((02+address_h+address_l+data_h+data_l)%256)+1;
  fprintf(fp,"02");  // 默认每行记录2个数据

fprintf(fp,"%04x",address);

  fprintf(fp,"00");
  if(data>=0&&data<0x10) fprintf(fp,"000");
  if(data>=0x10&&data<0x100) fprintf(fp,"00");
  if(data>=0x100&&data<0x1000) fprintf(fp,"0");
  fprintf(fp,"%x",(unsigned int)data);
  if(crc>=0x0&&crc<0x10) fprintf(fp,"0");
  fprintf(fp,"%x",(unsigned char)crc);
  fprintf(fp,"\n");
 }
 fprintf(fp,":00000001ff");
}

---------------------------------------------------------------------------------

% 用matlab产生hex文件
function hexfile(filename,var,databytenum,firstaddr,record_set,depth)
% function hexfile(filename,var,width,depth)
%       It creates a 'hex' file called filename,which be written with var.
%       The 'mif' file is a kind of file formats which is uesed in Altera's
%       EDA tool,like maxplus II ,quartus II,to initialize the memory
%       models,just like cam,rom,ram.
%       Using this function,you can easily produce the 'hex' file written
%       with  all kinds of your data.
%       If the size of 'var' is shorter than 'depth',0 will be written for the
%       lefts.If the size of 'var' is greater than 'depth',than only 'depth' former
%       data of 'var' will be written;
%       the radix of address and data is hex
%       filename --the name of the file to be created,eg,"a.hex",string;
%       var --the data to be writed to the file, can be 3D or less ,int or other fittable;
%       databytenum --the num of byte in the every record,int,dec;
%       firstaddr --the first address of the data to be stored in the memory,int,dec;
%       record_set --the record style of the data,int,dec;
%       depth --the number of the record to be writed,int;
%       because matlab read the matrix is colum first,if you want to write
%       the 'var' data in row first mode, just set var to var';
%       author:email --allstars52@gmail.com
%       Data:07/10/2007
%       example:
%            a="uint8"(rand(16,1)*256);
%            hexfile('randnum.hex',a,2,0,0,16);
%       Intel hex 由一条或多条记录组成,每条记录都由一个冒号“:”打头,其格式如下:     
%       :CCAAAARR...ZZ
%     其中:
%       CC:本条记录中的数据字节数
%       AAAA :本条记录中的数据在存储区中的起始地址
%       RR :记录类型:
%        00 数据记录     (data record)
%        01 结束记录     (end record)
%        02 段记录       (paragraph record)
%        03 转移地址记录 (transfer address record)
%       ... :数据域
%       ZZ :数据域校验和
%       ZZ="01h"+NOT(CC+AA+AA+RR+...)

fh=fopen(filename,'w+');
var=rem(var,depth+2);
[sx,sy,sz]=size(var);
value=var(1,1,1);
idepth=0;
temp=1;
temp1=uint8(0);
zz=0;
straddr=dec2hex(0,4);
strnum=dec2hex(0,databytenum);
strdata=dec2hex(0,databytenum);
strdatasum=0;
tatol=0;
for k="1:sz",
    for j="1:sy",
        for i="1:sx",
            if(~((i==1 ) &&( j==1) &&( k==1)))
               if(idepth<depth)
                  idepth="idepth"+1;
                  firstaddr="firstaddr"+1;
                  straddr="dec2hex"(firstaddr-1,4);
                  for m="0:2"(databytenum-1)*2)
                      strdata="dec2hex"(value,2*databytenum);
                      x="1"+m;
                      y="x"+1;
                      strdatasum="strdatasum"+hex2dec(strdata(x:y));
                  end
                   temp1=uint8(databytenum+hex2dec(straddr(1:2))+hex2dec(straddr(3:4))+record_set+strdatasum);
                   zz="dec2hex"((1+bitxor(temp1, uint8(255))),2);%数据域校验和
                   tatol=[dec2hex(databytenum,2) dec2hex(firstaddr-1,4) dec2hex(record_set,2) dec2hex(value,2*databytenum) dec2hex((1+bitxor(temp1, uint8(255))),2)];
                   fprintf(fh,':%s\r\n',tatol);
                   strdatasum="0";
                else
                   fprintf(fh,':%s\r\n',tatol);
                   strdatasum="0";
               end
                   value="var"(i,j,k);
            end
        end
    end
end

if(idepth<depth)
                 firstaddr="firstaddr"+1;
                 straddr="dec2hex"(firstaddr-1,4);
                           for m="0:2"(databytenum-1)*2)
                               strdata="dec2hex"(value,2*databytenum);
                               x="1"+m;
                               y="x"+1;
                               strdatasum="strdatasum"+hex2dec(strdata(x:y));
                               end
                               temp1=uint8(databytenum+hex2dec(straddr(1:2))+hex2dec(straddr(3:4))+record_set+strdatasum);
                               zz="dec2hex"((1+bitxor(temp1, uint8(255))),2);
                               tatol=[dec2hex(databytenum,2) dec2hex(firstaddr-1,4) dec2hex(record_set,2) dec2hex(value,2*databytenum) zz ];
                fprintf(fh,':%s\r\n',tatol);
              else
                fprintf(fh,':%s\r\n',tatol);
end

if(idepth<depth-1)
    if(idepth==(depth-2))
        fprintf(fh,':%s\r\n',0);
    else
        fprintf(fh,':%s\r\n',0);
    end
end
fprintf(fh,':00000001ff');   %hex文件结束标志          
fclose(fh);

% ---------------MATLAB 程序产生hex文件----------------------
% filename:设置你要保存的hex格式文件的文件名;
% databytenum:每条记录中的数据字节数;
% firstaddr:数据在存储区中的起始地址;
% record_set:记录类型;
% depth:深度,也就是总纪录数;

专栏文章内容及配图由作者撰写发布,仅供工程师学习之用,如有侵权或者其他违规问题,请联系本站处理。 联系我们

关键词:

相关推荐

射频通信电路分析

资源下载 2007-12-27

2026:物理智能元年

模拟技术 2026-02-25

ASML发布2025年度报告

EDA/PCB 2026-02-25

如何在数in2组成430W均流输出

视频 2011-02-20

水下无人机如何在水下通信

机器人 2026-02-25

恩智浦Cortex-M0 LPC1100设计挑战赛

视频 2011-03-17

模糊PID参数自整定技术及其在中央空调系统中的应用

介绍采用 nanoWatt XLP技术的超低功耗系列单片机

PID参数整定

氧化铟芯片为何备受关注?

村田采用电容器核心薄膜技术,开发废气循环VOC清除系统

汽车气囊参考演示

视频 2011-03-03

深圳村田科技有限公司20周年庆典暨新年会盛大举行

采用 LTpowerPlay 来管理 LTC2978

视频 2011-02-17

模糊PID在电阻炉温度控制系统中的应用

“小显存,大模型”国数集联解读如何降低AI普惠门槛

神经网络PID在电机调速中的应用

贸泽电子亮相SPS广州,一站式工业自动化平台加速AI+制造落地

定制化AI解决方案,决胜智造未来

【IEEE好文分享】运动传感器测量结果差异巨大,新标准正在缩小差距

更多 培训课堂
更多 焦点
更多 视频

技术专区