STM32寫的頻譜儀的代碼
全部源碼下載:
STM32Spectrum.rar
(356.87 KB, 下載次數: 72)
2017-4-18 10:45 上傳
點擊文件名下載附件
頻譜代碼
預覽:
- /************************************************************
- **注意事項:系統直接集成TFF文件系統
- **版本:V1.0
- **日期:09-6-10
- *************************************************************/
- #include "STM32Lib\\stm32f10x.h"
- #include "hal.h"
- #include <stdio.h>
- #include "TFT\\TFTdriver.h"
- #include "fft\\FFTInc.h"
- //-------------------------------主程序--------------------------------------------------
- unsigned int FFT_N = 128; //初始化128點
- unsigned int ActualBuf[MAX_FFT_N]; //實部
- unsigned int ImagBuf[MAX_FFT_N]; //虛部
- unsigned int FFT_dat_W ; //-- 數據寬度
- unsigned char Distance ; //-- 頻譜條間隔距離
- unsigned char Article_W ; //-- 頻譜條寬度
- unsigned char DOWN_SP ; //-- 頻譜條跌落速度
- unsigned char DOWN_bit ; //-- 頻譜點跌落速度
- unsigned char MAX_bit_del ; //-- 頻譜點跌維持時間
- unsigned int TFT_FFT_H ; //-- 頻譜顯示區高度
- unsigned char FFT_Bit;
- unsigned char show_sav[MAX_FFT_N/2]; //--頻譜條緩存
- unsigned int show_bit[MAX_FFT_N/2]; //--頻譜點緩存
- void FFT_Show_Init( unsigned int n )
- {
- unsigned int i;
- FFT_dat_W = n / 2; //-- 數據寬度
- Distance = 320 / FFT_dat_W ; //-- 頻譜條間隔距離
- switch( n )
- {
- case 16:
- Article_W = Distance - 3; //--
- DOWN_SP = 1; //--
- DOWN_bit = 2; //--
- MAX_bit_del = 45; //--
- FFT_Bit = 4 ; //--
- break;
- case 32:
- Article_W = Distance - 2;
- DOWN_SP = 1 ; //
- DOWN_bit = 2 ; //
- MAX_bit_del = 40 ; //
- FFT_Bit = 5 ;
- break;
- case 64:
- Article_W = Distance - 1;
- DOWN_SP = 1 ; //
- DOWN_bit = 2 ; //
- MAX_bit_del = 35 ; //
- FFT_Bit = 6 ;
- break;
- case 128:
- Article_W = Distance - 1;
- DOWN_SP = 1 ; //
- DOWN_bit = 2 ; //
- MAX_bit_del = 30 ; //
- FFT_Bit = 7 ;
- break;
- case 256:
- Article_W = Distance - 1;
- DOWN_SP = 2 ; //
- DOWN_bit = 3 ; //
- MAX_bit_del = 25 ; //
- FFT_Bit = 8 ;
- break;
- case 512:
- Article_W = Distance;
- DOWN_SP = 3 ; //
- DOWN_bit = 4 ; //
- MAX_bit_del = 20 ; //
- FFT_Bit = 9 ;
- break;
- }
- TFT_FFT_H = 239 - DOWN_bit - 1; //-- 頻譜顯示區高度
- for ( i = 0 ;i < MAX_FFT_N / 2;i ++ )
- {
- show_bit[ i ] = show_sav[ i ] = 0;
- }
- }
- //--------------???--??---------------//
- union{
- struct{
- unsigned B_dat :8; //--
- unsigned B_del :8; //--
- }Bit_bit;
- unsigned int Bit_bits;
- }Bit_dat; //---
-
-
- void FFT_Show( unsigned int *buf )
- {
- int i,j,dat;
-
- for ( i = 1,j = 0;i < FFT_dat_W ;i ++,j += Distance ) //--
- {
- Bit_dat.Bit_bits = show_bit[ i ]; //--
- TFTRectFill( Bit_dat.Bit_bit.B_dat, j , Bit_dat.Bit_bit.B_dat + DOWN_bit + 1,j + Article_W, BLACK,0);//--擦除無效點
- TFTRectFill( show_sav[i], j , show_sav[i] + DOWN_bit, j + Article_W, BLACK,0);//--擦除無效頻譜條
- if ( buf[ i ] > TFT_FFT_H ) dat = TFT_FFT_H; //--限制頻譜條數據在工作區范圍內
- else
- dat = buf[i];
- // buf[i] = 0; //--
- if ( show_sav[i] < dat ) //--有新的頻譜條出現
- {
- TFTRectFill(show_sav[i],j,dat,j + Article_W,RED_BRIGHT,0); //-- 先刷差值
- show_sav[i] = dat; //-- 再保存新值
- }
- if ( Bit_dat.Bit_bit.B_dat < dat ) //--有新的頻譜點出現(不和頻譜條一起刷新)
- {
- Bit_dat.Bit_bit.B_dat = dat; //--刷新頻譜點
- Bit_dat.Bit_bit.B_del = MAX_bit_del; //--頻譜點維持時間刷新
- }
- //---- 刷頻譜點
- TFTRectFill(Bit_dat.Bit_bit.B_dat,j,Bit_dat.Bit_bit.B_dat+1,j + Article_W,GREEN,0);
-
- if ( show_sav[i] >= DOWN_SP ) show_sav[i] -= DOWN_SP; //頻譜條跌落
- else show_sav[i] = 0;
- if ( Bit_dat.Bit_bit.B_del ) Bit_dat.Bit_bit.B_del --;//--頻譜點維持
- else
- if ( Bit_dat.Bit_bit.B_dat - DOWN_bit > show_sav[i] ) //--防止點跌落超過條
- {
- if ( Bit_dat.Bit_bit.B_dat >= DOWN_bit )
- Bit_dat.Bit_bit.B_dat -= DOWN_bit; //頻譜點跌落
- else
- Bit_dat.Bit_bit.B_dat = 0;
- }
- else //--點跌落到條頂部后,再維持
- Bit_dat.Bit_bit.B_del = MAX_bit_del;
-
- show_bit[i] = Bit_dat.Bit_bits; //--保存變量
- }
- }
- unsigned char luv = 0;
- extern unsigned int adcread(void);
- int main(void)
- {
- ChipHalInit();
- ChipOutHalInit();
- Lcd_Init(); //初始化
- FFT_read:
- TFTFill(0,0,240,320,BG_COLOR);
- FFT_Show_Init( FFT_N );
- Int_FFT_TAB( FFT_N );
- for(;;)
- {
- int i;
- int del;
-
- for ( i = 0;i < FFT_N; i++ )
- {
- ActualBuf [i]= adcread(); // 音頻實部
- ImagBuf[i] = 0; // 音頻虛部
- del = 520; // 音頻采樣速率
- while(del--);
-
- }
- fft( (int *)ActualBuf, (int *)ImagBuf, FFT_Bit, FFT_N );//fft
-
- //--平滑處理
- if ( GET_LEFT() ) luv = 1;
- if ( GET_RIGHT() ) luv = 0;
- //--FFT點數切換 +
- if ( GET_UP() )
- {
- if ( FFT_N < MAX_FFT_N ) FFT_N <<= 1;
- else FFT_N = MIN_FFT_N;
- while( GET_UP() );
- goto FFT_read;
- }
- //--FFT點數切換 -
- if ( GET_DOWN() )
- {
- if ( FFT_N > MIN_FFT_N ) FFT_N >>= 1;
- while( GET_DOWN() );
- goto FFT_read;
- }
- if ( luv ) //--簡單的平滑處理
- {
- for ( i = 2;i < FFT_N;i ++ )
- {
- if ( ImagBuf[ i-1 ] > ImagBuf[ i ] ) //-- 前一通道 大于當前通道 ,對當前通道進行提升
- {
- ImagBuf[ i ] += ( ( ImagBuf[ i-1 ] - ImagBuf[ i ]) >> 1 ) ;
- }
- if ( ImagBuf[ i-1 ] < ImagBuf[ i ] ) //-- 當前通道 大于 前一通道,對前一通道進行提升
- {
- ImagBuf[ i-1 ] += ( ( ImagBuf[ i ] - ImagBuf[ i-1 ] ) >> 1);
- }
- }
- }
- FFT_Show( ImagBuf );
-
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
|