晓毕8 发表于 2013-9-1 14:20:06

51音乐频谱

单独使用5110可以显示,单独使用FFT()和showbar()函数用串口调试可以,为什么我把showbar()里的数值显示在5110里就不行了,郁闷啊,请教大神#include"STC12C5A60S2.h"
#include"5110.h"
#include"ADDA.h"
#include"math.h"
#defineuchar unsigned char
#defineuintunsigned int
struct compx                                                                         //定义复数结构体
{
        float real;//实部
        float imag;//虚部
};
xdata struct compx s[ 64 ];//数据缓冲区                         //FFT数据缓存放在XDATA空间
struct compx EE(struct compx a1,struct compx b2)         //复数乘法
{
        struct compx b3;
        b3.real=a1.real*b2.real-a1.imag*b2.imag; //实部1乘实部2-
        b3.imag=a1.real*b2.imag+a1.imag*b2.real;
        return(b3);
}
void FFT(struct compx xin[],int N)//64点                                  
{
        int f;
        int m;
        int nv2; //
        int nm1;
        int i;
        int k;
        int j=1;
        int l;
        struct compx v,w,t;
        nv2=N/2;
        f=N;
        for(m=1;(f=f/2)!=1;m++){;}
        nm1=N-1;
        for(i=0;i<nm1;i++)                                           //倒序操作
        {
                if(i<j)
                {
                        t=xin;
                        xin=xin;
                        xin=t;
                }
                k=nv2;                                                     //k为倒序中相应位置的权值
                while(k<j)
                {
                        j=j-k;
                        k=k/2;
                }
                j=j+k;
        }
       
        {
                int le,lei,ip;
                float pi;
                for(l=1;l<=m;l++)
                   {
                le=pow(2,l);                                               //乘方
                    lei=le/2;
                    pi=3.14159265;
                    v.real=1.0;
                v.imag=0.0;
                   w.real=cos(pi/lei);                                         //旋转因子
                    w.imag=-sin(pi/lei);
                   
                for(j=1;j<=lei;j++)                                         //控制蝶形运算的级数
                   {
                        for(i=j-1;i<N;i=i+le)                                         //控制每级蝶形运算的次数
                              {
                                ip=i+lei;
                                       t=EE(xin[ ip ],v);
                                       xin[ ip ].real=xin[ i ].real-t.real;   //蝶形计算
                                       xin[ ip ].imag=xin[ i ].imag-t.imag;
                                       xin[ i ].real=xin[ i ].real+t.real;
                                       xin[ i ].imag=xin[ i ].imag+t.imag;
                              }
                              v=EE(v,w);   
                      }   
                   }
        }
}
void showbar(void)
{
        uchar i,j,dat1,dat2;
        uinttmp1=33,tmp2=33;

        for(i=0;i<32;i+=4)
        {
                float t0=0,t2=0;

                t0=sqrt(pow((s.real+s.real),2)+pow((s.imag+s.imag),2))/2;
                t2=sqrt(pow((s.real+s.real),2)+pow((s.imag+s.imag),2))/2;

                tmp1=(uint)(t0/3);
                tmp2=(uint)(t2/3);

                for(j=2;j>0;j--)//行处理
                {               
                        if (tmp1>=8)
                          {
                                     dat1=7;
                                  tmp1=tmp1-8;
                                }                         //满格
                        elseif(tmp1==0)
                          dat1=20;                                        //空格
                        else
                          {
                                dat1=tmp1-1;
                                tmp1=0;
                                }                                        //1-7格
                                               
                        if (tmp2>=8){dat2=7;tmp2=tmp2-8;}                         //满格
                        elseif(tmp2==0) dat2=20;                                        //空格
                        else{dat2=tmp2-1;tmp2=0;}                                        //1-7格

                        /*UART_Put_Num(tmp1);
                        UART_Send_Str("");
                        UART_Put_Num(tmp2);
                        UART_Send_Enter(); */


                        LCD_write_pinpu(i/2,j-1,dat1); //dat1为0-8之间的数
                        LCD_write_pinpu(i/2+1,j-1,dat2);
                }                        
                                                                     //\\\\\\\\\\这里是显示的部分,重点是确定I(频率),J,dat(幅度) 的值
                                                                        // \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\       \\\\\\\\\\\\///////////////////
                                                                       //\\\\\\\\\\\\\\\\\\\\\\\\       //////////////////////////////////////////////////
        }

}


void delay_ms()
{
int i,j;
for(i=0;i<6000;i++)
    for(j=0;j<200;j++);
}
void main()
{

   int N=64,i;                                                           //变量初始化,64点FFT运算
       float offset;
           LCD_Init();
LCD_clear();
       AD_init();    //A/D转换初始化
   //UART_Init(); //串口初始化
        // UART_Send_Str("串口调试AD值");
        // UART_Send_Enter();
       offset = AD_get(1);

        while(1)
        {       
                     for(i=0;i<N;i++)                                  //采集音频信号
                          {
                                ADC_CONTR=0xC8;                                       //40.96K采样率
                                while(!(ADC_CONTR&0x10));
                                   s.real=(AD_get(1)-offset)/4;//((((int)ADC_DATA-128)/2))*4;
                                   s.imag=0;
                          }
                  FFT(s,N);                                       //调用FFT函数进行变换
                          showbar();                                                        //显示频谱                
        }
/*int i;
LCD_Init();
LCD_clear();
while(1)
{
   for(i=0;i<8;i++)
       {
       LCD_write_pinpu(1,1,i);
       delay_ms();
       }
}        */
}

semonpic 发表于 2013-9-1 20:21:51

顶楼主,谢谢。

成就与价值 发表于 2013-9-1 20:58:30

求楼主分享!
页: [1]
查看完整版本: 51音乐频谱