找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3235|回复: 1
打印 上一主题 下一主题
收起左侧

串口向28335发送数据DSP数字信号FFT实现

[复制链接]
跳转到指定楼层
楼主
ID:237028 发表于 2017-10-25 14:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

//###########################################################################
//
// FILE:    Example_2833x_FFT.c
//
// TITLE:   FFT
//
// ASSUMPTIONS:
//
//    This program requires the DSP2833x header files.
//
//    Two different examples are included. Select the example
//    to execute before compiling using the #define statements
//    found at the top of the code.
//
//    As supplied, this project is configured for "boot to SARAM"
//    operation.  The 2833x Boot Mode table is shown below.
//    For information on configuring the boot mode of an eZdsp,
//    please refer to the documentation included with the eZdsp,
//
//       $Boot_Table:
//
//         GPIO87   GPIO86     GPIO85   GPIO84
//          XA15     XA14       XA13     XA12
//           PU       PU         PU       PU
//        ==========================================
//            1        1          1        1    Jump to Flash
//            1        1          1        0    SCI-A boot
//            1        1          0        1    SPI-A boot
//            1        1          0        0    I2C-A boot
//            1        0          1        1    eCAN-A boot
//            1        0          1        0    McBSP-A boot
//            1        0          0        1    Jump to XINTF x16
//            1        0          0        0    Jump to XINTF x32
//            0        1          1        1    Jump to OTP
//            0        1          1        0    Parallel GPIO I/O boot
//            0        1          0        1    Parallel XINTF boot
//            0        1          0        0    Jump to SARAM            <- "boot to SARAM"
//            0        0          1        1    Branch to check boot mode
//            0        0          1        0    Boot to flash, bypass ADC cal
//            0        0          0        1    Boot to SARAM, bypass ADC cal
//            0        0          0        0    Boot to SCI-A, bypass ADC cal
//                                              Boot_Table_End$
//
// DESCRIPTION:
//   FFT变换  数字信号处理
//
//
//
//###########################################################################
// $TI Release: DSP2833x/DSP2823x Header Files V1.20 $
// $Release Date: August 1, 2008 $
//###########################################################################

#include "DSP28x_Project.h"     // Device Headerfile and Examples Include File
#include"math.h"

#define PI 3.1415926
#define SAMPLENUMBER 512
Uint16    *ExRamStart = (Uint16 *)0x100000;
#define FIRNUMBER 25
int16 INPUT[SAMPLENUMBER];
// Prototype statements for functions found within this file.
void scib_echoback_init(void);
void scib_fifo_init(void);
void scib_xmit(int a);
void scib_msg(char *msg);

// Global counts used in this example
Uint16 LoopCount;
Uint16 ErrorCount;

  int16 InputWave[SAMPLENUMBER];

void InitForFFT();
void FFT(float dataR[SAMPLENUMBER],float dataI[SAMPLENUMBER]);

void MakeWave();
//void FFT(float dataR[SAMPLENUMBER],float dataI[SAMPLENUMBER]);
//#pragma DATA_SECTION(INPUT, ".INPUT")
int INPUT[SAMPLENUMBER];
int DATA[SAMPLENUMBER];
int16 DATA2[SAMPLENUMBER];
//int INPUT[SAMPLENUMBER],DATA[SAMPLENUMBER];
float fWaveR[SAMPLENUMBER],fWaveI[SAMPLENUMBER],w[SAMPLENUMBER];
float sin_tab[SAMPLENUMBER],cos_tab[SAMPLENUMBER];




void MakeWave()
{
        int i;
       
        for ( i=0;i<SAMPLENUMBER;i++ )
        {
                INPUT[i]=sin(PI*2*i/SAMPLENUMBER*3)*1024;//+sin(PI*2*i/SAMPLENUMBER*8)*100+sin(PI*2*i/SAMPLENUMBER*60)*1024;// ---1024
        }
}


void main(void)
{


   int i;
    Uint16 ReceivedChar[10];
            Uint16 cnt;
                        Uint16 cntf;
                        int16 sendchar;
                        int n;

// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the DSP2833x_SysCtrl.c file.
   InitSysCtrl();
   InitScibGpio();

// Step 2. Initalize GPIO:
// This example function is found in the DSP2833x_Gpio.c file and
// illustrates how to set the GPIO to it's default state.
   // InitGpio(); Skipped for this example

// Step 3. Clear all interrupts and initialize PIE vector table:
// Disable CPU interrupts
   DINT;

// Initialize PIE control registers to their default state.
// The default state is all PIE interrupts disabled and flags
// are cleared.
// This function is found in the DSP2833x_PieCtrl.c file.
   InitPieCtrl();

// Disable CPU interrupts and clear all CPU interrupt flags:
   IER = 0x0000;
   IFR = 0x0000;

// Initialize the PIE vector table with pointers to the shell Interrupt
// Service Routines (ISR).
// This will populate the entire table, even if the interrupt
// is not used in this example.  This is useful for debug purposes.
// The shell ISR routines are found in DSP2833x_DefaultIsr.c.
// This function is found in DSP2833x_PieVect.c.
   InitPieVectTable();

//*ExRamStart=0x1111;
// Step 4.

    LoopCount = 0;
    ErrorCount = 0;
    cnt=0;
         cntf=0;
        n=0;
//        flag=0;
    scib_fifo_init();           // Initialize the SCI FIFO
    scib_echoback_init();  // Initalize SCI for
     InitForFFT();

    ScibRegs.SCIFFRX.bit.RXFFOVRCLR=1;

for(;;)
{  
   if(cnt==SAMPLENUMBER)
      {   cntf=1;}
    else
           { cntf=0; }

   while(ScibRegs.SCIFFRX.bit.RXFFST !=1) { } // wait for XRDY =1 for empty state


      sendchar= ScibRegs.SCIRXBUF.all;
if(cntf==1)
{        //MakeWave();
        for ( i=0;i<SAMPLENUMBER;i++ )
        {   
                fWaveR[i]=InputWave[i];//INPUT[i];//
                fWaveI[i]=0.0f;
                w[i]=0.0f;
        }
        FFT(fWaveR,fWaveI);
        for ( i=0;i<SAMPLENUMBER;i++ )
        {
                DATA[i]=w[i];
      //  sendchar=DATA[i];
        //        scib_xmit( DATA[i]);
        }
cnt=0;
      for(i=0;i<SAMPLENUMBER;i++)
           { InputWave[i]=0;
                           //DATA[i]=0;
                  }     

}
else
{
  if(sendchar==0x002C)
  {
    if (ReceivedChar[0]==0x002D)
         {
           for(i=1;i<n;i++)
            InputWave[cnt]+=((ReceivedChar[i]-48)*pow(10,n-i-1));
          InputWave[cnt]=0x10000-InputWave[cnt];
          }
     else
        {for(i=0;i<n;i++)
        InputWave[cnt]+=((ReceivedChar[i]-48)*pow(10,n-i-1));
        }
  n=0;
  for(i=0;i<10;i++)
         { ReceivedChar[i]=0;
         }
   cnt++;
        }
  else
  {ReceivedChar[n]=  sendchar;
   n++;

   }

}

scib_xmit(sendchar);  
      LoopCount++;


}

}


/////////////////////////////////////////////////////
void FFT(float dataR[SAMPLENUMBER],float dataI[SAMPLENUMBER])
{
        int x0,x1,x2,x3,x4,x5,x6,x7,xx;  
        int i,j,k,b,r,m,L;  
        float TR,TI,temp;
       
        /********** following code invert sequence ************/
        /********** 实现倒序 ************/   
        for ( i=0;i<SAMPLENUMBER;i++ )
         {   x7=SAMPLENUMBER;   
             x0=x1=x2=x3=x4=x5=x6=0;
         x0=i&0x01;
         x1=(i/2)&0x01;
         x2=(i/4)&0x01;
         x3=(i/8)&0x01;
         x4=(i/16)&0x01;
         x5=(i/32)&0x01;
         x6=(i/64)&0x01;
         xx=x0*x7/2+x1*x7/4+x2*x7/8+x3*x7/16+x4*x7/32+x5*x7/64+x6;   
         dataI[xx]=dataR[i];/*虚部此时暂时没用,暂时做缓存数组*/  
         }
        for ( i=0;i<SAMPLENUMBER;i++ )
        {
                dataR[i]=dataI[i]; dataI[i]=0;
        }
   /**************计算*SAMPLENUMBER=2^(m)中的m******************/
    m=0;
    for (j=SAMPLENUMBER;j>1;j=j/2)
    {m=m+1;}
    //printf("%d\n",m);
  /************** following code FFT *******************/
        for ( L=1;L<=m;L++ )
        { /* for(1) */
                b=1; i=L-1;
                while ( i>0 )
                {
                        b=b*2; i--;
                } /* b= 2^(L-1) */
                for ( j=0;j<=b-1;j++ ) /* for (2) */
                {   r=1; i=m-L;     
                    while ( i>0 )      /* r=2^(m-L)*j,用于计算Wr */   
                     {r=r*2; i--;}   
                      r=r*j;

                        for ( k=j;k<SAMPLENUMBER;k=k+2*b ) /* for (3) */
                        {
                                TR=dataR[k]; TI=dataI[k]; temp=dataR[k+b];
                                dataR[k]=dataR[k]+dataR[k+b]*cos_tab[r]+dataI[k+b]*sin_tab[r];
                                dataI[k]=dataI[k]-dataR[k+b]*sin_tab[r]+dataI[k+b]*cos_tab[r];
                                dataR[k+b]=TR-dataR[k+b]*cos_tab[r]-dataI[k+b]*sin_tab[r];
                                dataI[k+b]=TI+temp*sin_tab[r]-dataI[k+b]*cos_tab[r];
                        } /* END for (3) */
                } /* END for (2) */
        } /* END for (1) */
        for ( i=0;i<SAMPLENUMBER/2;i++ )
        {
                w[i]=sqrt(dataR[i]*dataR[i]+dataI[i]*dataI[i]);
        }   
} /* END FFT */




void InitForFFT()
{
        int i;
       
        for ( i=0;i<SAMPLENUMBER;i++ )
        {
                sin_tab[i]=sin(PI*2*i/SAMPLENUMBER);
                cos_tab[i]=cos(PI*2*i/SAMPLENUMBER);
        }
}


////////////////////////////////////////////////////



void scib_echoback_init()
{
    // Note: Clocks were turned on to the Scib peripheral
    // in the InitSysCtrl() function

        ScibRegs.SCICCR.all =0x0007;   // 1 stop bit,  No loopback
                                   // No parity,8 char bits,
                                   // async mode, idle-line protocol
        ScibRegs.SCICTL1.all =0x0003;  // enable TX, RX, internal SCICLK,
                                   // Disable RX ERR, SLEEP, TXWAKE
        ScibRegs.SCICTL2.all =0x0003;
        ScibRegs.SCICTL2.bit.TXINTENA =1;
        ScibRegs.SCICTL2.bit.RXBKINTENA =1;
        #if (CPU_FRQ_150MHZ)
              ScibRegs.SCIHBAUD    =0x0001;  // 9600 baud @LSPCLK = 37.5MHz.
              ScibRegs.SCILBAUD    =0x00E7;
        #endif
        #if (CPU_FRQ_100MHZ)
      ScibRegs.SCIHBAUD    =0x0001;  // 9600 baud @LSPCLK = 20MHz.
      ScibRegs.SCILBAUD    =0x0044;
        #endif
        ScibRegs.SCICTL1.all =0x0023;  // Relinquish SCI from Reset
}

// Transmit a character from the SCI
void scib_xmit(int a)
{
    while (ScibRegs.SCIFFTX.bit.TXFFST != 0) {}
    ScibRegs.SCITXBUF=a;

}

void scib_msg(char * msg)
{
    int i;
    i = 0;
    while(msg[i] != '\0')
    {
        scib_xmit(msg[i]);
        i++;
    }
}

// Initalize the SCI FIFO
void scib_fifo_init()
{
    ScibRegs.SCIFFTX.all=0xE040;
    ScibRegs.SCIFFRX.all=0x204f;
    ScibRegs.SCIFFCT.all=0x0;

}




//===========================================================================
// No more.
//===========================================================================


serialFFT.rar

3.37 MB, 下载次数: 26, 下载积分: 黑币 -5

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:380348 发表于 2018-7-28 22:33 | 只看该作者
楼主这个是验证过的?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表