Skip to main content
vicki19880929
Associate III
November 23, 2013
Question

Do ADC_GetConversionValue read the current ADC value?

  • November 23, 2013
  • 10 replies
  • 3194 views
Posted on November 23, 2013 at 01:22

Hi all,

I'm recently building an application which asks for the current analog input at each poll.  It only reads the ADC value when it receives the request, which means that my STM32L will not read the ADC value all the time.  

And here's my question,  I found out that the ADC value I read at the current request is always the value read in the previous request.

So I did a simple test like this: I wrote a for loop that will read the ADC value for ten times.  In the meanwhile, I tuned my power supply which gives the analog input. (e.x. 0V-3V-0V-3V-0V-3V...)  And then I found out the same thing that it only prints out the previous ADC value instead of the current one. (e.x. 0-0-4096-0-4096-0...)  

 

for (i = 0; i < 10; i++) {

    /* Read ADC conversion result */

    ADCdata = ADC_GetConversionValue(ADC1);

    printf(''%4u\n'', ADCdata);

    printf(''change value now\n'');

    Delay(0xfffff);

    Delay(0xfffff);

    Delay(0xfffff);

    Delay(0xfffff);

    Delay(0xfffff);

    Delay(0xfffff);

  }

Does anyone know how I could read the current ADC value?

Thanks a lot!

#use-your-head!
    This topic has been closed for replies.

    10 replies

    Tesla DeLorean
    Guru
    November 23, 2013
    Posted on November 23, 2013 at 02:13

    The prescribed method is to start the conversion, and then wait for the EOC (End Of Conversion) status, and then read the value, once.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    vicki19880929
    Associate III
    November 23, 2013
    Posted on November 23, 2013 at 02:44

    hi clive,

    I followed what you said but it still gets the same result.

    This is my modified code:

    for (i = 0; i < 10; i++) {

        /* Start ADC1 Software Conversion */

        ADC_SoftwareStartConv(ADC1);

        /* Wait until ADC end of conversion */

        while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET) {}

        /* Read ADC conversion result */

        ADCdata = ADC_GetConversionValue(ADC1);

        printf(''%4u\n'', ADCdata);

        printf(''change value now\n'');

        Delay(0xfffff);

        Delay(0xfffff);

        Delay(0xfffff);

        Delay(0xfffff);

        Delay(0xfffff);

        Delay(0xfffff);

      }

    Tesla DeLorean
    Guru
    November 23, 2013
    Posted on November 23, 2013 at 03:26

    Then may be you should focus on the initialization code I can't see, and make sure you're not in a continuous conversion mode?

      ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;

    And perhaps wait on a key stroke before initiating the next conversion.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    vicki19880929
    Associate III
    November 23, 2013
    Posted on November 23, 2013 at 03:44

    Oh yes!  That's the one!  I wasn't aware of the function of the continuous mode.  Thanks a lot! :)

    giulio
    Associate
    October 27, 2016
    Posted on October 27, 2016 at 22:41

    Hi there

    To avoid opening almost same question I'm using this space.

    I have a similar issue, but instead of having same value, I'm having a strange value on my adc.

    I have full battery charge. 3.7v

    In my GSM module AT+CBC gives me 4210

    Then I execute this function every each 2 seconds:

    /* Start ADC1 Software Conversion */

    ADC_StartOfConversion(ADC1);

    /* Wait until ADC end of conversion */

    while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET) {}

    /* Read ADC conversion result */

    V_Value = ADC_GetConversionValue(ADC1);

    I'm getting the value 2072 +/- 4

    This is my init:

      ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;

      ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;

      ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;

      ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;

      ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward;

      ADC_Init(ADC1, &ADC_InitStructure);

    ///// other stuffs ///

    ADC_AutoPowerOffCmd(ADC1, ENABLE);//Enables or disables the ADC Power Off.

    ADC_VbatCmd(ENABLE);

    Can you help me understand this value and how can I get the battery level ?

    Thanks

    Tesla DeLorean
    Guru
    October 27, 2016
    Posted on October 27, 2016 at 23:06

    What part are you using? What is your VDDA/+VREF voltage?

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    giulio
    Associate
    October 27, 2016
    Posted on October 27, 2016 at 23:14

    STMF32072

    VREF = 1720

    VDDA = 2044

    tks

    Tesla DeLorean
    Guru
    October 27, 2016
    Posted on October 28, 2016 at 00:35

    1492?

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    giulio
    Associate
    October 28, 2016
    Posted on October 28, 2016 at 13:36

    sorry.. this value should be where ?

    V_Value = ADC_GetConversionValue(ADC1);

    = 2045

    Tesla DeLorean
    Guru
    October 28, 2016
    Posted on October 28, 2016 at 15:04

    Look, you've off-topic'd a 3 year old thread on the STM32L1 for some issue reading battery voltages on an STM32F0, I ask you what the analogue reference *voltage* is on your design, which I know nothing about, and you give me a pair of numbers in the thousands that make no sense.

    Open a new thread, describe your design so I have some context about what voltages you are presenting where.

    Can the F0 accept a VBAT that is higher than +VREF?

    Does it half or quarter the VBAT internally to keep it from going off-scale?

    You have to take the 0..4095 value returned by the ADC an scale that against the external minimum/maximal references.

    You should also be able to benchmark the reading against the internal reference.

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