cancel
Showing results for 
Search instead for 
Did you mean: 

Problem in Reading continuous ADC value

sdashmiz
Associate II
Posted on July 07, 2014 at 22:19

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){};

#adc #dma
6 REPLIES 6
Posted on July 07, 2014 at 23:36

Yes, all rather a mess there.

Using DMA, or polling?

Using ADC1 or ADC2?

One channel or two? Independent or Simultaneously?

Initial element of array adcdata[0] not adcdata[1]

[DEAD LINK /public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/3%20channels%20on%20ADC1%20and%20DMA&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&currentviews=690]https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2Fcortex_mx_stm32%2F3%20channels%20on%20ADC1%20and%20DMA&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B¤tviews=690

[DEAD LINK /public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/stm32f207%20ADC%2BTIMER%2BDMA%20%20Poor%20Peripheral%20Library%20Examples&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&currentviews=2642]https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2Fcortex_mx_stm32%2Fstm32f207%20ADC%2BTIMER%2BDMA%20%20Poor%20Peripheral%20Library%20Examples&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B¤tviews=2642

See also

STM32F2xx_StdPeriph_Lib_V1.1.0\Project\STM32F2xx_StdPeriph_Examples\ADC
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
sdashmiz
Associate II
Posted on July 10, 2014 at 01:15

Thank you very much for your reply,

I was trying to use two different ADCs at the same time, I have cleaned the code and I am using only one channel of ADC2 to read an external value. My neat code is below, however, the ADC does not change by changing voltage and it is constantly 1 v regardless of true value.

int main(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

ADC_InitTypeDef ADC_InitStructure;

DMA_InitTypeDef DMA_InitStructure;

 ADC_CommonInitTypeDef ADC_CommonInitStructure;

__IO uint16_t Adcdata=0 ;

  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, ENABLE);

   RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2, ENABLE);

  DMA_StructInit(&DMA_InitStructure);

   DMA_DeInit(DMA2_Stream3); 

  DMA_InitStructure.DMA_Channel = DMA_Channel_1;

  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC2->DR; //Source address*/

  DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&Adcdata; //Destination address*/

  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;

  DMA_InitStructure.DMA_BufferSize = 1; //Buffer size

  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;

  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;

  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //source size - 16bit

  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; // destination size = 16b

  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_Stream3, &DMA_InitStructure); //Initialize the DMA

  DMA_Cmd(DMA2_Stream3, 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);

  /*ADC2 configuration*/

  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;

  ADC_InitStructure.ADC_ScanConvMode = DISABLE;

  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);

  /*ADC2 regular TRIMER_CHANNEL configuration*/

  ADC_RegularChannelConfig(ADC2, ADC_Channel_15, 1, ADC_SampleTime_56Cycles);

    ADC_Cmd(ADC2, ENABLE);

   ADC_DMARequestAfterLastTransferCmd(ADC2, ENABLE);

  ADC_DMACmd(ADC2, ENABLE)

 ADC_SoftwareStartConv(ADC2);

      

  ADC_Cmd(ADC2, ENABLE);

   ADC_DMARequestAfterLastTransferCmd(ADC2, ENABLE);

  ADC_DMACmd(ADC2, ENABLE);

 ADC_SoftwareStartConv(ADC2);

  GLCD_Backlight(BACKLIGHT_ON);

  

   

while(1)

  {  

  GLCD_print(''%d'', Adcdata);

}

while(1){}

}

Thanks

Posted on July 10, 2014 at 02:45

I don't have a manual/schematic for your board, will assume the POT/TRIMMER is on PC5

Are you should there aren't any jumpers on the board precluding the ADC from reading the voltage?

Probably wouldn't put DMA data on the stack
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
sdashmiz
Associate II
Posted on July 10, 2014 at 23:00

Thank you

PC5 is channel 15 of ADC2 according to manual.

I added a line   Adcdata = ADC_GetConversionValue(ADC2); so at least the value is correct but there is no change of value, do you have any specific jumper in the mind that I can check?

Thanks

sdashmiz
Associate II
Posted on July 17, 2014 at 18:08

The former problem solved now, I can read the ADC but there is voltage offset that increases with the voltage applied from PO, do you have any idea why this happens?

Thanks

sdashmiz
Associate II
Posted on July 17, 2014 at 19:28

Actually the main problem is reading 0 , it does not give a stable zero reading but the the highest voltage is exactly 3.3 volts