STM32F407 ADC1 DMA 采集完整DEMO
时间: 2023-10-17 17:03:53 浏览: 99
以下是一个完整的基于STM32F407芯片的ADC1 DMA采集Demo程序,你可以参考它来进行开发:
```c
#include "stm32f4xx.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_adc.h"
#include "stm32f4xx_dma.h"
#define ADC1_DR_ADDRESS ((uint32_t)0x4001204C) // 定义ADC1数据寄存器地址
#define ADC1_CHANNEL_NUM 3 // ADC1采集通道数
#define ADC1_SAMPLE_NUM 10 // 每个通道采集次数
__IO uint16_t ADC1ConvertedValues[ADC1_CHANNEL_NUM * ADC1_SAMPLE_NUM]; // 定义存储ADC1采集结果的数组
void RCC_Configuration(void);
void GPIO_Configuration(void);
void ADC1_DMA_Configuration(void);
int main(void)
{
RCC_Configuration(); // 配置系统时钟
GPIO_Configuration(); // 配置GPIO
ADC1_DMA_Configuration(); // 配置ADC1和DMA
while (1)
{
// 等待采集完成
while (DMA_GetFlagStatus(DMA2_Stream0, DMA_FLAG_TCIF0) == RESET);
// 打印采集结果
for (int i = 0; i < ADC1_CHANNEL_NUM * ADC1_SAMPLE_NUM; i++)
{
printf("%d ", ADC1ConvertedValues[i]);
}
printf("\n");
DMA_ClearFlag(DMA2_Stream0, DMA_FLAG_TCIF0); // 清除DMA传输完成标志
DMA_Cmd(DMA2_Stream0, ENABLE); // 重新使能DMA传输
ADC_SoftwareStartConv(ADC1); // 开始新一轮ADC1采集
}
}
void RCC_Configuration(void)
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE); // 使能DMA2时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); // 使能GPIOA时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); // 使能ADC1时钟
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// 配置PA0、PA1、PA2为模拟输入
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);
}
void ADC1_DMA_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
// 配置DMA传输参数
DMA_InitStructure.DMA_Channel = DMA_Channel_0;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_ADDRESS;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADC1ConvertedValues;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = ADC1_CHANNEL_NUM * ADC1_SAMPLE_NUM;
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); // 使能DMA传输
// 配置ADC1参数
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;
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 = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = ADC1_CHANNEL_NUM;
ADC_Init(ADC1, &ADC_InitStructure);
// 配置ADC1通道
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_56Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_56Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 3, ADC_SampleTime_56Cycles);
ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE); // DMA传输完最后一个采样点后自动停止转换
ADC_DMACmd(ADC1, ENABLE); // 使能ADC1 DMA传输
ADC_Cmd(ADC1, ENABLE); // 使能ADC1
ADC_SoftwareStartConv(ADC1); // 开始ADC1转换
}
```
这个程序实现了ADC1采集PA0、PA1、PA2三个通道的模拟信号,并使用DMA将采集结果存储到数组中。在主函数中,等待DMA传输完成后打印采集结果,并重新启动ADC1和DMA进行下一轮采集。
阅读全文