详解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字段。

 

http://jinsuo2007.blog.163.com/blog/static/1922799520109151745262/






 
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);
  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值