Jump to content
43oh

how read the battery voltage


Recommended Posts

Hi all,

@semicolo: Thanks a lot for the code.

 

but if the the voltage is < 3V i think it can't read the value because it needs a 1.5V reference.

The code in the first post is quite ok.

I think I need only to reset an ADC register in order to restart in a clean state. But I don't know what I've to reset.

Link to post
Share on other sites

Voltage regulators can only lower voltages...

 

For analogRead the value isn't really the Energia pin# but rather the ADC channel (INCH_xx constant), 11 just happens to be an internal "(Vcc-Vss)/2" generated channel used for measuring the MSP430's Vcc voltage.

 

Sent from my Galaxy Note II with Tapatalk 4

 

 

Hi, thanx for the explanation.  If one wants to use the #11 pin for power measurement one should not use it for other purpose or it will mess with measurement, I suppose.

Link to post
Share on other sites

I was just making a quick test. Something like this should do the same as your code and be easier to read than working with the MSP430 registers:

#define ANALOG_HALFVCC_INPUT 11

void setup() 
{ 
  Serial.begin(9600); // msp430g2231 must use 4800
} 

// returns VCC in millivolts
int getVCC() {
  // start with the 1.5V internal reference
  analogReference(INTERNAL1V5);
  int data = analogRead(ANALOG_HALFVCC_INPUT);
  // if overflow, VCC is > 3V, switch to the 2.5V reference
  if (data==0x3ff) {
    analogReference(INTERNAL2V5);
    data = (int)map(analogRead(ANALOG_HALFVCC_INPUT), 0, 1023, 0, 5000);
  } else {
    data = (int)map(data, 0, 1023, 0, 3000);
  }
  return data;  
}

void loop() 
{ 
  Serial.print("VCC value:");
  int data = getVCC();
  Serial.println (data);
  delay(1000);
}

Would be nice to have a standard constant for reading VCC

Link to post
Share on other sites

Hi, thanx for the explanation.  If one wants to use the #11 pin for power measurement one should not use it for other purpose or it will mess with measurement, I suppose.

It's actually not like that ... analog "pin" 11 doesn't correspond to pin#11 on the MCU's leads, it corresponds to an internal virtual channel.  So digitalWrite/digitalRead type of operations on pin 11 can still happen while you're reading "analog pin" 11.

 

Analog "pins" and digital "pins" are actually separate concepts in Energia, but it just so happens that the analog pins correspond to the digital pins for the first 8 (A0-A7).

 

Example from Energia's pins_energia.h for the launchpad:

static const uint8_t A0  = 0;
static const uint8_t A1  = 1;
static const uint8_t A2  = 2;
static const uint8_t A3  = 3;
static const uint8_t A4  = 4;
static const uint8_t A5  = 5;
static const uint8_t A6  = 6;
static const uint8_t A7  = 7;
static const uint8_t A10 = 10; // special. This is the internal temp sensor

"analog pin" 10, in fact, is the temperature sensor inside the chip and has nothing to do with "digital" pin #10.  It's just 0-7 that correspond.  11 isn't listed there but it happens to be the "(Vcc-Vss)/2" channel.

 

What actually happens is the digital pins get resolved by some lookup tables in pins_energia.h into a PxOUT / BITx combination for manipulating the digital states, but analog input channels resolve into INCH_xx values which are written to the ADC10CTL1 register before an ADC conversion (analog read) is performed.

Link to post
Share on other sites
What actually happens is the digital pins get resolved by some lookup tables in pins_energia.h into a PxOUT / BITx combination for manipulating the digital states, but analog input channels resolve into INCH_xx values which are written to the ADC10CTL1 register before an ADC conversion (analog read) is performed.

 

Not the first time I get confused and mess logical and physical pin assignments. My bad

Link to post
Share on other sites

Just tried this with the launchpad connected to a li-ion battery, the accuracy drops a lot when the 1.5V reference is used, I'll try to find why.

VCC value:3.103
VCC value:3.103
VCC value:3.98
VCC value:3.103
VCC value:3.93
VCC value:3.98
VCC value:3.93
VCC value:3.98
VCC value:3.93

Instead of finding about 3V, it jumps to 4V, maybe there's a bug in the code.

Link to post
Share on other sites
  • 8 months later...

Another variety of the test routine, this time on the F5529 Launchpad.  appears to be off by a factor of 2.  (5000 is not the right value for the 'map' function?)

 

#include "Energia.h"
 
 
#define ANALOG_HALFVCC_INPUT 11
 
void setup()
{
// start with the 2.5V internal reference
  analogReference(INTERNAL2V5);
  Serial.begin(115200); // msp430f5529 Launchpad
}
 
// returns VCC in millivolts
int getVCC() {
   int rawdata = analogRead(ANALOG_HALFVCC_INPUT);
   int data = (int)map(rawdata, 0, 4096, 0, 5000);  // Modified for ADC12_A
   Serial.print("ADC12_A Value: 0x");
   Serial.print(rawdata, HEX);
   Serial.print("\t VCC: ");
   Serial.println (data, DEC);
   Serial.print("ADC12REF2_5V: 0x");
   Serial.println(ADC12REF2_5V, HEX);
   Serial.print("ADC12CTL0: 0x");
   Serial.println(ADC12CTL0, HEX);
   Serial.println("---end of ADC CH11 read---");
  return 0;
}
 
void loop()
{
  int data = getVCC();
 
  delay(1000);
}
/* --sample output --
 
ADC12_A Value: 0x7FF     VCC: 2498
ADC12REF2_5V: 0x40
ADC12CTL0: 0x412
---end of ADC CH11 read---
ADC12_A Value: 0x7FF     VCC: 2498
ADC12REF2_5V: 0x40
ADC12CTL0: 0x412
---end of ADC CH11 read---
ADC12_A Value: 0x7FF     VCC: 2498
ADC12REF2_5V: 0x40
ADC12CTL0: 0x412
---end of ADC CH11 read---
*/
Link to post
Share on other sites
  • 1 year later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...