AnsweredAssumed Answered

Problem with ADC: channel affected by another and trouble with type conversion

Question asked by prieur.jean on Jun 24, 2012
Latest reply on Jun 25, 2012 by prieur.jean
Hello,

I'm using IAR embedded Workbench for ARM 6.30 kickstart to program a STM32F4 DISCOVERY microcontroler.

I have two problem with ADCs:

- I use two ADC channels (12 & 14) based on the same ADC (ADC2) to 8-bits digitizing two potentiometers output voltages. The trouble is, when I change the first potentiometer value, it's affects the second potentiometer digital value.

- The strangest problem: sometimes when I run my project on the STM32 with the "Make and Restart Debugger" button, with NO CHANGES in my code, the ADC behavior can change: instead of being digitized on 8-bit (0 to 255), potentiometers values will be digitized on 6-bits (0 to 63) or on 16-bits (0 to 65535), I can see it on the watch !! What is the problem with IAR ?

Thanks for your help ! I work on this problem for 2 weeks...

Jean



01.
// ADC ADRESS
02.#define ADC_CCR_ADDRESS    ((uint32_t)0x40012308)
03. 
04.// Table to store digitized ADC values
05.extern __IO uint16_t TabADC[2];
06. 
07. 
08.////////////////////////////////////////////////////////////////////
09.//////// ADC configuration                               ///////////
10.////////////////////////////////////////////////////////////////////
11. 
12. 
13.void ADC_Config(void)
14.{
15.   
16.  // ADC initialisation
17.  ADC_CommonInitTypeDef ADC_CommonInitStructure;
18. 
19.  // CLOCK set for DMA2 + GPIOC2 + ADC1 + ADC2
20.  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_GPIOC, ENABLE);
21.  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC2, ENABLE);
22. 
23.  // DMA2 config (direct access memory) Stream0 channel0
24.  DMA_InitTypeDef DMA_InitStructure;
25.  DMA_InitStructure.DMA_Channel = DMA_Channel_0; // USE CHANNEL 0
26.  DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&TabADC; // Store in my table the digitized values
27.  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC_CCR_ADDRESS;
28.  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
29.  DMA_InitStructure.DMA_BufferSize = 2;
30.  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
31.  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
32.  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
33.  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
34.  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
35.  DMA_InitStructure.DMA_Priority = DMA_Priority_High;
36.  DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;        
37.  DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
38.  DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
39.  DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
40.  DMA_Init(DMA2_Stream0, &DMA_InitStructure);
41.  DMA_Cmd(DMA2_Stream0, ENABLE); // DMA2_Stream0 enable
42.   
43.  // ADCs configuration: ADC Channel 12 &  14
44.  // POT2: ADC Channel 12 -> PIN PC2
45.  // POT3: ADC Channel 14 -> PIN PC4
46. 
47.  GPIO_InitTypeDef GPIO_InitStructure;  // GPIOC
48.  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_4;
49.  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
50.  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN ;
51.  GPIO_Init(GPIOC, &GPIO_InitStructure);
52. 
53.  // 'ADC "Common"
54.  ADC_CommonInitStructure.ADC_Mode = ADC_DualMode_RegSimult;
55.  ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
56.  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1;
57.  ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
58.  ADC_CommonInit(&ADC_CommonInitStructure);
59. 
60.  // Initialisation ADC "2" channels 12 & 14
61.  ADC_InitStructure.ADC_Resolution = ADC_Resolution_8b;
62.  ADC_InitStructure.ADC_ScanConvMode = ENABLE;
63.  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
64.  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
65.  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
66.  ADC_InitStructure.ADC_NbrOfConversion = 2;
67.  ADC_Init(ADC2, &ADC_InitStructure);
68.  // ADC2 regular channels 12, 14
69.  ADC_RegularChannelConfig(ADC2, ADC_Channel_12, 1, ADC_SampleTime_3Cycles);
70.  ADC_RegularChannelConfig(ADC2, ADC_Channel_14, 2, ADC_SampleTime_3Cycles);
71. 
72.  // Enable DMA request after last transfer (Multi-ADC mode)
73.  ADC_MultiModeDMARequestAfterLastTransferCmd(ENABLE);
74. 
75.  // Start ADC2
76.  ADC_Cmd(ADC1, ENABLE);
77.  ADC_Cmd(ADC2, ENABLE);
78. 
79.  // Start ADC1 Software Conversion
80.  ADC_SoftwareStartConv(ADC1);
81.}

Outcomes