当前位置:网站首页>Stm32f4 MCU ADC sampling and FFT of ARM-DSP Library
Stm32f4 MCU ADC sampling and FFT of ARM-DSP Library
2022-04-23 04:00:00 【Chenxr32】
The analog signal passes through ADC After sampling, it becomes a digital signal , Digital signals can be used FFT operation , It is easier to analyze the characteristics of the signal in the frequency domain . How will this be used STM32F4 In the process of ADC sampling , And make use of ARMDSP In the database FFT Algorithm to ADC The sampled values are subjected to fast Fourier transform .
I'm using STM32F407VG Single chip microcomputer , Because of the need ADC More sampling values ( Collected 4096 A little bit ), So it's configured STM32 Of ADC and DMA, Use DMA take ADC It is more efficient to transfer the sampling value to memory . When configuring peripherals , Use APB2 Of the clock 2 Frequency division as ADCCLK,ADC The sampling time is 84 individual ADCCLK,Tconv( Total conversion time ) = Sampling time +12 individual ADCCLK, Theoretically ADC The sampling frequency is about 4,375,00Hz. In order to ensure the integrity of the signal , According to Nyquist theorem, the sampling frequency needs to be greater than the highest frequency in the signal 2 times , Therefore, the signal frequency measured by the peripheral configuration method should not be greater than 218,750Hz. The peripheral configuration code is as follows :
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2|RCC_AHB1Periph_GPIOC,ENABLE);
while(DMA_GetCmdStatus(DMA2_Stream0)!=DISABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,DISABLE);
//init GPIO ADC1, channel 14
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure);
DMA_InitStructure.DMA_Channel = DMA_Channel_0;
DMA_InitStructure.DMA_PeripheralBaseAddr = (unsigned int)&(ADC1->DR);
DMA_InitStructure.DMA_Memory0BaseAddr = (unsigned int)&(fft.ADC_ConvertedValue[0]);
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = 4096;
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_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream0,&DMA_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
DMA_ClearFlag(DMA2_Stream0,DMA_IT_TC);
DMA_ITConfig(DMA2_Stream0,DMA_IT_TC,ENABLE);// Enable data stream transmission completion interrupt
DMA_Cmd(DMA2_Stream0,ENABLE);
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;//84M/2 = 42M
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_20Cycles;
ADC_CommonInit(&ADC_CommonInitStructure);
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 1;
ADC_Init(ADC1,&ADC_InitStructure);
ADC_Cmd(ADC1,ENABLE);
ADC_RegularChannelConfig(ADC1,ADC_Channel_14,1,ADC_SampleTime_84Cycles);// Conversion time 84 individual ADC cycle
ADC_SoftwareStartConv(ADC1);
ADC_DMARequestAfterLastTransferCmd(ADC1,ENABLE);
ADC_DMACmd(ADC1,ENABLE);
Next , take ADC The sampling value of is converted to the corresponding voltage value , utilize DSP Library FFT Algorithm FFT operation , Calculate the amplitude frequency characteristics . here , I use base 4 floating-point FFT Algorithm , The base 4 Algorithm biki 2 Our algorithm is faster . The code is as follows :
#define FFT_LENGTH 4096
void FFTTestTask(void *arg)
{
OS_ERR err;
CPU_TS ts;
arm_cfft_radix4_instance_f32 scfft;
int i = 0;
unsigned char str[10];
arm_cfft_radix4_init_f32(&scfft,FFT_LENGTH,0,1);//FFT initialization
while(1)
{
OSTaskSemPend(0,OS_OPT_PEND_BLOCKING,&ts,&err);// Waiting for transmission completion semaphore
for(i=0;i<FFT_LENGTH;i++)
{
fft.fft_input[2*i] = (float)fft.ADC_ConvertedValue[i]*3.3f/4096.0f;// The real part is ADC Sampling value
fft.fft_input[2*i+1] = 0;// The imaginary part is 0
}
arm_cfft_radix4_f32(&scfft,fft.fft_input);//FFT operation
arm_cmplx_mag_f32(fft.fft_input,fft.fft_output,FFT_LENGTH);// Calculate the modulus of each point
for(i=0;i<FFT_LENGTH;i++)
{
sprintf((char*)str,"%.2f\r\n",fft.fft_output[i]);
board.UART4Send(str,strlen((char*)str));// Print the data to the serial port assistant , Convenient for observation
OSTimeDly(1,OS_OPT_TIME_DLY,&err);
}
OSTimeDly(500,OS_OPT_TIME_DLY,&err);
board.ADC1_DMA2Enable();// Restart ADC Conversion and DMA transmission
}
}
In the experiments , I use FPGA and DAC Did DDS Signal generator , The generated waveform function is . The voltage signal is collected by the above method FFT operation , Finally using MATLAB Some amplitude frequency characteristic curves are drawn . The theoretical amplitude of DC component is 6758.4, actual value 7168.2; The theoretical amplitude of the fundamental component is 3379.2, actual value 3454.3.
Please correct the deficiencies .
The code download : STM32F4 ADC sampling FFT Operation test code
版权声明
本文为[Chenxr32]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230356325317.html
边栏推荐
- [mathematical modeling] my mathematical memory
- Xiaohongshu was exposed to layoffs of 20% as a whole, and the internal volume among large factories was also very serious
- VS Studio 修改C語言scanf等報錯
- 【测绘程序设计】坐标反算神器V1.0(附C/C#/VB源程序)
- 【李宏毅2022 机器学习春】hw6_GAN(不懂..)
- ROS series (V): common commands in ROS
- Abstract classes, interfaces and common keywords
- Second kill all interval related problems
- TreeSet课后练习
- 知乎有问题,谁来解答?
猜你喜欢
[AI vision · quick review of NLP natural language processing papers today, issue 29] Mon, 14 Feb 2022
Matlab reads multiple fig graphs and then combines them into one graph (in the form of sub graph)
[AI vision · quick review of robot papers today, issue 30] Thu, 14 APR 2022
【BIM+GIS】ArcGIS Pro2. 8 how to open Revit model, Bim and GIS integration?
Source code and update details of new instance segmentation network panet (path aggregation network for instance segmentation)
Openvino only supports Intel CPUs of generation 6 and above
创下国产手机在海外市场销量最高纪录的小米,重新关注国内市场
Retrieval question answering system baseline
STM32F4单片机ADC采样及ARM-DSP库的FFT
单极性非归零NRZ码、双极性非归零NRZ码、2ASK、2FSK、2PSK、2DPSK及MATLAB仿真
随机推荐
Why is it necessary to divide the variance by 255^2 when adding Gaussian noise using the imnoise function of MATLAB
ROS series (IV): ROS communication mechanism series (2): Service Communication
QT program integration easyplayer RTSP streaming media player screen flicker what is the reason?
C语言常用字符串处理函数
Matlab reads multiple fig graphs and then combines them into one graph (in the form of sub graph)
Cause analysis of incorrect time of AI traffic statistics of Dahua Equipment Development Bank
As a code farmer, what kind of experience is it that a girlfriend can code better than herself?
Detailed explanation on the use of annotation tool via (VGg image annotator) in mask RCNN
Machine translation baseline
Who will answer the question?
中国移动日赚2.85亿很高?其实是5G难带来更多利润,那么钱去哪里了?
Win10 boot VMware virtual machine boot seconds blue screen problem perfect solution
Shopping mall for transportation tools based on PHP
OpenCV----YOLACT实例分割模型推理
Definition, understanding and calculation of significant figures in numerical analysis
【ICCV 2019】MAP-VAE:Multi-Angle Point Cloud-VAE: Unsupervised Feature Learning for 3D Point Clouds..
What if you encounter symbols you don't know in mathematical formulas
Solve the technical problems in seq2seq + attention machine translation
Digital image processing third edition Gonzalez notes Chapter 2
How to introduce opencv into cmake project