用STM32F411进行双ADC采样,ADC1 采样8路数据,DMA传输,因为ADC2不能DMA,所以就想直接读取,现在ADC1 8路DMA是没问题的,ADC2却始终没有数据。一整天了,翻遍了国内国外的各大论坛,有一些单ADC多通道的,但没有双ADC多通道的,STM32F1系列倒是有双ADC的,但要用到CDR寄存器,而在STM32F4中貌似并没有这个寄存器,不知道该如何操作,手册Datasheet也看了N遍,水平有限实在搞不定了故发帖求助。还望大神点拨一下,代码如下:
void RCC_Config(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2, ENABLE);
RCC_APB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_DMA2,ENABLE);
}
void DMA_Config(void)
{
while (DMA_GetCmdStatus(&DMA_Streamx) != DISABLE){}//等待 DMA 可配置
DMA_InitStructure.DMA_Channel = DMA_Channel_0;
DMA_InitStructure.DMA_PeripheralBaseAddr =(uint32_t)ADC1_DR_Address; //ADC1 DATA ADDRESS
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&AD_Value; //DMA memory base address
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;//
DMA_InitStructure.DMA_BufferSize =8; //
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_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream0, &DMA_InitStructure);
DMA_Cmd(DMA2_Stream0, ENABLE);
}
void Gpio_Config(void)
{
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_InitStructure.GPIO_Pin = (uint16_t)0x00FF;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;//PB0 ADC2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void ADC_Config(void)
{
//ADC common
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1;
ADC_CommonInitStructure.ADC_Mode = ADC_DualMode_RegSimult;
//ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
ADC_CommonInit(&ADC_CommonInitStructure);
//ADC1
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_NbrOfConversion = 8;
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
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_RegularChannelConfig(ADC1,ADC_Channel_3,4,ADC_SampleTime_15Cycles);
ADC_RegularChannelConfig(ADC1,ADC_Channel_4,5,ADC_SampleTime_15Cycles);
ADC_RegularChannelConfig(ADC1,ADC_Channel_5,6,ADC_SampleTime_15Cycles);
ADC_RegularChannelConfig(ADC1,ADC_Channel_6,7,ADC_SampleTime_15Cycles);
ADC_RegularChannelConfig(ADC1,ADC_Channel_7,8,ADC_SampleTime_15Cycles);
//ADC2
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 1;
ADC_Init(ADC2, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC2,ADC_Channel_8,1,ADC_SampleTime_15Cycles);
ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE); //if not have this function , can't get new value
/* Enable ADC1 DMA */
ADC_DMACmd(ADC1, ENABLE); //make enable ADC1 WITH DMA2
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
ADC_Cmd(ADC2, ENABLE);
}
void init_ADC(void)
{
RCC_Config();
Gpio_Config();
ADC_Config();
DMA_Config();
ADC_SoftwareStartConv(ADC1);
ADC_SoftwareStartConv(ADC2);
}
ADC2的1路数据读取用 ADC_GetConversionValue(ADC2);
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>