//******************************************************************************
// Demo - MSP430F169, ADC12, Sample Pressure Sensor on A2 w/ External Reference
//
// Description: This code will configure a TI MSP430F169 to sample an analog
// voltage signal from a pressure transducer (Model MSP-600-500-P-3-D-4 from
// Measurement Specialties Inc.) using the ADC12.  The output of this pressure 
// sensor ranges from 0.5V-4.5V.  This must be lowered to a max voltage of 2.5V 
// since Vref+ is 2.5V (anything above this voltage will read as 0xFFFF).  A 
// voltage divder with a ratio of 0.556 will bring 4.5V down to 2.5V and 0.5V to
// 0.278V.  This ratio can be acheived with 20k and 25k resistors.  First the 
// program configures P6.2/A2 (pin 61) for use with the ADC12 module.  Next the 
// ADC12 is turned on with sample/hold pulse mode, the sampling time is set for 
// 256 ADC12CLK cycles and ADC12MEM0 is configured to use the external voltage 
// reference (2.5V) while sampling channel 2.  Finally conversions are enabled.  
// A conversion is started and stores the result into the variable "pressure".  
// Another conversion is started but this time adding the result to the last 
// result.  This is repeated ten times and then the average is found by dividing 
// the final result by ten.  The average of ten conversions is then printed out 
// in the format "Average is 0xXXXX".  If the user uses the voltage divider
// described above and the pressure being read is normal air pressure (1 ATM or
// 14.7 psi) then the result should be around 0x1C7.  To find the the 
// corresponding analog voltage convert this hex number to decimal, multipy by 
// 2.5 (external voltage reference) and divide by 4095 (2^12;0-4095)  Open the 
// terminal I/O window while debugging to see the printed results.  The flag 
// ADC12BUSY is used for flow control.
//
// Stefan Gadomski
// UC Santa Cruz
// June 3, 2007
// Senior Design 123B
// Built with IAR Embedded Workbench Version: 3.42A
//******************************************************************************

#include  <msp430x16x.h>
#include <stdio.h>

unsigned int i = 0;
unsigned int pressure;

void main(void)
{
  WDTCTL = WDTPW+WDTHOLD;                   // Stop watchdog timer
  P6SEL |= 0x04;                            // Enable A/D channel A2
  ADC12CTL0 = ADC12ON+SHT0_4;               // Turn on ADC12, set sampling time
  ADC12CTL1 = SHP;                          // Use sampling timer
  ADC12MCTL0 = SREF_2 + INCH_2;             // Vr+ = VeREF+ (external)
  ADC12CTL0 |= ENC;                         // Enable conversions

  while (1)
  {
    ADC12CTL0 |= ADC12SC;                         // Start conversion
    while (ADC12CTL1 & ADC12BUSY == ADC12BUSY)    // If ADC is busy do nothing
    {
    }
    i++;
    pressure = ADC12MEM0;                      // Store result
    while (i < 10)
    {
      ADC12CTL0 |= ADC12SC;                       // Start conversion
      while (ADC12CTL1 & ADC12BUSY == ADC12BUSY)  // If ADC is busy do nothing
      {
      }
      pressure = pressure + ADC12MEM0;  // Add new result to last result
      i++;
    }
    pressure = pressure / 10;
    printf ("Average is 0x%x\n", pressure);
    printf ("\n");
    i = 0;
  }
}
