首页 > 代码库 > matlab生成HEX文件
matlab生成HEX文件
HEX文件格式不赘述,写里直接放上代码。请批评改正。
1 %%convert a sinal data into hex file format 2 % data format:16bit 3 % signal length: less than 2^24-1 4 % author: Yang Li yangli0534@gmail.com 5 % data:2015.01.27 6 7 clear all; 8 close all; 9 clc;10 11 %% fixed point or binary16 float point12 fixed=1;13 %% generate a signal waveform14 %15 T=10;%time length16 fs=1e3;%sample rate17 N=10000;18 Period=N/fs/10;19 f=1/Period;20 t=linspace(0,(N-1)/fs,N);21 22 %data=http://www.mamicode.com/1:2^5-1; 23 data= http://www.mamicode.com/sin(2*pi*f*t) +(rand(1,N)-0.5)/10;24 % data=http://www.mamicode.com/mod(50000*t,1000);25 % plot(t,data)26 dataBits=16;27 byteBits=8;28 29 %% convert into 16 bits fixed point number30 if fixed == 131 maximum=max(abs(data));%maximum of absolute signal 32 scale=(2^15-1)/maximum;%scale 33 data=http://www.mamicode.com/round(data*scale);34 for i=1:1:length(data)35 if data(i) <0 36 data(i) =2^16-abs(data(i)) ;37 end38 end39 end40 %% write41 fh=fopen(‘data.hex‘,‘w‘);%open a new file (file path, file name)42 bytesEveryRow=16;%write 16 bytes every row43 dataEveryRow=bytesEveryRow/(dataBits/byteBits);%number of data every row44 lengthData=http://www.mamicode.com/length(data);%length of signal data45 rowLengths=ceil(lengthData/dataEveryRow);%number of rows of the file to be written46 47 for i=1:rowLengths % write a row each time48 if(mod(i,hex2dec(‘1000‘))==1)49 baseaddr=dec2hex(floor(i/hex2dec(‘1000‘)),4);50 checkSum=6+hex2dec(baseaddr(1:2))+hex2dec(baseaddr(3:4));51 checkSum=(dec2hex(bitand(256-mod(checkSum,256),255),2));52 fprintf(fh,[‘:02000004‘,baseaddr,checkSum,13,10]);%产生HEX码(end-1:end)53 54 end55 checkSum=0;56 addr=dec2hex(mod((i-1)*16,hex2dec(‘10000‘)),4);%the start address of this row57 checkSum=hex2dec(addr(1:2))+hex2dec(addr(3:4));58 if i*dataEveryRow <lengthData % numbers of data to be written greater than dataEveryRow59 dataRow=dataEveryRow;60 dataHex=‘‘;61 for j=8*(i-1)+1:1:8*(i) 62 if fixed == 1% 63 dataHexCurr=dec2hex(data(j),4);%16 bits fixed point64 else65 dataHexCurr=num2binary16hex(data(j));%number format: 16bits floatIEEE 754 half precison float point standard,result is a hex format66 end67 checkSum=checkSum+hex2dec(dataHexCurr(1:2))+hex2dec(dataHexCurr(3:4));68 dataHex=strcat(dataHex,dataHexCurr);69 end 70 71 else72 dataHex=‘‘;73 dataRow =lengthData-(rowLengths-1)*dataEveryRow;74 for j=(rowLengths-1)*dataEveryRow+1:1:lengthData 75 if fixed == 1% 76 dataHexCurr=dec2hex(data(j),4);%16 bits fixed point77 else78 dataHexCurr=num2binary16hex(data(j));%number format: 16bits floatIEEE 754 half precison float point standard,result is a hex format79 end 80 checkSum=checkSum+hex2dec(dataHexCurr(1:2))+hex2dec(dataHexCurr(3:4));81 dataHex=strcat(dataHex,dataHexCurr);82 end83 end84 checkSum=checkSum+dataRow*dataBits/byteBits;85 checkSum=(dec2hex(bitand(256-mod(checkSum,256),255),2)); 86 % disp([num2str(i),‘--‘,dataHex,‘--‘,checkSum])87 fprintf(fh,[‘:‘,dec2hex(dataRow*dataBits/byteBits,2),addr,‘00‘,dataHex,checkSum,13,10]);%产生HEX码(end-1:end)88 end89 90 fprintf(fh,‘:00000001FF‘); 91 fclose(fh);
代码中有定点浮点两种,其中浮点格式为IEEE754标准中的half-precision-floating point, 16bit, 1bit 浮号位,5bit指数位,10小数位,代码如下:
1 function [ hex ] = num2binary16he( number ) 2 %The IEEE 754 standard specifies a binary16 as having the following format: 3 4 %Sign bit: 1 bit 5 %Exponent width: 5 bits 6 %Significand precision: 11 bits (10 explicitly stored) 7 %S EEEEE MMMMMMMMMM 8 %INPUT: number is a random precision number 9 10 %OUTPUT : var binary is a 2‘s string of the binary1611 % var decimail is the value of the number in decimal12 13 %Author :Yang Li .14 %Email: yangli0534@gmail.com15 if real(number) >= 2^14 16 error(‘beyond the maximum :-2^14--+2^24‘);17 end18 19 S=number<0 ;20 E=0; 21 M=abs(number);22 while((E>-15 && E<15)&&( M>=2 || M<1 ))23 if(M>=2)24 E=E+1;25 M=M/2;26 else27 E=E-1;28 M=M*2;29 end 30 end31 if ((E==-15 || E==15)&& M>=2 || M<1)32 M=0;33 else34 M=round((M-1)*2^10) ; 35 end36 37 38 number1=(-1)^S*(2^ E )*(1+M/2^10);39 % binary=strcat(num2str(S),num2str(E),num2str(M)) 40 E =E+15;41 binary=strcat(num2str(S),dec2bin(E,5), dec2bin(M,10)) ;42 43 44 sReal=(str2double(binary(1)));45 eReal =-15; 46 for i=2:647 eReal=eReal+str2double(binary(i))*2^(6-i);48 end 49 % eReal=eReal*(-1)^str2double(binary(2));50 mReal = 0; 51 for i=7:1652 mReal=mReal+str2double(binary(i))*2^(16-i);53 end 54 55 numberReal=(-1)^sReal*2^eReal*(1+mReal/2^10);56 err=num2str(abs(number-numberReal));57 decimal=numberReal;58 % disp([‘the origin data is : ‘,num2str(number)]);59 % disp([‘the float format is : ‘,binary]); 60 % disp([‘so ,there is a error :‘,num2str(abs(number-numberReal))]);61 62 num=0;63 for i=1:1664 num=num+str2double(binary(i))*2^(16-i);65 end66 hex=dec2hex(num,4);67 end
matlab生成HEX文件
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。