代码如下:
/**************************************************
mif 文件生成
***************************************************/
#include <stdio.h>
#include <math.h>
/*-------------------------参数区------------------------*/
#define DEPTH 128 /* 数据深度,即存储单元的个数*/
#define WIDTH 8 /* 存储单元的宽度 */
#define MODE 1 /* 1:正弦波 2:三角波 3:锯齿波 4:方波 */
#define maxwords 255
/*-------------------------参数区------------------------*/
#define PI 3.141592
int main(void)
{
int i,temp=0;
FILE *fp;
fp = fopen("sin.mif","w"); /*可更改,但扩展名必须为.mif*/
if(NULL == fp)
printf("Can not creat file!\r\n");
else
{
printf("File created successfully!\n");
/*
* 生成文件头:注意不要忘了“;”
*/
fprintf (fp, "DEPTH = %d;\n",1920);
fprintf (fp, "WIDTH = %d;\n",WIDTH);
fprintf (fp, "ADDRESS_RADIX = HEX;\n");
fprintf (fp, "DATA_RADIX = HEX;\n");
fprintf (fp, "CONTENT\n");
fprintf (fp, "BEGIN\n");
/*
* 以十六进制输出地址和数据
*/
for(i = 0; i < 15*DEPTH; i++)
{
switch(MODE)
{
case 1 ://sine
{
temp = (int)( (maxwords/2-1)*sin(2*PI*i/DEPTH)+maxwords/2) ;
break;
}
case 2 ://triangle
{
int k=2*maxwords/DEPTH;
if((i%DEPTH)<DEPTH/2)
{
temp+=k;
}
else temp-=k;
if(temp>maxwords) temp=maxwords-1;
break;
}
case 3 ://sawtooth
{ int k=(maxwords-1)/(DEPTH-1);
temp+=k;
if (temp>=maxwords) temp-=maxwords;
break;
}
case 4 ://square
{
if ((i % DEPTH )<=(DEPTH/2))
temp = 0;
else
temp = maxwords;
break;
}
default:
{
break;
}
}
fprintf (fp, "%x \t : \t %x;\n", i, temp);
}//end for
fprintf (fp, "END;\n");
fclose (fp);
}
}