• Join
  • Sign In with my.TI Login
Texas Instruments
  • Products
  • Applications
  • Tools & Software
  • Support & Community
  • Sample & Buy
  • About TI
Sample & Purchase Cart Sample & Purchase Cart
  • Search
  • Advanced
TI E2E™ Community
  • Support Forums
  • Blogs
  • Groups
  • Videos
  • 简体中文
  • More ...
TI Home » TI E2E Community » Support Forums » Microcontrollers » MSP430™ Microcontrollers » MSP430 Ultra-Low Power 16-bit Microcontroller Forum » Problem when measuring battery voltage with ADC12
Share
MSP430™ Microcontrollers
  • Forum
  • Announcements
  • E2E Wiki
Options
  • Subscribe via RSS
MSP430 Resources
  • MSP430 Product Folder
  • MSP-EXP430G2 - MSP430 LaunchPad Value Line Development kit
  • MSP430 Getting Started Guide
  • MSP430 Microcontroller Projects
  • More Resources >
  • Problem when measuring battery voltage with ADC12

    Problem when measuring battery voltage with ADC12

    This question is answered
    Veikko Soininen
    Posted by Veikko Soininen
    on Jun 18 2012 08:31 AM
    Prodigy150 points

    Hello all!

    I've red all the documentation I've found about the measuring input voltage and comparing it to internal reference (2.0V). Yet my results with voltage measurements are way off. I'm posting my code below. With accurate multimeter I get the result of 2,958 V. But with ADC I get the result of 145 (8-bit) which turns into 2,27V with my conversion formula:

    VCC = 2 * result * (Vref/255) = 2.27V,

    Vref = 2.0V.

    I've went through all forums I found and it bugges me that I don't find anything wrong with my attempt. Any help?

    Edit: Should have mentioned that I use CC430F5137!

    unsigned char getVcc3()
    {
        unsigned int result;

        /* Initialize REF module */
        // Enable 2.0V shared reference, disable temperature sensor to save power
        REFCTL0 |= REFMSTR + REFVSEL_1 + REFON + REFTCOFF;

        ADC12CTL0 = ADC12ON;                                                       // Turn on ADC12, set sampling time (0000 = 4clk)

        ADC12CTL1 = ADC12SHP + ADC12SSEL_1;                    // Use sampling timer, aclk

        ADC12CTL2 &= ~ADC12RES_2; // 8-bit result

        ADC12MCTL0 |= ADC12INCH_11 + ADC12SREF_1;        // Select channel B (1011), Vr+=Vref+ and Vr-=AVss

        //app_wait_ms(settleTime);                                                        
        while(REFCTL0 & REFGENBUSY);                                       // Allow internal reference to stabilize

        ADC12CTL0 |= ADC12ENC; // Enable conversions
        ADC12CTL0 |= ADC12SC; // Start conversion

        while(!(ADC12IFG & BIT0));                                                     // Conversion done?

        result = ADC12MEM0;

        ADC12IFG &= ~BIT0;                                                                // Clear interrupt flag
        ADC12CTL0 &= ~(ADC12ON + ADC12ENC);                     // Disable ADC
        REFCTL0 &= ~REFON;                                                            // Disable reference

        return result;
    }
    Report Abuse
    • Reply
    You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    All Replies
    • Vidya Sagar Kantamneni
      Posted by Vidya Sagar Kantamneni
      on Jun 18 2012 15:30 PM
      Intellectual935 points

      Dear Veikko

      Ofcourse, what is the step size? or in other way, what is the minimum resolution you can get from your ADC? I think this will answer your question. 

      Hope this helps.

      Best regards, 

      Vidya. 

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Veikko Soininen
      Posted by Veikko Soininen
      on Jun 19 2012 00:44 AM
      Prodigy150 points

      Dear Vidya,

      as I see it, the step size is 4 / 255 V = ~16 mV which is okay for me. Results were the same when I used 10-bit conversion. It seems not to explain why I get 2,27 V when I should get nearly 3 volts.

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • hardik padhariya
      Posted by hardik padhariya
      on Jun 19 2012 02:30 AM
      Prodigy150 points

      Do u get 145 every time???????

      VCC = 2 * result * (Vref/255) = 2.27V,

      if you are using 12bit adc then   result = 1023*i/p/Vref;

      can you explain how u derived this formula??

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Argail
      Posted by Argail
      on Jun 19 2012 03:04 AM
      Expert1510 points

      There is a bug inside the ref module of the serie 6xxx, ADC27 in the errata sheet. But the workaround seems not to works as described, what I discovered is to divide the ADC12OSC by 3 and not set the reference as output, and to use the register of the ADC12 to set the reference (REFMSTR=0). With this configuration temp. sensor, voltage divider and AI seems to works,.

      Anyway, another thing is that you can't use REFGENBUSY to check if the reference is stable, this bits indicates that the reference is used bay a module. You have to set an independant wait loop.

      You can also use interruption ans set LPM3 to wait for the conversion, ths will reduce ADC noise.

      Finally your formula seems not to be correct, have a lock on page 550 of slau208i.pdf. Using the voltage divider, Vref+ (2.0V) and AVss in 12bits mode, the conversion (whitout using TLV) is NADC*4/4095.

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Veikko Soininen
      Posted by Veikko Soininen
      on Jun 19 2012 03:35 AM
      Prodigy150 points

      Ok, good to know about the REFGENBUSY, I'll revert back to my simple delay. I will have to try to set the ref voltage in ADC register and not in the Shared Reference register.

      Your conversion code seems to be the same as mine! Instead of 2 * 2.0 (multiplying first by two because of the Vcc divider and then multiplying by two because of the 2,0V reference) it uses multiplication by 4.

      145*4/255 = 2,27V, here using 255 because of the 8-bit resolution.

      And question regarding the ADC result; conversion is NOT always the same but it depends on actual Vcc. It just seems to be too small value all the time. I will post more result later.

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Argail
      Posted by Argail
      on Jun 19 2012 04:04 AM
      Expert1510 points

      Oh yeah, my fault concerning the calculation, the 8bit resolution went off my brain while answering:D

      You'll not be able to select 2.0V VRef directly with the ADC12 registers, but 1,5 or 2,5. This should be ok for testing the measurement.

      Try to lengthen the sampling time too.

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Veikko Soininen
      Posted by Veikko Soininen
      on Jun 19 2012 04:32 AM
      Prodigy150 points

      I changed my battery and with multimeter I got 3,538 V. This gave me out ADC result of 177. With conversion formula 177*4/255 I got the result of 2,78 V. So when voltage goes up, my result goes up but it is too low...

      Edit: Getting out of ideas... I changed to use the ADC reference 2,5V and when expecting 3,538 V, got the result of 141. This turns out as 141*2,5*2/255 = 2,78 V. So not better at all...

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Argail
      Posted by Argail
      on Jun 19 2012 04:37 AM
      Expert1510 points

      Did you have access to the TLV structure?

      And could you measure your Vref?

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Veikko Soininen
      Posted by Veikko Soininen
      on Jun 19 2012 04:56 AM
      Prodigy150 points

      I tried way longer sampling time but with same result. I'll have to measure the Vref once I have time to do that. I'm not sure what TLV is used for. I quickly checked through the datasheet what it means, but didn't know what to see from there.

      Thanks for the help so far!

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Argail
      Posted by Argail
      on Jun 19 2012 05:15 AM
      Expert1510 points

      There is some calibration values in the TLV, but your measures are way out of range;)

      Considering:

      "Edit: Getting out of ideas... I changed to use the ADC reference 2,5V and when expecting 3,538 V, got the result of 141. This turns out as 141*2,5*2/255 = 2,78 V. So not better at all..."

      Is seems it's not a reference problem, as you measure the same voltage the two times. It's perhaps your multimeter which is wrong xD


      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Veikko Soininen
      Posted by Veikko Soininen
      on Jun 19 2012 05:30 AM
      Prodigy150 points

      I've tried with lab power supply with voltage reading too, so unfortunately the answer isn't that easy. :)

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Vidya Sagar Kantamneni
      Posted by Vidya Sagar Kantamneni
      on Jun 19 2012 06:27 AM
      Intellectual935 points

      Argail
      I changed to use the ADC reference 2,5V and when expecting 3,538 V, got the result of 141.

      Dear Veikko, 

      To my knowledge if you can use a reference of 2.5V, you can test a maximum of 2.5V. I am using ADC on 5438 experimenter board and I give a reference of 3.3V, the maximum output of ADC corresponds to 3V. 

      Please check this and let me know. Hope this helps.

      Best regards, 

      Vidya

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Veikko Soininen
      Posted by Veikko Soininen
      on Jun 19 2012 06:31 AM
      Prodigy150 points

      Yeah, it is true that you can measure the Vref at max. But for that reason the ADC channel B (ADC12INCH_11) is Vcc divided by two (with two equal resistors). Therefore I'm actually measuring half of the 3,538 V (which is 1,769 V). The result just needs to be multiplied by two to get the result. It is included in the calculation formula.

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Veikko Soininen
      Posted by Veikko Soininen
      on Jun 19 2012 07:05 AM
      Prodigy150 points

      Getting interesting!

      I set the ADC reference from ADC registers and disabled the Shared reference. Then I outputted the Vref+ from P2.5. I got mysterious results.

      When I set the 2,5 V reference, the measured value was 2,11 V and when I set the 1,5 V reference, the measured value was 1,26 V. Whats going on? The uC pin is unused and unrouted, so it doesn't sink too much current or so. Why the reference levels are so low? These were measured with 2,9+ V power supply. With 3,4 V power supply I get better results, but the 2,5V reference is still bit low 2,4V.

      Test lines:

          REFCTL0 = 0;

          ADC12CTL0 = ADC12ON + ADC12REFON;           // Turn on ADC12, set sampling time (0000 = 4clk), 1,5 V ref

          ADC12CTL2 |= ADC12REFOUT;                                // Ref out

          PMAPPWD = 0x02D52;                                        // Get write-access to port mapping regs
          PMAPCTL |= PMAPRECFG;                                    // Do NOT lock the port settings.
          P2MAP5 = PM_ANALOG;                                        // Analog data out
          PMAPPWD = 0;                                            // Lock port mapping registers

          P2SEL |= BIT5;                                            // Select pin function

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Jens-Michael Gross
      Posted by Jens-Michael Gross
      on Jun 20 2012 11:10 AM
      Guru139900 points

      Veikko Soininen1
      When I set the 2,5 V reference, the measured value was 2,11 V and when I set the 1,5 V reference, the measured value was 1,26 V

      That's really weird. Since for a lower reference, the conversion result should be higher.
      So if you have 1.5V reference and a measured voltage of 1V, the conversion result is 0xAA (8 bit). If the reference were 1.26V isntead, you'd read 0xCB for 1V applied voltage.
      A reading that is lower than expected measn that either the measured voltage is lower than expected, the formula is wrong or the reference is higher than expected.

      However, I wouldn't use a multimeter to measure any MCU-related voltage if I have a problem. Use a scope instead. Multimeters do averaging and low-pass high-frequency distortions, while those things may well influence your readings.

      P.s.:

      Veikko Soininen1
          P2MAP5 = PM_ANALOG;                                        // Analog data out
      No. It does not enable analog funciton, it disables all digital functions oin this pin. Analog functions are always present and available indepenently of the port mapping. However, disablign the digital pins reduces influence on the pin if there is a high-impedance signal source.

      _____________________________________
      Before posting bug reports or ask for help, do at least quick scan over this article. It applies to any kind of problem reporting. On any forum. And/or look here.
      If you cannot discuss your problem in the public, feel free to start a private conversation: click on my name and then 'start conversation'. But please do so only if you really cannot do it in a public thread, as I usually read all threads. And I prefer to answer where others can profit from it (or contribute to it) too.

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    12
    TI E2E™ Community
    • Support Forums
    • Blogs
    • Videos
    • Groups
    • Site Support & Feedback
    • Settings
    TI E2E™ Community Groups
    • TI University Program
    • Make the Switch
    • Microcontroller Projects
    • Motor Drive & Control
    Other Communities
    • Deyisupport
    • Designsomething.org
    • beagleboard.org
    • TI on Element 14
    • TI on TechXchangeSM
    Other Technical & Support Resources
    • WEBENCH® Design Center
    • Product Information Centers
    • Technical Documents
    • TI Design Network
    • TI Technical Articles
    • TI Training

    All content and materials on this site are provided "as is". TI and its respective suppliers and providers of content make no representations about the suitability of these materials for any purpose and disclaim all warranties and conditions with regard to these materials, including but not limited to all implied warranties and conditions of merchantability, fitness for a particular purpose, title and non-infringement of any third party intellectual property right. TI and its respective suppliers and providers of content make no representations about the suitability of these materials for any purpose and disclaim all warranties and conditions with respect to these materials. No license, either express or implied, by estoppel or otherwise, is granted by TI. Use of the information on this site may require a license from a third party, or a license from TI.

    Content on this site may contain or be subject to specific guidelines or limitations on use. All postings and use of the content on this site are subject to the Terms of Use of the site; third parties using this content agree to abide by any limitations or guidelines and to comply with the Terms of Use of this site. TI, its suppliers and providers of content reserve the right to make corrections, deletions, modifications, enhancements, improvements and other changes to the content and materials, its products, programs and services at any time or to move or discontinue any content, products, programs, or services without notice.

    Follow Us Texas Instruments on Facebook Texas Instruments on Twitter Texas Instruments on LinkedIn Texas Instruments on Google+
    TI Worldwide | Contact Us | my.TI Login | Site Map | Corporate Citizenship | mobile m.ti.com (Mobile Version)

    TI is a global semiconductor design and manufacturing company. Innovate with 100,000+ analog ICs and
    embedded processors, along with software, tools and the industry’s largest sales/support staff.

    © Copyright 1995-2013 Texas Instruments Incorporated. All rights reserved.
    Trademarks | Privacy Policy | Terms of Use