1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > ccs读取dat文件c语言程序 详解CCS中的.dat文件

ccs读取dat文件c语言程序 详解CCS中的.dat文件

时间:2019-12-28 18:45:22

相关推荐

ccs读取dat文件c语言程序 详解CCS中的.dat文件

CCS支持的.dat文件的格式为:

文件头为

定数 数据格式起始地址页类型数据块大小

1651 1 80000000 0 10

固定标识 数据格式 基地址页类型 长度

数据格式:1-十六进制 2-十进制 3-十进制长整型 4-十进制浮点型

页类型: 0-数据 1-程序 ?

长度: 装入数据的长度

比如一个.dat文件:

1651 1 800 1 10

0x0000

0x0000

0x0000

0x0000

0x0000

0x0000

0x0000

0x0000

0x0000

0x0000

0x0000

0x0000

0x0000

制作.dat 文件的方法也很简单,可以用VC++或者MATLAB来实现。比如hellodsp的网友lwxing提供的使用matlab创建.dat文件的一个实例:

matlab向dsp传递.dat文件

x=2*sin(2*pi*100*m*dt);

for m=1:200;

if x(m)>=0 y(m)=x(m);

else y(m)=4+x(m);

end;

end;

y=y*16384;

fid=fopen('input.dat','w');%打开文件,'w'是将此文件定义为可写的,fid是此文件的整数标示

fprintf(fid,'1651 1 0 1 0\n');%输出文件头,文件头必须是dsp所能识别的,就如此句程序所设定的

fprintf(fid,'0x%x\n',round(y));%输出y数组,并写到与fid标示符相同的文件,即yinput.dat文件里。round是取y值的最近的数,即如果是1.2,就取1,如果1.6,就取2.

fclose(fid); %关闭fid标示符的文件。

fid=fopen('input.dat','w');%打开文件,属性设置为写

fprintf(fid,'1651 1 0 1 0\n');%输出文件头,只有此文件头dsp芯片才能识别

fprintf(fid,'0x%x\n',round(x));%输出十六进制的x

fclose(fid);关闭

这里x要转换成二进制补码,这也是我发此贴的目的所在。只是个人的理解,如果有问题,请大侠们改正,为更多dsp学习者们提供借鉴。

首先确定x的范围,譬如x=【-2,2】,那么,我们采用定点Q14,那么就是要乘以16384,如果x<0,还要转化成其补码。补码应该是用模加上x,即4+x,然后再将此数乘以16384.

其在CCS中的使用方法可以有一下命令:

File->Data->Load

File->Data->Store

File->File I/O

验证一下,采用倒推的方法,即使用save data将内存中的数据取出写入到一个文件中,首先将数据写入到内存中,写入的数据见下面截图

之后将部分数据,地址为0x80000000,长度0x10保存到test.dat中,使用编辑工具打开test.dat,得到的文件内容如下

1651 1 80000000 0 10

0x01020304

0x00000001

0x00000002

0x00000003

0x00000004

0x00000000

0x00000000

0x00000000

0x00000000

0x00000000

0x00000000

0x00000000

0x00000000

0x00000000

0x00000000

0x00000000

由于CCS中设定采用小端的存储方式,所以数据存储的顺序有一些改变,即0x01020304中04是数据的最低位,放在内存的LSB,01是数据的最高位,放在内存的MSB,同时在所有的32位数据之前添加了0x字段。

to mayerx:

我按你说的方法试了一下,不知道负数应该怎样处理?你说的“生成的数据转化为合适Q值的十六进制数据”看了不是很明白.我的小程序如下,错的地方请指正,谢谢!

x=8*sin(0.5*2*pi*[1:1000]/300)+3*sin(40*2*pi*[1:1000]/300);%产生数据

fid=fopen('input.dat','w');%打开文件

fprintf(fid,'1651 1 0 1 0/n');%输出文件头

fprintf(fid,'0x%x/n',round(x));%输出

fclose(fid);

************************888

我知道怎么用matlab产生dsp 需要的数据文件,程序附在下面.

% Creat a data file of the sinewave, which DSP chip can load .%

clear all;

Fs=8000; %Sampling Frequecy of AD535 chip=8KHz%

f1=100; %Frequency of Sine Wave"

y=10*sin(2*pi*f1*[0: 160]/Fs); % Number of Sampling point=160, and create sine wave%

plot(y);

grid on;

reply=input('Please input category of data: '); % input category of data%

fid=fopen(sine1.dat', 'w'); %creat a data file in the given folder%

fprintf(fid, '1651 %d 0 0 0 /n', reply); % output the head file of .dat file %

switch reply % output data into the .dat file according to the category of data%

case 1

fprintf(fid, '0x%x/n', y); % output 32bit hexadecimal data %

case 2

fprintf(fid, '%d/n', round(y)); % output 32bit float data %

case 3

fprintf(fid, '%12.1f/n', round(y)); % output 40bit long inter data %

case 4

fprintf(fid, '%f/n', y); % outout 32bit integer data %

end

fclose(fid); % close the .dat file %

*************************************************88

对于负数,应当采用补码,用模减去该数的绝对值.

你的程序中有如下问题没有考虑:

x=8*sin(0.5*2*pi*[1:1000]/300)+3*sin(40*2*pi*[1:1000]/300);%产生数据

%%此后要对X中的各个元素需要转化为合适的定点数,比如你的各元素在-11~11之间,则选用Q11,其示数范围在-16≤x≤15.9995117,否则保存结果应当还是浮点数.用浮点数A转换成定点数B:B =(int)A×2^Q来进行转换,如果是负数,还需要求补.

fid=fopen('input.dat','w');%打开文件

fprintf(fid,'1651 1 0 1 0/n');%输出文件头

fprintf(fid,'0x%x/n',round(x));%输出

fclose(fid);

to mayerx:

我按你说的方法试了一下,不知道负数应该怎样处理?你说的“生成的数据转化为合适Q值的十六进制数据”看了不是很明白.我的小程序如下,错的地方请指正,谢谢!

x=8*sin(0.5*2*pi*[1:1000]/300)+3*sin(40*2*pi*[1:1000]/300);%产生数据

fid=fopen('input.dat','w');%打开文件

fprintf(fid,'1651 1 0 1 0/n');%输出文件头

fprintf(fid,'0x%x/n',round(x));%输出

fclose(fid);

************************888

我知道怎么用matlab产生dsp 需要的数据文件,程序附在下面.

% Creat a data file of the sinewave, which DSP chip can load .%

clear all;

Fs=8000; %Sampling Frequecy of AD535 chip=8KHz%

f1=100; %Frequency of Sine Wave"

y=10*sin(2*pi*f1*[0: 160]/Fs); % Number of Sampling point=160, and create sine wave%

plot(y);

grid on;

reply=input('Please input category of data: '); % input category of data%

fid=fopen(sine1.dat', 'w'); %creat a data file in the given folder%

fprintf(fid, '1651 %d 0 0 0 /n', reply); % output the head file of .dat file %

switch reply % output data into the .dat file according to the category of data%

case 1

fprintf(fid, '0x%x/n', y); % output 32bit hexadecimal data %

case 2

fprintf(fid, '%d/n', round(y)); % output 32bit float data %

case 3

fprintf(fid, '%12.1f/n', round(y)); % output 40bit long inter data %

case 4

fprintf(fid, '%f/n', y); % outout 32bit integer data %

end

fclose(fid); % close the .dat file %

*************************************************88

对于负数,应当采用补码,用模减去该数的绝对值.

你的程序中有如下问题没有考虑:

x=8*sin(0.5*2*pi*[1:1000]/300)+3*sin(40*2*pi*[1:1000]/300);%产生数据

%%此后要对X中的各个元素需要转化为合适的定点数,比如你的各元素在-11~11之间,则选用Q11,其示数范围在-16≤x≤15.9995117,否则保存结果应当还是浮点数.用浮点数A转换成定点数B:B =(int)A×2^Q来进行转换,如果是负数,还需要求补.

fid=fopen('input.dat','w');%打开文件

fprintf(fid,'1651 1 0 1 0/n');%输出文件头

fprintf(fid,'0x%x/n',round(x));%输出

fclose(fid);

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。