1. DMA外设基地址(DMA_MemoryBaseAddr),当只需要读取一个通道的值时,设置成某一自定义变量的地址;当需要读取多个通道的值时,设成某一自定义数组的地址。地址都是32位——(uint32_t)。
如:数组:
__IO uint16_t ADC_RegularConvertedValueTab[3];
DMA_InitStructure.DMA_MemoryBaseAddr= (uint32_t)ADC_RegularConvertedValueTab;
2. 多通道读取时(DMA基地址设置成数组地址),DMA_MemoryInc(用来设定内存地址寄存器递增与否)需要使能。
3. DMA传送ADC采样值到定义的数组或者变量,如果需要得到采样值,不需要再使用ADC_GetConversionValue(ADCx);直接用数组或变量转化即可。
如:
__IO uint16_t ADC_RegularConvertedValueTab[3];
adc_front=((float)(ADC_RegularConvertedValueTab[0])*3.3/4096); adc_left=((float)(ADC_RegularConvertedValueTab[1])*3.3/4096);
adc_right=((float)(ADC_RegularConvertedValueTab[2])*3.3/4096);
4. ADC_ExternalTrigConv定义了使用外部触发来启动规则通道的模数转换。如果设置成ADC_ExternalTrigConv_None,就是由软件触发,即ADC采样周期来决定触发采样,比较常见。
5. DMA_DIR_PeripheralDST 外设作为数据传输的目的地;
DMA_DIR_PeripheralSRC 外设作为数据传输的来源。
6. DMA_BufferSize用以定义指定DMA通道的DMA缓存的大小,设置成每一次循环中需要传输的数据个数。通常和数组大小设置成相同。
7. 软件触发ADC采样时要开启
ADC_SoftwareStartConvCmd(ADCx, ENABLE);
8. 经常使用ADC循环不间断地采样,这时DMA也要相应配置成DMA_Mode_Circular 。
DMA_Mode_Circular 工作在循环缓存模式
DMA_Mode_Normal 工作在正常缓存模式
9. ADC_RegularChannelConfig不能忘记开启。
如:
ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_13Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 2, ADC_SampleTime_13Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 3, ADC_SampleTime_13Cycles5);
下面附一使用例程:
adc.c:
#include "stm32f10x.h"
#include "adc.h"
#define ADC1_DR_Address ((uint32_t)0x4001244C)
__IO uint16_t ADC_RegularConvertedValueTab[3];
void ADC_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
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_AIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ADC_RegularConvertedValueTab;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
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_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
DMA_Cmd(DMA1_Channel1, ENABLE);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 3;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_13Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 2, ADC_SampleTime_13Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 3, ADC_SampleTime_13Cycles5);
// ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 4, ADC_SampleTime_13Cycles5);
ADC_DMACmd(ADC1, ENABLE);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}