cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 F4 Discovery adc / dma setup for 4 channels

thecrocodial
Associate II
Posted on October 17, 2013 at 18:24

Hi,

based on the example in the peripheral lib i tried to expand the code to read 4 different pins. Googling around i managed to get values, but the problem is: the first (adc channel 11) is working fine, but the second  (adc channel 12) has an effect on all channels (or at least the values in the array). channel 14 and 15 don't have any effect on the array. I skipped 13 because its already in use on the STM32F4Discovery.

Since the Values at the Pins are fine ( checked with multimeter ) the problem has to be my init code. I would be really thankful for advice.

*edit : fixed clives remarks

My init-code:

__IO uint16_t ADC3ConvertedValue[4];

__IO uint32_t ADC3ConvertedVoltage[4];

void ADC3_CH12_DMA_Config(void)

{

  ADC_InitTypeDef       ADC_InitStructure;

  ADC_CommonInitTypeDef ADC_CommonInitStructure;

  DMA_InitTypeDef       DMA_InitStructure;

  GPIO_InitTypeDef      GPIO_InitStructure;

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

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_GPIOC, ENABLE);

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE);

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

  DMA_DeInit(DMA2_Stream0);

  DMA_InitStructure.DMA_Channel = DMA_Channel_2;

  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC3_DR_ADDRESS;

  DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADC3ConvertedValue[0];

  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;

  DMA_InitStructure.DMA_BufferSize = 4;

  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; // orig dis

  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //orig dis

  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 ADC3 Channel12 pin as analog input ******************************/

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_4|GPIO_Pin_5;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;

  GPIO_Init(GPIOC, &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; // Orig dis

  ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;

  ADC_CommonInit(&ADC_CommonInitStructure);

  /* ADC3 Init ****************************************************************/

  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;

  ADC_InitStructure.ADC_ScanConvMode = ENABLE; //orig disable

  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;

  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;

  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;

  ADC_InitStructure.ADC_NbrOfConversion = 4;

  ADC_Init(ADC3, &ADC_InitStructure);

  /* ADC3 regular channel12 configuration *************************************/

  ADC_RegularChannelConfig(ADC3, ADC_Channel_11, 1, ADC_SampleTime_15Cycles);

  ADC_RegularChannelConfig(ADC3, ADC_Channel_12, 2, ADC_SampleTime_15Cycles);

  ADC_RegularChannelConfig(ADC3, ADC_Channel_14, 3, ADC_SampleTime_15Cycles);

  ADC_RegularChannelConfig(ADC3, ADC_Channel_15, 4, ADC_SampleTime_15Cycles);

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

  ADC_DMARequestAfterLastTransferCmd(ADC3, ENABLE);

  /* Enable ADC3 DMA */

  ADC_DMACmd(ADC3, ENABLE);

  /* Enable ADC3 */

  ADC_Cmd(ADC3, ENABLE);

}

#adc #stm32f4 #dma #discovery
6 REPLIES 6
Posted on October 17, 2013 at 19:10

DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADC3ConvertedValue[0];

DMA_InitStructure.DMA_BufferSize = 4; // FOUR ELEMENTS

3 Cycles, that will give some awful results

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
thecrocodial
Associate II
Posted on October 17, 2013 at 19:34

Thanks for the response! I fixed the buffersize and the memoryadress and switched to 15 cycles. The odd behaviour stays the same. Any more ideas?

Posted on October 17, 2013 at 19:50

PC4 and PC5 don't have connectivity to ADC3?

PC4 -> ADC12_IN14

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
thecrocodial
Associate II
Posted on October 17, 2013 at 20:14

Now I am kind of embarrassed. I switched to PA1 and PA2 and it works fine now. Thank you! 

thecrocodial
Associate II
Posted on October 17, 2013 at 20:15

Now I am kind of embarrassed. I switched to PA1 and PA2 and it works fine now. Thank you! 

Posted on October 17, 2013 at 21:08

Don't be embarrassed, I have to look it up and cross-check the stuff myself.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..