本帖最后由 者结终 于 2018-8-28 15:45 编辑
使用PA5和PA6
void AD_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
/*Enable DMA1 Clock*/
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
/*DMA1 Channel1 Configuration*/
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_ADDRESS;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ADC_ConvertedValue;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 2;
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);
/*ENABLE DMA1 CHANNEL1*/
DMA_Cmd(DMA1_Channel1, ENABLE);
/*------------ADC1 configuration with DMA enabled---------*/
/*Enable the HSI oscillator*/
RCC_HSICmd(ENABLE);
/*Enable GPIOA Clock*/
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*Check that HSI oscillator is ready*/
while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
ADC_DeInit(ADC1);
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 = 2;
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_Init(ADC1, &ADC_InitStructure);
/*ADC1 regular channel10 configuration*/
ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 1, ADC_SampleTime_48Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 2, ADC_SampleTime_48Cycles);
ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
//Enable ADC1 DMA
ADC_DMACmd(ADC1, ENABLE);
//Enable ADC1
ADC_Cmd(ADC1, ENABLE);
/*Wait until the ADC1 is ready*/
while(ADC_GetFlagStatus(ADC1, ADC_FLAG_ADONS) == RESET);
/*Start ADC1 Software Conversion*/
ADC_SoftwareStartConv(ADC1);
} |