【文件属性】:
文件名称:STM32音乐频谱分析仪
文件大小:3.64MB
文件格式:ZIP
更新时间:2018-02-16 09:26:57
音乐频谱
Main:
#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "lcd.h"
#include "usart.h"
#include "adc.h"
#include "stm32_dsp.h"
#include
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define DOUBLE_COLOR 1 //ÊÇ·ñΪ˫ɫÆÁ£¬ÈôΪ˫ɫÆÁÔò¸ÄΪ1
#define NPT 64 //FFT²ÉÑùµãÊý
#define GREEN_STOP_TIME 15 //ÂÌÉ«µã¶¥¶ËÍ£¶Ùʱ¼ä£¬ÖµÔ½´óʱ¼äÔ½³¤
#define GREEN_SUB_SPEED 100 //ÂÌÉ«µãÏÂÒÆËٶȣ¬ÖµÔ½´óËÙ¶ÈÔ½Âý
#define RED_SUB_SPEED 50 //ºìɫƵÖùÏòÏÂËõ¶ÌËٶȣ¬ÖµÔ½´óËÙ¶ÈÔ½Âý
uint32_t ADC_DataNum=0; //ADC²ÉÑùµãÊý
uint32_t RedTime=0; //ºìÉ«µãÏÂÒÆʱ¼ä±äÁ¿
#if DOUBLE_COLOR
uint32_t GreenTime=0; //ÂÌÉ«µãÏÂÒÆʱ¼ä±äÁ¿
uint32_t GreenStopTime[32]={0}; //ÂÌÉ«µã¶¥¶ËÍ£¶Ùʱ¼äÊý¾Ý
#endif
volatile uint8_t ADC_TimeOutFlag=1; //ADC¶¨Ê±²ÉÑùʱ¼äµ½±êÖ¾
extern __IO uint16_t ADCConvertedValue; //ADC²ÉÑùÖµ
extern int LCD_COLOR;
long lBUFMAG[NPT+NPT/2]; //´æ´¢ÇóÄ£ºóµÄÊý¾Ý
long lBUFOUT[NPT];//FFTÊä³öÐòÁÐ
long lBUFIN[NPT];//FFTÊäÈëϵÁÐ
uint8_t fftHightRedBuf[NPT/2]={0}; //ºìɫƵÖù¸ß¶ÈÊý×é
uint8_t DisplayRedDataBuf[32*8]={0}; //ºìÉ«ÏÔʾ»º³åÇø
#if DOUBLE_COLOR
uint8_t fftHightGreenBuf[NPT/2]={0}; //ÂÌɫƵµã¸ß¶ÈÊý×é
uint8_t DisplayGreenDataBuf[32*8]={0}; //ÂÌÉ«ÏÔʾ»º³åÇø
#endif
u16 color_tab[16]={DARKBLUE,BLUE,LIGHTBLUE,GREEN,LIGHTGREEN,RED,BRED,BRRED,BLACK,YELLOW,CYAN,MAGENTA,GRAYBLUE,LGRAYBLUE,BROWN,LGRAY};
void music_fft_main(uint8_t *RedNewHeight,uint8_t *GreenNewHeight)
{
int BarWidth = 8;
int i=0;
int j=0;
static uint8_t RedOldHeight[32] = {0};
static uint8_t GreenOldHeight[32] = {0};
for(i=0;i<32;i++)
{
//Çå³ý֮ǰµÄÂÌÉ«·½¿é
//LCD_COLOR = LCD_COLOR_BLACK;
LCD_Fill(GreenOldHeight[i],(BarWidth+2)*i,GreenOldHeight[i]+3,(BarWidth+2)*i+BarWidth,WHITE);
//ÏÔʾµ±Ç°µÄÂÌÉ«·½¿é
LCD_Fill(GreenNewHeight[i],(BarWidth+2)*i,GreenNewHeight[i]+3,(BarWidth+2)*i+BarWidth,color_tab[16-j]);
//ÏÔʾºìÉ«Öù
if(RedNewHeight[i]>RedOldHeight[i]){//Èç¹ûµ±Ç°µÄÂÌÉ«Öù×Ӹ߶ȱÈ֮ǰµÄ´óÔò²¹ÆëÂÌÉ«Öù×Ó
LCD_Fill(RedOldHeight[i],(BarWidth+2)*i,RedNewHeight[i],(BarWidth+2)*i+BarWidth,color_tab[j]);
}else{//Èç¹ûµ±Ç°ÏÔʾµÄÂÌÉ«Öù×Ӹ߶ÈСÓÚ֮ǰµÄÖù×ÓÔòÐèÒª½«¶àÓàµÄÂÌÉ«Öù×ÓÓñ³¾°É«Ìî³ä
LCD_Fill(RedNewHeight[i],(BarWidth+2)*i,RedOldHeight[i],(BarWidth+2)*i+BarWidth,WHITE);
}
//½«ÐÂÊý¾Ý±£´æ
RedOldHeight[i] = RedNewHeight[i];
GreenOldHeight[i] = GreenNewHeight[i];
if(j>=15)
j=0;
j++;
}
}
void powerMag(long nfill)
{ int32_t lX,lY;
uint32_t i;
for (i=0; i < nfill; i++)
{
lX= (lBUFOUT[i]<<16)>>16; /* sine_cosine --> cos */
lY= (lBUFOUT[i] >> 16); /* sine_cosine --> sin */
{
float X= 64*((float)lX)/32768;
float Y = 64*((float)lY)/32768;
float Mag = sqrt(X*X+ Y*Y)/nfill; // ÏÈƽ·½ºÍ,ÔÙ¿ª·½
lBUFMAG[i] = (long)(Mag*65536);
}
}
}
int main(void)
{uint32_t i=0;
delay_init(); //ÑÓʱº¯Êý³õʼ»¯
NVIC_Configuration(); //ÉèÖÃNVICÖжϷÖ×é2:2λÇÀÕ¼ÓÅÏȼ¶£¬2λÏìÓ¦ÓÅÏȼ¶
uart_init(9600); //´®¿Ú³õʼ»¯Îª9600
LED_Init(); //LED¶Ë¿Ú³õʼ»¯
TIM2_Configuration();
TIM2_NVIC_Configuration();
FFT_RCC_Configuration();
FFT_GPIO_Configuration();
FFT_DMA_Init();
FFT_ADC_Init();
LCD_Init();
// BACK_COLOR=BLACK;
TIM_Cmd(TIM2, ENABLE);
ADC_SoftwareStartConvCmd(ADC1, DISABLE);
while(1)
{
if(ADC_TimeOutFlag){
#if DOUBLE_COLOR
GreenTime++;
#endif
RedTime++;
ADC_TimeOutFlag=0;
if(ADC_DataNumCR2 |= 0x00500000;//
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while(!DMA_GetFlagStatus(DMA1_FLAG_TC1));
/* Clear channel1 transfer complete flag */
DMA_ClearFlag(DMA1_FLAG_TC1);
// ADC1->CR2 &= 0xFFAFFFFF;//
ADC_SoftwareStartConvCmd(ADC1, DISABLE);
lBUFIN[ADC_DataNum]=ADCConvertedValue<<16;
ADC_DataNum++;
}else{
TIM_Cmd(TIM2, DISABLE);
ADC_DataNum=0;
cr4_fft_64_stm32(lBUFOUT,lBUFIN,NPT);//µ÷ÓÃSTM32µÄDSP¿â×÷FFT±ä»»
powerMag(NPT);//¼ÆËãƵµã·ùÖµ
//¸üкìÉ«µãµÄ¸ß¶È
for(i=0;ifftHightRedBuf[i]){
fftHightRedBuf[i]=(lBUFMAG[i]);
}
#if DOUBLE_COLOR
//Ë¢ÐÂÂÌÉ«µã¸ß¶È
if(fftHightRedBuf[i]>=fftHightGreenBuf[i]){
fftHightGreenBuf[i]=fftHightRedBuf[i];
GreenStopTime[i]=GREEN_STOP_TIME;//Â̵ãÍ£¶Ùʱ¼ä
if(fftHightRedBuf[i]>=235){
fftHightGreenBuf[i]=235;
fftHightRedBuf[i]=235;
}
}
#else
if(fftHightRedBuf[i]>=239){
fftHightRedBuf[i]=239;
}
#endif
}
//ÏÔʾºìÉ«Öù×Ó
music_fft_main(fftHightRedBuf,fftHightGreenBuf);
//ÏÔʾÂÌÉ«µã
#if DOUBLE_COLOR
//ÂÌÉ«µãÏÂÒÆ
if((GreenTime>GREEN_SUB_SPEED)){ //ÂÌÉ«µãϽµ¼ä¸ôʱ¼ä
GreenTime=0;
for(i=0;iRED_SUB_SPEED){
RedTime=0;
for(i=0;iSR = (uint16_t)~TIM_FLAG_Update;
TIM_ClearITPendingBit(TIM2,TIM_FLAG_Update); //ÇåÖжÏ
ADC_TimeOutFlag=1;
}
}