基于stm32F1音乐频谱
单片机源程序如下:
- #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 <math.h>
- /* 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_DataNum<NPT){//采样点没有达到所要求的点
- // ADC1->CR2 |= 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;i<NPT/2;i++){
- if((uint8_t)(lBUFMAG[i])>fftHightRedBuf[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;i<NPT/2;i++){
- if((fftHightGreenBuf[i]!=0)&&(GreenStopTime[i]==0)){
- fftHightGreenBuf[i]--;
- }
- }
- }
- #endif
- //红色下移
- if(RedTime>RED_SUB_SPEED){
- RedTime=0;
- for(i=0;i<NPT/2;i++){
- if((fftHightRedBuf[i]!=0)){
- fftHightRedBuf[i]--;
- }
- }
- }
- //绿色点停顿时间减一
- #if DOUBLE_COLOR
- for(i=0;i<NPT/2;i++){
- if(GreenStopTime[i]!=0){
- GreenStopTime[i]--;
- }
- }
- #endif
- TIM_Cmd(TIM2, ENABLE);
- }
- }
- }
- }
-
-
- void TIM2_IRQHandler(void)
- {
- if(TIM_GetITStatus(TIM2,TIM_IT_Update)==SET){
- // TIM2->SR = (uint16_t)~TIM_FLAG_Update;
- TIM_ClearITPendingBit(TIM2,TIM_FLAG_Update); //清中断
- ADC_TimeOutFlag=1;
- }
- }
复制代码
所有资料51hei提供下载:
基于STM32战舰版的音乐频谱.zip
(3.64 MB, 下载次数: 255)
|