#include "stm32f10x.h"
#include <math.h>
#include<stdio.h>
#include "stm32_dsp.h"
#include "table_fft.h"
//#include "sine_data.h"
#include "2sine.h"
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#define PI2 6.28318530717959
#define ADC1_DR_Address ((uint32_t)0x4001244C)
#define NPT 64 /* NPT = No of FFT point*/
#define N 31
extern uint16_t TableFFT[];
int16_t x[N];
long lBUFIN[NPT]; /* Complex input vector */
long lBUFOUT[NPT]; /* Complex output vector */
long lBUFMAG[NPT + NPT/2];/* Magnitude vector */
ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
USART_InitTypeDef USARTInitTypeStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
__IO int16_t ADCConvertedValue[64];
void USART_Configuration(void);
void RCC_Configuration(void);
void GPIO_Configuration(void);
void DMA_Configuration(void);
void ADC_Configuration(void);
void TIM_Configuration(void);
short FIR_filter(int16_t *x,const int16_t *c,unsigned char n);
extern my_fir_filter( int16_t *x,const int16_t *c,u32 n,int *o);
/* Private function prototypes -----------------------------------------------*/
void MyDualSweep(uint32_t freqinc1,uint32_t freqinc2);
void MygSin(long nfill, long Fs, long Freq1, long Freq2, long Ampli);
void powerMag(long nfill, char* strPara);
void onesided(long nfill);
int main(void)
{
int i;
int out_loop;
int output;
int16_t fir_out;
RCC_Configuration();
GPIO_Configuration();
USART_Configuration();
DMA_Configuration();
ADC_Configuration();
//TIM_Configuration();
//while(DMA_GetFlagStatus(DMA1_FLAG_TC1)==0);
for(out_loop=0;out_loop<44;out_loop++)
{
for(i=0;i<31;i++)
{
x[i]=0;
}
for(i=0;i<400;i++)
{
x[0]=sine_data[(out_loop)*400+i];
my_fir_filter(&x[0],&c[0],31,&output);
fir_out=output>>16;
printf("%d \n",fir_out);
}
}
}
void MyDualSweep(uint32_t freqinc1,uint32_t freqinc2)
{
uint32_t freq;
for (freq=40; freq <4000; freq+=freqinc1)
{
MygSin(NPT, 8000, freq, 0, 32767);
//GPIOC->BSRR = GPIO_Pin_7;
cr4_fft_64_stm32(lBUFOUT, lBUFIN, NPT);
// GPIOC->BRR = GPIO_Pin_7;
powerMag(NPT,"2SIDED");
//In_displayWaveform(DISPLAY_RIGHT);
//displayPowerMag(DISPLAY_RIGHT, 9);
// while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_9) == 0x00);
}
for (freq=40; freq <4000; freq+=freqinc2)
{
MygSin(NPT, 8000, freq, 160, 32767/2);
//GPIOC->BSRR = GPIO_Pin_7;
cr4_fft_64_stm32(lBUFOUT, lBUFIN, NPT);
//GPIOC->BRR = GPIO_Pin_7;
powerMag(NPT,"2SIDED");
//In_displayWaveform(DISPLAY_LEFT);
//displayPowerMag(DISPLAY_LEFT, 8);
//while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_9) == 0x00);
}
}
void onesided(long nfill)
{
uint32_t i;
lBUFMAG[0] = lBUFMAG[0];
lBUFMAG[nfill/2] = lBUFMAG[nfill/2];
for (i=1; i < nfill/2; i++)
{
lBUFMAG[i] = lBUFMAG[i] + lBUFMAG[nfill-i];
lBUFMAG[nfill-i] = 0x0;
}
}
/**
* @brief Compute power magnitude of the FFT transform
* @param ill: length of the array holding power mag
* 计算信号的功率谱密度
* : strPara: if set to "1SIDED", removes aliases part of spectrum (not tested)
* @retval : None
*/
void powerMag(long nfill, char* strPara)
{
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] = (uint32_t)(Mag*65536);
}
}
if (strPara == "1SIDED") onesided(nfill);
}
/**
* @brief Produces a combination of two sinewaves as input signal
* @param ill: length of the array holding input signal
* Fs: sampling frequency
* Freq1: frequency of the 1st sinewave
* Freq2: frequency of the 2nd sinewave
* Ampli: scaling factor
* @retval : None
*/
/*void MygSin()
功能:模拟产生正弦信号的离散采样数据
long nfill 采样点数(fft计算点数)
long Fs 采样频率
long Frea1 信号1频率
long Freq2 信号2频率
long Ampli 信号幅度
*/
void MygSin(long nfill, long Fs, long Freq1, long Freq2, long Ampli)
{
uint32_t i;
float fFs, fFreq1, fFreq2, fAmpli;
float fZ,fY;
fFs = (float) Fs;
fFreq1 = (float) Freq1;
fFreq2 = (float) Freq2;
fAmpli = (float) Ampli;
for (i=0; i < nfill; i++)
{
fY = sin(PI2 * i * (fFreq1/fFs)) + sin(PI2 * i * (fFreq2/fFs));
fZ = fAmpli * fY;
lBUFIN[i]= ((short)fZ) << 16 ; /* sine_cosine (cos=0x0) */
}
}
void RCC_Configuration(void)
{
#if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
/* ADCCLK = PCLK2/2 */
RCC_ADCCLKConfig(RCC_PCLK2_Div2);
#else
/* ADCCLK = PCLK2/4 */
RCC_ADCCLKConfig(RCC_PCLK2_Div4);
#endif
/* Enable peripheral clocks ------------------------------------------------*/
/* Enable DMA1 clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
/* Enable ADC1 and GPIOC clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1| RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_USART1, ENABLE);
}
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);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_All;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
void DMA_Configuration(void)
{
/* DMA1 channel1 configuration ----------------------------------------------*/
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ADCConvertedValue;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 64;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
/* Enable DMA1 channel1 */
DMA_Cmd(DMA1_Channel1, ENABLE);
}
void ADC_Configuration(void)
{
/* ADC1 configuration ------------------------------------------------------*/
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1 ;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channel14 configuration */
ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_1Cycles5);
/* Enable ADC1 DMA */
ADC_DMACmd(ADC1, ENABLE);
ADC_DiscModeCmd(ADC1,ENABLE);
ADC_ExternalTrigConvCmd(ADC1,ENABLE);// ADC_ExternalTrigConvConfig(ADC1,ENABLE);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Enable ADC1 reset calibration register */
ADC_ResetCalibration(ADC1);
/