写一个STM32 C语言通过ADC采样计算THD的重新
时间: 2024-03-01 15:49:52 浏览: 84
stm32f103c8t6实现adc采样例程,基于stm32cube
5星 · 资源好评率100%
下面是一个使用 STM32 的 C 语言编写的通过 ADC 采样计算 THD 的示例代码:
```c
#include "stm32f4xx.h"
#include "arm_math.h"
#define ADC1_DR_ADDRESS ((uint32_t)0x4001204C)
void ADC1_Init(void);
void TIM2_Init(void);
void GPIO_Init(void);
void NVIC_Configuration(void);
void Delay(__IO uint32_t nCount);
void CalculateTHD(void);
__IO uint16_t ADC1ConvertedValue[1024];
uint32_t SampleCount = 1024;
uint32_t THD = 0;
float32_t harmonic[51];
float32_t signal[1024];
int main(void)
{
ADC1_Init();
TIM2_Init();
GPIO_Init();
NVIC_Configuration();
while (1)
{
CalculateTHD();
}
}
void ADC1_Init(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
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_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_3Cycles);
ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
DMA_InitStructure.DMA_Channel = DMA_Channel_0;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_ADDRESS;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADC1ConvertedValue;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = SampleCount;
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_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);
DMA_Cmd(DMA2_Stream0, ENABLE);
ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
ADC_Cmd(ADC1, ENABLE);
ADC_DMACmd(ADC1, ENABLE);
ADC_SoftwareStartConv(ADC1);
}
void TIM2_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 999;
TIM_TimeBaseStructure.TIM_Prescaler = 83;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_Cmd(TIM2, ENABLE);
}
void GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOD, &GPIO_InitStructure);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void DMA2_Stream0_IRQHandler(void)
{
GPIO_SetBits(GPIOD, GPIO_Pin_14);
DMA_ClearITPendingBit(DMA2_Stream0, DMA_IT_TC);
}
void Delay(__IO uint32_t nCount)
{
while (nCount--)
{
}
}
void CalculateTHD(void)
{
uint32_t i, j;
float32_t maxValue = 0.0;
float32_t squaredSum = 0.0;
float32_t rmsValue = 0.0;
for (i = 0; i < SampleCount; i++)
{
signal[i] = (float32_t)ADC1ConvertedValue[i] / 4095.0 * 3.3;
}
arm_rms_f32(signal, SampleCount, &rmsValue);
for (j = 1; j <= 50; j++)
{
maxValue = 0.0;
for (i = 0; i < SampleCount; i++)
{
harmonic[j] = harmonic[j] + signal[i] * arm_sin_f32(2.0 * PI * j * i / SampleCount);
}
harmonic[j] = harmonic[j] * 2.0 / SampleCount;
for (i = 0; i < SampleCount; i++)
{
squaredSum = squaredSum + arm_power_f32((signal[i] - harmonic[j] * arm_sin_f32(2.0 * PI * j * i / SampleCount)), 2.0);
}
THD = THD + (uint32_t)(100 * (arm_sqrt_f32(squaredSum / SampleCount) / rmsValue));
}
THD = THD / 50;
Delay(100);
}
```
该示例代码使用了 STM32 的 ADC 和 DMA 功能,采样 1024 个数据点,并通过计算得到了 THD 的值。该函数的实现过程如下:
1. 初始化 ADC 和 DMA,使其能够采样并存储数据。
2. 循环采样数据,计算其 RMS 值和谐波分量。
3. 计算 THD 值。
在计算 THD 值时,我们首先计算了每个谐波分量的值,然后计算了每个数据点与谐波分量之间的差值的平方和。最后,我们计算了 RMS 值并将其用作基准来计算 THD 值。
请注意,该示例代码仅供参考,并且可能需要根据你的具体应用场景进行修改。
阅读全文