AnsweredAssumed Answered

Problem in Reading continuous ADC value

Question asked by dashmiz.shadi on Jul 7, 2014
Latest reply on Jul 17, 2014 by dashmiz.shadi
Hi,
I am using STM3220G-SK to read adc values from an external source.
I have tried different codes but all of them give me the same result. once the program is fired the adc valued is read and shown on the LCD but after that the value will remain unchanged regardless of the voltage change. to read the new value I have to either enter debugging mode again or turn off/on the module.
could anyone let me know where I am making a mistake. I have not so much experience in embedding programming and I have just started to work in this area.
Thanks
my code is written below:
int main(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
 ADC_CommonInitTypeDef ADC_CommonInitStructure;
__IO uint16_t Adcdata [2] ;
 RCC_AHB1PeriphClockCmd(TRIMER_CLK, ENABLE);


  GPIO_InitStructure.GPIO_Pin = TRIMER_PIN;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(TRIMER_PORT, &GPIO_InitStructure);

 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2| RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOA , ENABLE);
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2, ENABLE);
  DMA_StructInit(&DMA_InitStructure); 



    DMA_DeInit(DMA2_Stream4); 
  DMA_InitStructure.DMA_Channel = DMA_Channel_0;
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_ADDRESS; //Source address*/
  DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&Adcdata[1]; //Destination address*/
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
  DMA_InitStructure.DMA_BufferSize = 2; 
  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_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
  DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; 
  DMA_Init(DMA2_Stream4, &DMA_InitStructure); //Initialize the DMA
  DMA_Cmd(DMA2_Stream4, ENABLE); 

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOC, &GPIO_InitStructure);

  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  


  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);
  
  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfConversion = 1;
  ADC_Init(ADC2, &ADC_InitStructure);
   ADC_Init(ADC1, &ADC_InitStructure);

ADC_RegularChannelConfig(ADC2, ADC_Channel_15, 1, ADC_SampleTime_144Cycles);

   ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
 
  ADC_DMACmd(ADC2, ENABLE);

  ADC_Cmd(ADC2, ENABLE);
  
 ADC_SoftwareStartConv(ADC2);
ADC_ITConfig(ADC2, ADC_IT_EOC, ENABLE);

while(1)
  {   
   
           if   (ADC_GetFlagStatus(ADC2, ADC_FLAG_EOC) == RESET);
        {
     
        Adcdata[0] = ADC_GetConversionValue(ADC2);
         ADC_DMARequestAfterLastTransferCmd(ADC2, ENABLE);

  GLCD_print("%d", Adcdata[0]);
         ADC_ClearFlag(ADC2, ADC_FLAG_EOC);
  DelayResolution100us(10000);
          /* Start ADC1 Software Conversion */
       ADC_SoftwareStartConv(ADC2);
    }
}
while(1){};


Outcomes