正文

Intel hex 文件格式2007-04-22 13:42:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/vfdff/25121.html

分享到:

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:深度,也就是总纪录数;

阅读(6507) | 评论(3)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

loading...
您需要登录后才能评论,请 登录 或者 注册