20100801

MSP430 Launchpad - Low Power Thermometer Code

TI has a really low cost development platform called launchpad that I have been playing with recently.  It is $4.30 with free shipping until august and it has some crazy low power capabilities.  Below is my first program beyond a blinky.  It sleeps in the second to lowest power mode then wakes up, reads the temperature, counts it out on 2 LEDs on the dev board then goes back to sleep.  Other than driving the LEDs, this should use so little power that batteries could conceivably die from age as fast as they are used if I cranked the sleep all the way up.  It also has the advantage of working on a launchpad in factory condition with no additional parts.

Like I said, I am new at this, and has been at least 8 years since I have even looked at anything that was in a base language so I am certain I have made some mistakes.  If you see some let me know.

I don't claim to understand all of this perfectly yet.  Why you need a sleep mode to read the ADC, or what all the ADC flags actually mean are a bit unclear.  Those portions are taken from TI examples.

// I am new at this - it likely has bad errors
//
// the goal was to sleep as long as possible, read temp
// then display it through flashing the launchpad leds
//
// when not flashing or measuring go to the lowest power reasonable
//

#include  "msp430x20x2.h"


//unsigned short cc = 65470;    //longest CCR0
//unsigned short cc = 1000;       //debugging CCR0 value
unsigned short cc = 10000;       //interval between tests - CCR0 value

long IntDegF;

//this is all setup
void main(void)
{
  WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT
  BCSCTL3 |= LFXT1S_2;                      // make clock work without crystal
  P1DIR |= 65;                              // P1.0 & 6 output
  P1OUT = 65;                               // lights on so we know we are started
  CCTL0 = CCIE;                             // CCR0 interrupt enabled
  CCR0 = cc;                                // set the long interval
  TACTL = TASSEL_1 + MC_1 + ID_3 ;          // SMCLK, upmode, / 8

  _BIS_SR(LPM3_bits + GIE);                 // Enter LPM3 w/ interrupt
}


//this captures the temp value
void dotemp(void)
{
  WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT
  ADC10CTL1 = INCH_10 + ADC10DIV_3;         // Temp Sensor ADC10CLK/4
  ADC10CTL0 = SREF_1 + ADC10SHT_3 + REFON + ADC10ON + ADC10IE;
  ADC10CTL0 |= ENC + ADC10SC;             // Sampling and conversion start
  __bis_SR_register(CPUOFF + GIE);        // LPM0 with interrupts enabled

  // oF = ((A10/1024)*1500mV)-923mV)*1/1.97mV = A10*761/1024 - 468
  long temp = ADC10MEM;                         // raw adc temp
  IntDegF = ((temp - 630) * 761) / 1024;
 
  CCR0 = 999;                             // set short interval for display
}

// ADC10 interrupt service routine - I don't entirely understand the need here
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR (void)
{
  __bic_SR_register_on_exit(CPUOFF);        // Clear CPUOFF bit from 0(SR)
}

// Timer A0 interrupt service routine - this is the main flow control
#pragma vector=TIMERA0_VECTOR
__interrupt void Timer_A (void)
{
  if (CCR0<1000)            //if we are on the short interval we are blinking
    if (P1OUT > 0)          //if the leds are on turn them off
    {
      P1OUT = 0;
      CCR0 = 600;
    }
    else                    //time to count down
      if (IntDegF == 0)     //if we have counted to zero back to the long count
        CCR0 = cc;
      else
      {
        CCR0 = 100;
        if (IntDegF > 9)    //if there are more than 10 left use the first led
        {
          P1OUT = 1;        //first led on
          IntDegF -= 10;    //decrement 10
        }
        else
        {
          P1OUT = 64;       //less than ten, second led
          IntDegF -= 1;     //decrement one
        }
      }
  else
    dotemp();               //do the temp measure
}


Firefox Feedly RSS option

If you use Firefox with a RSS button and want the default RSS page to offer a Feedly option here is what you need to do: go to the about:c...