Ah.. Battery. The Best thing about MSP430 uC is that one doesnt need to give a second thought as far as powering the controller is considered. This guy accepts almost everything below 4V  (when nicely put and harshly i have used it at 4.2V which  is Voc of a 3.7 Li-ion battery used in Cordless phones in this part of the world). Added to it, one can exploit the low power modes to conserve the battery power.

That all been said..One question still persists in every battery powered device is “How Much I`ve Got?”.  This question has been answered by the designers of the chip by  incorporating an internal voltage sensor into the chip,whats better is that the output can also be read as ADC input which opens a lot of great possibilities of voltage processing to disable certain voltage related features on a product/project.

Now lets get digging . A closer look at the user`s guide page no.-561 :

pic1

So, the 11th channel is (Vcc-Vss)/2. Vss is GND or 0V . Hence, What the ADC will return will eventually be  Vcc/2 .

Thats pretty much, now you need to just code and read this value from the internal channel always or upon request. But there still is a catch.. Internal reference is  required because if you choose the default Vcc and Vss reference which will pretty much fall as you use your gadget so measuring the voltage will be a lunatic idea in this case. For the purpose , we have two voltage references in the device i.e. 1.5V and 2.5V. Its obvious that we will choose 2.5V as if Vcc=3.7V , then the ADC output incase of 1.5V ref will be 3.7/2=1.85 .So, the value will ride on 1023 all the time , hence , we choose the 2.5V reference.

pic2

However, there seems to be another draw back of 2.5 V reference, which is that at the 2.2V point or voltages below 2.5V , the reference will not be generated. But that can be neglected as most hobby parts dont work at such low voltages. A more robust strategy can be adopted as follows: Whenever the voltage falls below/nears the 2.5V point, we can quickly stop the ADC and change the reference to 1.5V

Further, on design pov , battery status need not be always read, it can be triggered by suppose a button press and indicated by LED flashes this is when you require the minimalist hardware in place for this task.. Of course we can use LCD displays to display exact voltage but thats usually an overkill as Battery is perceived better as a level than a value.

Now lets get started and code the application, i wont cover the idea of switching the threshold/reference as far as code is concerned

The code is available on my git here. This is a threshold one only!

Here is the code to blink the LED based on the voltage levels:


//Coded by: Rohit Gupta on 16th June`13 at 0127hrs

#include<msp430g2553.h>

#define SWC BIT3
#define LED BIT0
volatile float voltage;

void blink(int t)
{   int i;
    P1OUT=0;
    P1DIR|=LED;
    P1OUT|=LED;
	for (i=0;i<t;i++)
	{
	P1OUT|=LED;
	__delay_cycles(500000);
	P1OUT&=~LED;
    __delay_cycles(500000);

	}

}
void delay_ms(unsigned int ms ) //Delay_MS
{
      unsigned int i;
      for (i = 0; i<= ms; i++)
        __delay_cycles(5000); //Built-in function that suspends the execution for 500 cycles
}
void main()
{
	WDTCTL = WDTPW + WDTHOLD;
	 BCSCTL1 = CALBC1_1MHZ;
	 DCOCTL = CALDCO_1MHZ;
	P1DIR&=~SWC; // Input
    P1IE|=SWC; // Interrupt Enabled
    P1REN|=SWC; // Internal Pull Ups On // Do not use internal pull ups on Launchpad V1.4
    P1OUT|=SWC; // Yay! Its a pull up!
    P1IFG&=~SWC; //Clear Flag if not set!

    __bis_SR_register(GIE);

	while(1)
	{

	}
}
#pragma vector=PORT1_VECTOR  //ISR Sub-routine
__interrupt void Port_1(void)
{   unsigned int value=0;
	if(P1IFG&SWC)
	{
		ADC10CTL0=SREF_1+REFON+REF2_5V+ADC10ON+ADC10SHT_3; // Using internal reference of 2.5V
	    ADC10CTL1=INCH_11+ADC10DIV_3;   // Internal Voltage
	    __delay_cycles(1000);                   // Wait for ADC Ref to settle
	    ADC10CTL0 |= ENC + ADC10SC;             // Sampling and conversion start
	   while (ADC10CTL1 & BUSY);             //waiting for conversion to finish
	        value = ADC10MEM;                    //fetching adc value
	       ADC10CTL0&=~(ENC+ADC10ON);
	        voltage=(value*2*2.5)/1023;           // Converting To Legit Voltage Values in Volts

	  if(voltage>=3.5)
	  {
		  blink(5);
	  }
	  else if (voltage>=3.0 && voltage<3.5 )
	  {
		  blink(4);
	  }
	  else if (voltage>=2.7 && voltage<3.0)
	  {
		  blink(3);
	  }
	  else if (voltage>=2.2 && voltage<2.7)
	  {
		  blink(2);
	  }
	  else
	  {
		  blink(1);
	  }

	  P1IFG&=~SWC;	 //clear flag
	}

}

The video grab when you run the code and “do the watch expression thing” to see the value of voltage variable is as follows:

pic3

PS: This method is applicable only when you are measuring battery directly connected across the power pins of the MSP430 not thru a regulator. For that case, you will need to use the resistor divider+snubber circuit into an ADC Channel externally.

Here is a detailed video from compiling the above code to running it and pressing the button. You can certainly test by bread-boarding the MSP430 and apply different voltages as i did with my bread board power supply

Cheers,

Rohit Gupta

Advertisements

One thought on “Tutorial:How Much I`ve got Left- The MSP430 Way

  1. You are Amazing!
    The way you’ve brought out a simple, yet so important concept here so easily, is remarkable!
    Good Going!
    Looking forward to more such interesting posts!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s