工程中DSP代码片断

// 主程序

#include 
" LF2407.H "
#include 
" math.h "
#include 
" verable.h "       // 定义用到的各个全局变量
#include  " inicial.c "      // 系统初始化,CAP6捕获初始化,AD采样初始化,SCI串口通信初始化
#include  " dataprocess.c "       // 处理AD采样的到的数据,以便于用串口与上位机进行通信
#include  " send_rece.c "       // 串口的收发中断服务程序子函数
#include  " interrupt.c "       // 各种中断服务程序,包括AD,串口,捕获,phantom
#include  " fft_table.c "      // 定义fft中运用到的sin和cos表     

#define  wsgr portffff      // 0xFFFF(I/O space) is address of register WSGR
ioport 
uint  portffff;

void  delay( long   int  a)
{
    
long int i;
    
for(i=0;i<a;i++);
}

void  Feed_Dog()
{
  
*WDKEY=0x0055;
  
*WDKEY=0x00AA;
}

void  main( void )
{   
    
int i=0
    
int j;  

    disable();    
//禁止所有可屏蔽的中断
    initial();     //系统初始化2 
    Feed_Dog();//edit by liuwei
    wsgr=0x100;    //bus visibility off;IO space 4 wait states;both data and program space 0 wait states
      ADINITIAL();     //AD采样初始化 
      T3initial();     //T3初始化,用来作为发送最后一位的延时,在发送最后一位时,打开T3计时器,周期中断时将485控制线置位接收状态。
    cap1_initial();      //CAP1捕获初始化                    
    cap4_initial();      //CAP4捕获初始化                    
    RS232();    //SCI模块初始化
    while(1)
    
{   
        
while(adcounter<64/*没有采到64点前,等待*/
        
{};
        
if(adcounter==64)   /*采满64点,开始fft计算*/
        
{  
            adcounter
=0;
            
            
//后两行程序用来调试程序时,利用T4的数据寄存器看fft算法和数据处理所需时间
        
//    *T4CNT=0x00; 
        
//     *T4CON=*T4CON|0x0040;         
            
            
            
if(Vcenterpreselect[0]>2)    //首先判断是否第2级已经饱和,如果第2级饱和,那么选择第1级
            {   
                Vcenter_choice
=0;
            }

            
else if (Vcenterpreselect[1]>2)    //如果第2级没有饱和而第3级饱和,那么选择第2级
            {
                Vcenter_choice
=1;
            }

            
else                //如果第2级第3级都没有饱和,那么选择第3级
            {
                Vcenter_choice
=2;
            }
  
            Vcenterpreselect[
0]=0;
            Vcenterpreselect[
1]=0;

                 
for(i=0;i<64;i++)
            
{
                dataR[
0][i]=dataC[Vcenter_choice][i];
            }
    
        
            
for(i=0;i<64;i++)     //最终有6路信号参与FFT变换,这6路信号都放在2维数组dataR[8][64]中的前6行中    
            {   
                
for(j=1;j<6;j++)
                
{
                    dataR[j][i]
=dataC[j+2][i];
                }

            }
 
            
for(j=0;j<6;j++)
            
{
                FR[j]
=0;
                FI[j]
=0;    
            }

            
            
for(j=0;j<6;j++)
            
{
                FR[j]
=(dataR[j][1]+dataR[j][63]-dataR[j][31]-dataR[j][33])*cos_tab[1];
                FR[j]
+=(dataR[j][2]+dataR[j][62]-dataR[j][30]-dataR[j][34])*cos_tab[2];                                                                                                                          
                FR[j]
+=(dataR[j][3]+dataR[j][61]-dataR[j][29]-dataR[j][35])*cos_tab[3];
                FR[j]
+=(dataR[j][4]+dataR[j][60]-dataR[j][28]-dataR[j][36])*cos_tab[4];
                FR[j]
+=(dataR[j][5]+dataR[j][59]-dataR[j][27]-dataR[j][37])*cos_tab[5];
                FR[j]
+=(dataR[j][6]+dataR[j][58]-dataR[j][26]-dataR[j][38])*cos_tab[6];
                FR[j]
+=(dataR[j][7]+dataR[j][57]-dataR[j][25]-dataR[j][39])*cos_tab[7];
                FR[j]
+=(dataR[j][8]+dataR[j][56]-dataR[j][24]-dataR[j][40])*cos_tab[8];
                FR[j]
+=(dataR[j][9]+dataR[j][55]-dataR[j][23]-dataR[j][41])*cos_tab[9];
                FR[j]
+=(dataR[j][10]+dataR[j][54]-dataR[j][22]-dataR[j][42])*cos_tab[10];
                FR[j]
+=(dataR[j][11]+dataR[j][53]-dataR[j][21]-dataR[j][43])*cos_tab[11];
                FR[j]
+=(dataR[j][12]+dataR[j][52]-dataR[j][20]-dataR[j][44])*cos_tab[12];
                FR[j]
+=(dataR[j][13]+dataR[j][51]-dataR[j][19]-dataR[j][45])*cos_tab[13];
                FR[j]
+=(dataR[j][14]+dataR[j][50]-dataR[j][18]-dataR[j][46])*cos_tab[14];
                FR[j]
+=(dataR[j][15]+dataR[j][49]-dataR[j][17]-dataR[j][47])*cos_tab[15];
                FR[j]
+=(dataR[j][0]-dataR[j][32])*cos_tab[0];
                      
                FI[j]
=(dataR[j][1]+dataR[j][31]-dataR[j][33]-dataR[j][63])*sin_tab[1];
                FI[j]
+=(dataR[j][2]+dataR[j][30]-dataR[j][34]-dataR[j][62])*sin_tab[2];
                FI[j]
+=(dataR[j][3]+dataR[j][29]-dataR[j][35]-dataR[j][61])*sin_tab[3];
                FI[j]
+=(dataR[j][4]+dataR[j][28]-dataR[j][36]-dataR[j][60])*sin_tab[4];
                FI[j]
+=(dataR[j][5]+dataR[j][27]-dataR[j][37]-dataR[j][59])*sin_tab[5];
                FI[j]
+=(dataR[j][6]+dataR[j][26]-dataR[j][38]-dataR[j][58])*sin_tab[6];
                FI[j]
+=(dataR[j][7]+dataR[j][25]-dataR[j][39]-dataR[j][57])*sin_tab[7];
                FI[j]
+=(dataR[j][8]+dataR[j][24]-dataR[j][40]-dataR[j][56])*sin_tab[8];
                FI[j]
+=(dataR[j][9]+dataR[j][23]-dataR[j][41]-dataR[j][55])*sin_tab[9];
                FI[j]
+=(dataR[j][10]+dataR[j][22]-dataR[j][42]-dataR[j][54])*sin_tab[10];
                FI[j]
+=(dataR[j][11]+dataR[j][21]-dataR[j][43]-dataR[j][53])*sin_tab[11];
                FI[j]
+=(dataR[j][12]+dataR[j][20]-dataR[j][44]-dataR[j][52])*sin_tab[12];
                FI[j]
+=(dataR[j][13]+dataR[j][19]-dataR[j][45]-dataR[j][51])*sin_tab[13];
                FI[j]
+=(dataR[j][14]+dataR[j][18]-dataR[j][46]-dataR[j][50])*sin_tab[14];
                FI[j]
+=(dataR[j][15]+dataR[j][17]-dataR[j][47]-dataR[j][49])*sin_tab[15];
                FI[j]
+=(dataR[j][16]-dataR[j][48])*sin_tab[16];
                      
                
//因为sin,cos中都抬高了2^14倍,这里要再变回来因为FR[j]的数据类型是32bit long型,而后面传输是8*2 int型          
                FR[j]=FR[j]>>14;
                FI[j]
=FI[j]>>14;         

                
//算取相位,范围是0~360度,下面是把FR作为虚轴,FI作为实轴来计算的!!!!!!!!                        
                tempA=FR[j]*FR[j];
                tempB
=FI[j]*FI[j];
                tempC
=(long)(sqrt(tempA+tempB)+0.5);
                tempD
=((float)FR[j])/((float)tempC);
                amplitude[j]
=tempC;
                
                
if(tempD>1.0)
                
{
                    tempD
=1.0;
                }
                 
                
if(tempD<-1.0)
                
{
                    tempD
=-1.0;
                }

    
                tempE
=asin(tempD);
                tempE
=tempE*57.3;
                        
                Phrase[j]
=tempE;                    
                Phrase[j]
=Phrase[j]*100
                
if(FI[j]<0)
                
{                               
                    Phrase[j]
=18000-Phrase[j];
                }

                
else
                
{
                    Phrase[j]
=Phrase[j];
                }
 
                
if(Phrase[j]<0)
                
{
                    Phrase[j]
=Phrase[j]+36000;
                }
     
            }
             
            
            
if (time_ad==0)    /*ad 采样次数,奇偶次分别用不同的数组 */
            
{  
                dataprocess1();
                time_ad
=1//数据处理后放在datagrone[]中,datagrone[]中的数据更新完毕之后time_ad才会=1 
            
            }

            
else
            
{
                dataprocess2();
                time_ad
=0;       //数据处理后放在datagrtwo[]中,datagrtwo[]中的数据更新完毕之后time_ad才会=0
               
            }
 
            timer3
=timer3+1;//对主函数中FFT计算的次数进行计数
        }

    }
        
}

这是我的一个工程里面DSP芯片的主程序,其他都很简单,主要用到了一个蝴蝶算法。

你可能感兴趣的:(代码)