基于STM32芯片的128点FFT.doc_第1页
基于STM32芯片的128点FFT.doc_第2页
基于STM32芯片的128点FFT.doc_第3页
基于STM32芯片的128点FFT.doc_第4页
基于STM32芯片的128点FFT.doc_第5页
已阅读5页,还剩30页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Main函数/* (C) COPYRIGHT 2008 STMicroelectronics * File Name : main.c* Author : MCD Application Team* Version : V2.0.1* Date : 06/13/2008* Description : Main program body* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.*/* Includes -*/#include stm32f10x_lib.h#include stdio.h#include math.h#include stm32_dsp.h/* Private typedef -*/* Private define -*/#define ADC1_DR_Address (u32)0x4001244C)#define USART1_DR_Base (u32)0x40013804)#define LEN 128/* Private macro -*/* Private variables -*/ErrorStatus HSEStartUpStatus; /* Private function prototypes -*/void RCC_Configuration(void);void GPIO_Configuration(void);void NVIC_Configuration(void);void TIM_BaseConfiguration(void);void DMA_Configuration(void); void USART_Configuration(void);void ADC_Configuration(void);void ADC_ALL_Init(void);void FFT_Configuration(double *pr,double *pi,int n,int k,double*fr,double*fi,int l,int il);void FFT_solve(void);void Delay(u32 counter);/* Private functions -*/u16 data_buffLEN;u16 px_buffLEN;u16 pz_buffLEN;double x128;double pr128;double pi128;double fr128;double fi128;double mo128;/int *px = (int*)0x1f00;/int *pz = (int*)0x1f80;int xm,zm,i,t = 0;u8 Flag = 0;/* Function Name : main* Description : Main program* Input : None* Output : None* Return : None*/int main(void)#ifdef DEBUG debug();#endif /* System clocks configuration -*/ RCC_Configuration(); /* NVIC configuration -*/ NVIC_Configuration(); /* GPIO configuration -*/ GPIO_Configuration(); /* ADC1 configuration -*/ ADC_Configuration(); /*USART Configuration-*/ USART_Configuration(); ADC_ALL_Init(); while (1) FFT_solve(); /* Function Name : RCC_Configuration* Description : Configures the different system clocks.* Input : None* Output : None* Return : None*/void RCC_Configuration(void) /* RCC system reset(for debug purpose) */ RCC_DeInit(); /* Enable HSE */ RCC_HSEConfig(RCC_HSE_ON); /* Wait till HSE is ready */ HSEStartUpStatus = RCC_WaitForHSEStartUp(); if(HSEStartUpStatus = SUCCESS) /* Enable Prefetch Buffer */ FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); /* Flash 2 wait state */ FLASH_SetLatency(FLASH_Latency_2); /* HCLK = SYSCLK */ RCC_HCLKConfig(RCC_SYSCLK_Div1); /* PCLK2 = HCLK */ RCC_PCLK2Config(RCC_HCLK_Div1); /* PCLK1 = HCLK/2 */ RCC_PCLK1Config(RCC_HCLK_Div2); /* ADCCLK = PCLK2/4 */ RCC_ADCCLKConfig(RCC_PCLK2_Div4); /* PLLCLK = 8MHz * 7 = 56 MHz */ RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_7); /* Enable PLL */ RCC_PLLCmd(ENABLE); /* Wait till PLL is ready */ while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) = RESET) /* Select PLL as system clock source */ RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); /* Wait till PLL is used as system clock source */ while(RCC_GetSYSCLKSource() != 0x08) /* Enable peripheral clocks -*/ /* Enable DMA1 clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); /* Enable USART2 clock */ RCC_APB1PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); /*Enable TIM2 clock*/ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); /* Enable ADC1 and GPIOC clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOA, ENABLE);/* Function Name : GPIO_Configuration* Description : Configures the different GPIO ports.* Input : None* Output : None* Return : None*/void GPIO_Configuration(void) GPIO_InitTypeDef GPIO_InitStructure; /* Configure PC.04 (ADC Channel14) as analog input -*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOC, &GPIO_InitStructure); /* Configure PA.03 (ADC Channel14) as analog input -*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure USART1 Rx (PA.10) as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure USART1 Tx (PA.09) as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure);/* Function Name : NVIC_Configuration* Description : Configures Vector Table base location.* Input : None* Output : None* Return : None*/void NVIC_Configuration(void) NVIC_InitTypeDef NVIC_InitStructure;#ifdef VECT_TAB_RAM /* Set the Vector Table base location at 0x20000000 */ NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); #else /* VECT_TAB_FLASH */ /* Set the Vector Table base location at 0x08000000 */ NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); #endif /* Enable the TIM2 gloabal Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Enable the ADC1_EOC Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = ADC1_2_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);/* Function Name : TIM_BaseConfiguration* Description : Configures Vector Table base location.* Input : None* Output : None* Return : None*/void TIM_BaseConfiguration(void) TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 65535; TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); /*Enable TIM2*/ TIM_Cmd(TIM2,ENABLE); /* TIM IT enable */ TIM_ITConfig(TIM2, TIM_IT_Update , ENABLE);/* Function Name : ADC_Configuration* Description : Configures Vector Table base location.* Input : None* Output : None* Return : None*/void ADC_Configuration(void) ADC_InitTypeDef ADC_InitStructure; ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; /*工作在独立模式*/ ADC_InitStructure.ADC_ScanConvMode = ENABLE; /*规定了模数转换工作在扫描模式(多通道)还是单次(单通道)模式*/ ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; /*规定了模数转换工作在连续还是单次模式*/ ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; /*转换由软件而不是外部触发启动*/ ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; /*规定了ADC数据向左边对齐还是向右边对齐*/ ADC_InitStructure.ADC_NbrOfChannel = 1; /*规定了顺序进行规则转换的ADC通道的数目*/ ADC_Init(ADC1, &ADC_InitStructure); void ADC_ALL_Init(void) /* ADC1 regular channel14 configuration */ ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_7Cycles5); /*设置指定ADC的规则组通道,设置它们的转化顺序和采样时间*/ /* Enable ADC1 EOC interrupts */ ADC_ITConfig(ADC1, ADC_IT_EOC , ENABLE); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /* Enable ADC1 reset calibaration register */ ADC_ResetCalibration(ADC1); /*重置指定的ADC的校准寄存器*/ /* Check the end of ADC1 reset calibration register */ while(ADC_GetResetCalibrationStatus(ADC1);/*获取ADC重置校准寄存器的状态*/ /* Start ADC1 calibaration */ ADC_StartCalibration(ADC1); /* Check the end of ADC1 calibration */ while(ADC_GetCalibrationStatus(ADC1); /* Start ADC1 Software Conversion */ ADC_SoftwareStartConvCmd(ADC1, ENABLE);/*使能指定的ADC的软件转换启动功能*/* Function Name : USART_Configuration* Description : Configures Vector Table base location.* Input : None* Output : None* Return : None*/void USART_Configuration(void) USART_InitTypeDef USART_InitStructure; USART_InitStructure.USART_BaudRate = 9600; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; /* Configure USART1 */ USART_Init(USART1, &USART_InitStructure); /*Enable USART1 */ USART_Cmd(USART1, ENABLE);/* Function Name : FFT_Configuration* Description : * Input : None* Output : None* Return : None*/void FFT_Configuration(double *pr,double *pi,int n,int k,double*fr,double*fi,int l,int il)/int n,k,l,il; double pr,pi,fr,fi;/*pr(实部),pi(虚部),n(点数),k(阶数)*/ int it,m,is,i,j,nv,l0; double p,q,s,vr,vi,poddr,poddi; /*雷德(Rader)算法,实现数据的倒位序排列-*/ for (it=0; it=n-1; it+) m=it; is=0; for (i=0; i=k-1; i+) j=m/2; is=2*is+(m-2*j); m=j; frit=pris; fiit=piis; pr0=1.0; pi0=0.0; p=6.283185306/(1.0*n); pr1=cos(p); pi1=-sin(p); if (l!=0) pi1=-pi1; for (i=2; i=n-1; i+) p=pri-1*pr1; q=pii-1*pi1; s=(pri-1+pii-1)*(pr1+pi1); pri=p-q; pii=s-p-q; for (it=0; it=0; l0-) m=m/2; nv=2*nv; for (it=0; it=(m-1)*nv; it=it+nv) for (j=0; j=(nv/2)-1; j+) p=prm*j*frit+j+nv/2; q=pim*j*fiit+j+nv/2; s=prm*j+pim*j; s=s*(frit+j+nv/2+fiit+j+nv/2); poddr=p-q; poddi=s-p-q; frit+j+nv/2=frit+j-poddr; fiit+j+nv/2=fiit+j-poddi; frit+j=frit+j+poddr; fiit+j=fiit+j+poddi; if (l!=0) for (i=0; i=n-1; i+) fri=fri/(1.0*n); fii=fii/(1.0*n); if (il!=0) for (i=0; i=n-1; i+) pri=sqrt(fri*fri+fii*fii); if (fabs(fri)0) pii=90.0; else pii=-90.0; else pii=atan(fii/fri)*360.0/6.283185306; /* Function Name : FFT_solve* Description : * Input : None* Output : None* Return : None*/void FFT_solve(void) if(Flag) /*寄存器接收满后Flag = 1,关闭ADC中断,进行FFT运算-*/ ADC_Cmd(ADC1, DISABLE); /px = (int*)0x1f00; /*转换存储位置-*/ for(i=0;i128;i+) /*px=data_buffi; /px+; px_buffi = data_buffi; /px = (int*)0x1f00; /*将ADC采来的数据转换为实际的幅度值-*/ for (i=0; i=128; i+) /xm=*px; xm = px_buffi; xi=xm/32768.0; pri=xi;/*pr数组存放函数点的实部-*/ pii=0; /*pi数组存放函数点的虚部-*/ /px+; /*进行FFT运算-*/ FFT_Configuration(pr,pi,128,7,fr,fi,0,1); /pz = (int*)0x1f80; /*将最终计算出的值放入 pz 中-*/ for (i=0;i 0;counter-);#ifdef DEBUG/* Function Name : assert_failed* Description : Reports the name of the source file and the source line number* where the assert_param error has occurred.* Input : - file: pointer to the source file name* - line: assert_param error line source number* Output : None* Return : None*/void assert_failed(u8* file, u32 line) /* User can add his own implementation to report the file name and line number, ex: printf(Wrong parameters value: file %s on line %drn, file, line) */ /* Infinite loop */ while (1) #endif/* (C) COPYRIGHT 2008 STMicroelectronics *END OF FILE*/stm32f10x_it.c/* (C) COPYRIGHT 2008 STMicroelectronics * File Name : stm32f10x_it.c* Author : MCD Application Team* Version : V2.0.1* Date : 06/13/2008* Description : Main Interrupt Service Routines.* This file provides template for all exceptions handler* and peripherals interrupt service routine.* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE* CONTENT OF SUCH F

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论