Jump to content
43oh

how read the battery voltage


Recommended Posts

hi,

I've a msp430g2253 board powered by a battery. I'd like to read the voltage of the battery.

I've found a function online (http://blog.elevendroids.com/2013/06/code-recipe-reading-msp430-power-supply-voltage-level/).

uint16_t Msp430_GetSupplyVoltage(void)
{
	uint16_t raw_value;
	// first attempt - measure Vcc/2 with 1.5V reference (Vcc < 3V )
	ADC10CTL0 = SREF_1 | REFON | ADC10SHT_2 | ADC10SR | ADC10ON;
	ADC10CTL1 = INCH_11 | SHS_0 | ADC10DIV_0 | ADC10SSEL_0;
	// start conversion and wait for it
	ADC10CTL0 |= ENC | ADC10SC;
	while (ADC10CTL1 & ADC10BUSY) ;
	// stop conversion and turn off ADC
	ADC10CTL0 &= ~ENC;
	ADC10CTL0 &= ~(ADC10IFG | ADC10ON | REFON);
	raw_value = ADC10MEM;
	// check for overflow
	if (raw_value == 0x3ff) {
		// switch range - use 2.5V reference (Vcc >= 3V)
		ADC10CTL0 = SREF_1 | REF2_5V | REFON | ADC10SHT_2 | ADC10SR | ADC10ON;
		// start conversion and wait for it
		ADC10CTL0 |= ENC | ADC10SC;
		while (ADC10CTL1 & ADC10BUSY) ;
		raw_value = ADC10MEM;
		// end conversion and turn off ADC
		ADC10CTL0 &= ~ENC;
		ADC10CTL0 &= ~(ADC10IFG | ADC10ON | REFON);
		// convert value to mV
		return ((uint32_t)raw_value * 5000) / 1024;
	} else
		return ((uint32_t)raw_value * 3000) / 1024;
}

It works but the first time I call the function it uses the 1.5V reference (Vcc < 3V ) and the second time it uses 2.5V reference (Vcc >= 3V).

The real voltage is 3,12V

What is wrong?

Thank you in advance.

 

Regards

 

ilpaso

 

Link to post
Share on other sites

From the comments, it seems you should use a voltage divider (for example maybe 2 100K resistors), so you have VCC/2 on your analog input.

The first try is using the 1.5V internal reference, so it's good for voltage below 3V.

If the conversion returns 0x3FF, it means that VCC is >= 3V, so the software switches to the 2.5V reference that allows up to 5V for VCC (but of course you can't go higher than 4V on these chips).

Link to post
Share on other sites

 

It works but the first time I call the function it uses the 1.5V reference (Vcc < 3V ) and the second time it uses 2.5V reference (Vcc >= 3V).

The real voltage is 3,12V

What is wrong?

So are you asking why it's using the  1.5V reference and then the 2.5V one? But in fact it's working ok?

Link to post
Share on other sites

It seems to be working, the following sketch gives me 3.5V with USB VCC

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

  Serial.println("VCC value:");
  analogReference(INTERNAL2V5);
} 

void loop() 
{ 
  long data = map(analogRead(11), 0, 1023, 0, 500);
  Serial.print (data/100);
  Serial.print (".");
  Serial.println (data%100); // this is wrong if the tens are 0
  delay(1000);
} 
Link to post
Share on other sites

Not if there a voltage regulator embedded?

 

And why pin 11?

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

 

 

Link to post
Share on other sites

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...