//******************************************************************************
//  MSP430F20x2 Demo - Simple read acceleration data sample program. 
//

//How To Test:
//
// 1) Download code to a 'F20x2' device 
// 2) Run the code:
//         a) The test will indicate proper communication with the KXTE9 by
//            toggling the tilt and activity LEDs
//
//         b) The best method to view the proper functionality is to run to
//            the desired line of code in the code section of main and
//            view the Xaxis, Yaxis, and Zaxis variables in the Watch
//            Window. 
//
//  Please forward any questions regarding this code and or accelerometer 
//  products to apps@kionix.com
//            
//        
//
//    Author: IXC
//    -21 NOV 2008

//******************************************************************************
#include "msp430x20x2.h"
unsigned int Xout();
unsigned int Yout();
unsigned int Zout();
  
  unsigned int Xaxis;
  unsigned int Yaxis;
  unsigned int Zaxis;

void main(void)
{

  WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT
  ADC10CTL0 = ADC10SHT_2 + ADC10ON + ADC10IE; // ADC10ON, interrupt enabled
  P1DIR |= 0x01;                            // Set P1.0 to output direction

  

  
  while(1){
    //get acceleration data
  Xaxis = Xout();
  Yaxis = Yout();
  Zaxis = Zout();
  }
}
  

// ADC10 interrupt service routine
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR(void)
{
  __bic_SR_register_on_exit(CPUOFF);        // Clear CPUOFF bit from 0(SR)
}



unsigned int Xout()
{
    unsigned int output;
    ADC10CTL0 &= ~ENC;    //ADC10 control bits can only be modified
    ADC10CTL1 = INCH_4;   //when ENC = 0
    ADC10AE0 = 0x10;                         // PA.4 ADC option select
      ADC10CTL0 |= ENC + ADC10SC;             // Sampling and conversion start
    __bis_SR_register(CPUOFF + GIE);        // LPM0, ADC10_ISR will force exit
    output = ADC10MEM;
    return output;
}

unsigned int Yout()
{
    unsigned int output;
    ADC10CTL0 &= ~ENC;      //ADC10 control bits can only be modified 
    ADC10CTL1 = INCH_5;     //when ENC = 0
    ADC10AE0 = 0x20;                         // PA.5 ADC option select
      ADC10CTL0 |= ENC + ADC10SC;             // Sampling and conversion start
    __bis_SR_register(CPUOFF + GIE);        // LPM0, ADC10_ISR will force exit
    output = ADC10MEM;
    return output;
}

unsigned int Zout()
{
    unsigned int output;
    ADC10CTL0 &= ~ENC;    //ADC10 control bits can only be modified
    ADC10CTL1 = INCH_6;   //when ENC = 0;
    ADC10AE0 = 0x40;                         // PA.6 ADC option select
      ADC10CTL0 |= ENC + ADC10SC;             // Sampling and conversion start
    __bis_SR_register(CPUOFF + GIE);        // LPM0, ADC10_ISR will force exit
    output = ADC10MEM;
    return output;
}

