CCS支持的.dat文件详解


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

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

1651

其后是文件内容,每行表示一个数据。

定数固定为“1651”,数据格式可以选择“1”(十六进制整型)、“2”(十进制整型)、“3”(十进制长整型)、“4”(十进制浮点型)。起始地址为存储的地址,页类型和标示为程序或者数据。

比如一个.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.

tid=4537

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

File->Data->Load

File->Data->Store

File->File I/O

 

 


 

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);