Quantcast
Channel: MSP430 Technical Forums
Viewing all articles
Browse latest Browse all 2077

how read the battery voltage

$
0
0

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

 


Viewing all articles
Browse latest Browse all 2077

Trending Articles