2018-02-12 07:09 AM
Hi
I am trying to develop a project via the STM32F303RE Nucle Board. In the project, I need to read analog data from channel 0 (PA4) on the ADC2 hardware. For this I made an init code.
void ADC_DMA_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
/* Configure the ADC clock */
RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div1);
/* Enable ADC1 clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ADC12, ENABLE);
/* Enable GPIOA and GPIOC Periph clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
/* Enable DMA2 clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);
/* ADC Channels configuration */
/* Configure ADC2 Channel0 as analog input */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* DMA configuration */
/* DMA2 Channel1 Init Test */
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC2->DR;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADCConvertedValue;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 1;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
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_Medium;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA2_Channel1, &DMA_InitStructure);
ADC_StructInit(&ADC_InitStructure);
/* ADC Calibration procedure */
ADC_VoltageRegulatorCmd(ADC2, ENABLE);
/* Insert delay equal to 10 µs */
Delay(10);
ADC_SelectCalibrationMode(ADC2, ADC_CalibrationMode_Single);
ADC_StartCalibration(ADC2);
while(ADC_GetCalibrationStatus(ADC2) != RESET );
calibration_value_1 = ADC_GetCalibrationValue(ADC2);
/* ADC Dual mode configuration */
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Interleave;
ADC_CommonInitStructure.ADC_Clock = ADC_Clock_AsynClkMode;
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1;
ADC_CommonInitStructure.ADC_DMAMode = ADC_DMAMode_Circular;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = 10;
ADC_CommonInit(ADC2, &ADC_CommonInitStructure);
/* */
ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Enable;
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0;
ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_OverrunMode = ADC_OverrunMode_Disable;
ADC_InitStructure.ADC_AutoInjMode = ADC_AutoInjec_Disable;
ADC_InitStructure.ADC_NbrOfRegChannel = 1;
ADC_Init(ADC2, &ADC_InitStructure);
/* ADC2 regular channel1 configuration */
ADC_RegularChannelConfig(ADC2, ADC_Channel_1, 1, ADC_SampleTime_7Cycles5);
/* Configures the ADC DMA */
ADC_DMAConfig(ADC2, ADC_DMAMode_Circular);
/* Enable the ADC DMA */
ADC_DMACmd(ADC2, ENABLE);
/* Enable ADC2 */
ADC_Cmd(ADC2, ENABLE);
/* wait for ADC1 ADRDY */
while(!ADC_GetFlagStatus(ADC2, ADC_FLAG_RDY));
/* Enable the DMA channel */
DMA_Cmd(DMA2_Channel1, ENABLE);
/* Start ADC2 Software Conversion */
ADC_StartConversion(ADC2);
}
When I run these codes I can't read adc. I ran the same codes with ADC1 and DMA1. But ADC2 and DMA2 do not work.
Is there something I'm missing? Why does not work?
Thanks
2018-02-12 08:13 AM
>>
Is there something I'm missing? Why does not work?
Nothing jumping out immediately. Code not compilable/complete.
Probe DMA, ADC in Peripheral View mode, inspect registers, see if errors flagging, addresses as expected.
2018-02-13 12:33 AM
in Debug Mode, I see the ADC2->DR register. This register always appears to be zero. I guess the ADC2 hardware is not working. I don't understand what the problem is.
What is missing in the code? The ADC1 Hardware is working in the same way. But ADC2 is not working.
2018-02-13 10:53 PM
Is there anyone who can help? I can not run ADC2.
2018-02-14 01:00 AM
I don't have your board.
But the F3 DSP Lib (STM32F30x_DSP_StdPeriph_Lib) contains a working dual-mode ADC example (ADC1+2) you can work with, and compare.
2018-02-16 02:27 AM
But I am running ADC with Single ended Mode.What is the difference between ADC1 and ADC2?Because the same codes work with ADC1.But ADC1 does not match any channel PA4.
Turvey.Clive.002
When I watch the ADC2 registers with Debug, I see thatADC2->DR Register always Zero.Is there a configuration code with ADC2?Thanks
2018-02-16 03:14 AM
But I am running ADC with Single ended Mode.
Really ?
ADC_CommonInitStructure.ADC_Mode =
ADC_Mode_Interleave
;2018-02-16 06:00 AM
Thank you for the reply, Yes, I made a mistake there. ADC_Mode Should Be 'ADC_Mode_Independent'. But I can't understand how the ADC1 works with these settings.
2018-02-16 10:21 AM
But I can't understand how the ADC1 works with these settings.
If you refer to your code given in the first post that initializes ADC2, me neither