你试试这个#include "mm32.h"
#define ADC_CHANNEL 0
#define ADC_SAMPLE_COUNT 10
uint16_t adc_data[ADC_SAMPLE_COUNT];
void ADC_DMA_Init()
{
// 初始化ADC
ADC_InitTypeDef ADC_InitStruct;
ADC_InitStruct.ADC_Mode = ADC_Mode_Continue;
ADC_InitStruct.ADC_ScanMode = DISABLE;
ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStruct.ADC_ContinuousConvMode = ENABLE;
ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
ADC_InitStruct.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStruct.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStruct);
// 配置ADC通道
ADC_RegularChannelConfig(ADC1, ADC_CHANNEL, 1, ADC_Samctl_239_5);
// 初始化DMA
DMA_InitTypeDef DMA_InitStruct;
DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)(&(ADC1->DR));
DMA_InitStruct.DMA_MemoryBaseAddr = (uint32_t)adc_data;
DMA_InitStruct.DMA_Direction = DMA_DIR_PeripheralSRC;
DMA_InitStruct.DMA_BufferSize = ADC_SAMPLE_COUNT;
DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStruct.DMA_Mode = DMA_Mode_Circular;
DMA_InitStruct.DMA_Priority = DMA_Priority_High;
DMA_InitStruct.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStruct);
// 启动DMA
DMA_Cmd(DMA1_Channel1, ENABLE);
// 启动ADC
ADC_Cmd(ADC1, ENABLE);
ADC_DMACmd(ADC1, ENABLE);
}
int main()
{
// 初始化系统时钟等
// 初始化ADC和DMA
ADC_DMA_Init();
while (1)
{
// 等待DMA传输完成
while (!DMA_GetFlagStatus(DMA1_FLAG_TC1))
{
}
// 数据处理
for (int i = 0; i < ADC_SAMPLE_COUNT; i++)
{
// 处理adc_data[i]
}
// 清除DMA传输完成标志
DMA_ClearFlag(DMA1_FLAG_TC1);
}
}
但需要自己改哈,肯定不能直接编译通过的,可以参考一下思路
|