编写STM32通过DMA采集三路ADC电压采集程序
时间: 2023-07-29 16:13:31 浏览: 94
以下是一个基本的STM32通过DMA采集三路ADC电压的代码:
```c
#include "stm32f4xx.h"
#define ADC1_DR_ADDRESS ((uint32_t)0x4001204C)
void DMA_Configuration(void);
void ADC_Configuration(void);
void GPIO_Configuration(void);
volatile uint16_t ADCConvertedValues[3];
int main(void)
{
GPIO_Configuration();
ADC_Configuration();
DMA_Configuration();
while (1)
{
//等待DMA传输完成
while (!DMA_GetFlagStatus(DMA2_Stream0, DMA_FLAG_TCIF0));
//将采集到的数据存储到数组中
ADCConvertedValues[0] = (uint16_t)(ADC1ConvertedValue[0]);
ADCConvertedValues[1] = (uint16_t)(ADC1ConvertedValue[1]);
ADCConvertedValues[2] = (uint16_t)(ADC1ConvertedValue[2]);
//重启DMA传输
DMA_Cmd(DMA2_Stream0, ENABLE);
}
}
void DMA_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
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)&ADCConvertedValues;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = 3;
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);
}
void ADC_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
ADC_CommonInit(&ADC_CommonInitStructure);
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 3;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_15Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_15Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 3, ADC_SampleTime_15Cycles);
ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
ADC_DMACmd(ADC1, ENABLE);
ADC_Cmd(ADC1, ENABLE);
ADC_SoftwareStartConv(ADC1);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
```
在这个例子中,我们使用了ADC1和DMA2。其中,ADC1使用了PA0, PA1和PA2三个通道进行采集。DMA2用于将采集到的数据传输到内存中。
在main函数中,我们首先调用了GPIO_Configuration函数来配置GPIO,然后调用了ADC_Configuration函数来配置ADC。其中,ADC的配置包括配置通道、采样时间和DMA相关设置。最后,我们调用了DMA_Configuration函数来配置DMA。
在DMA_Configuration函数中,我们首先开启了DMA时钟,并且设置了DMA的初始化参数。其中,我们指定了DMA的通道为0,传输方向为从外设到内存,传输数据长度为16位,传输模式为循环模式,并且设置了DMA的优先级为高。接下来,我们使用DMA_Init函数初始化DMA,并且启动DMA传输。
在主while循环中,我们等待DMA传输完成,并且将采集到的数据存储到数组中。然后,我们重新启动DMA传输。
需要注意的是,ADC采集的数据是12位的,因此我们需要将其存储到一个16位的变量中。另外,ADC的采样时间需要根据具体的应用进行调整。
阅读全文