cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F205RC using ADC with DMA

Avonac
Associate II
Posted on June 09, 2013 at 16:32

Hey Guys,

I'm currently playing with the STM32F205RC Controller and I have some troubles with using  the A/D converter with DMA.

The task I want to do is quite simple. I want to measure a voltage using ADC1 on Port A Pin 4.

The Standard Peripherals Library offers an example for using ADC with DMA. The Example is setting up the ADC3 on Port F Pin 9.

I did some changes to the Standard Peripherals Library example in order to use it with the ADC1 on Port A Pin4.

-  I changed al settings from ADC3 to ADC1

-  I've setup DMA2 to use channel 0 stream 0 (according to the Reference Manual RM0033)

-  I'm using the ADC1 Data Register 0x4001204C for DMA

-  I've setup GPIO Port A Pin 4 to work in GPIO_Mode_AN

But anyway, I'm not able to get the ADC1 to measure the voltage. The Value of ADC1 is moving from 0x0070 to 0x007f independent of the voltage I want to measure. Therefore, I suppose it's just some noise I see .

I guess there is something I have overseen.

&sharpdefine

ADC1_DR_ADDRESS ((uint32_t)

0x4001204C

) __IO uint16_t ADC1ConvertedValue;

void

Hardware_AD_Init(

void

) { ADC_InitTypeDef ADC_InitStructure; ADC_CommonInitTypeDef ADC_CommonInitStructure; DMA_InitTypeDef DMA_InitStructure; GPIO_InitTypeDef GPIO_InitStructure;

/* Enable ADC1, DMA2 and GPIO clocks ****************************************/

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

/* DMA2 Stream0 channel0 configuration **************************************/

DMA_InitStructure.DMA_Channel = DMA_Channel_0; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_ADDRESS; DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADC1ConvertedValue; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; 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_High; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; 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);

/* Configure ADC1 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);

/* ADC Common Init **********************************************************/

ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2; ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; ADC_CommonInit(&ADC_CommonInitStructure);

/* ADC1 Init ****************************************************************/

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(ADC1, &ADC_InitStructure);

/* ADC1 regular channel1 configuration *************************************/

ADC_RegularChannelConfig(ADC1, ADC_Channel_1,

1

, ADC_SampleTime_3Cycles);

/* Enable DMA request after last transfer (Single-ADC mode) */

ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);

/* Enable ADC1 DMA */

ADC_DMACmd(ADC1, ENABLE);

/* Enable ADC1 */

ADC_Cmd(ADC1, ENABLE); }

void

StartConversion(

void

) { ADC_SoftwareStartConv(ADC1);

// A/D convert start

} UINT16 ui16_Read_ADC1_ConvertedValue(

void

) {

return

ADC1ConvertedValue;

// Read and return conversion result

}

Thank you in advance.

 

#adc #dma #stm32f205rc
2 REPLIES 2
Posted on June 09, 2013 at 17:27

ADC_RegularChannelConfig(ADC1, ADC_Channel_1,

1

, ADC_SampleTime_3Cycles);

Isn't PA4 ADC_Channel_4 (ADC12_IN4 per Data Sheet/Manual) ? 3 Cycles will generally give a hideously noisy result.

Using &ADC1->DR would simplify the address issue, I'd look it up but it's a better job for the compiler, and part independent.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Avonac
Associate II
Posted on June 10, 2013 at 20:22

Hey clive1,

You are right I've setup the wrong ADC channel.

Thank you for the fast reply.