//******************************************************************************
// Demo - MSP430F169, ADC12, Sample Thermistor on A1 with External Reference
//
// Description: This code will configure a TI MSP430F169 to sample an analog
// voltage signal from a thermal bridge consisting of a 10k resistor and a 10k
// thermistor (Model QT06001-055 from Quality Thermistor) using the ADC12.
// First the program configures P6.1/A1 (pin 60) for use with the ADC12 module.
// Next the ADC12 is turned on with sample/hold pulse mode, the sampling time 
// is set for 4 ADC12CLK cycles and ADC12MEM0 is configured to use the external
// voltage reference (2.5V) while sampling channel 1.  Finally conversions are
// enabled.  A conversion is started and stores the result into the variable
// "temp".  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 temperature is at room
// temperature (~72F) then the result should be around 0x940.  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 temp;

void main(void)
{
  WDTCTL = WDTPW+WDTHOLD;                   // Stop watchdog timer
  P6SEL |= 0x02;                            // Enable A/D channel A1
  ADC12CTL0 = ADC12ON+SHT0_0;               // Turn on ADC12, set sampling time
  ADC12CTL1 = SHP;                          // Use sampling timer
  ADC12MCTL0 = SREF_2 + INCH_1;             // Vr+ = VeREF+ (external)
  ADC12CTL0 |= ENC;                         // Enable conversions

  while (1)
  {
    ADC12CTL0 |= ADC12SC;                           // Start conversion
    while (ADC12CTL1 & ADC12BUSY == ADC12BUSY);     // If ADC is busy do nothing
    i++;
    temp = ADC12MEM0;                               // Store result
    while (i < 10)
    {
      ADC12CTL0 |= ADC12SC;                         // Start conversion
      while (ADC12CTL1 & ADC12BUSY == ADC12BUSY);   // If ADC is busy do nothing
      temp = temp + ADC12MEM0;                      // Add new result to last result
      i++;
    }
    temp = temp / 10;
    printf ("Average is 0x%x\n", temp);
    printf ("\n");
    i = 0;
  }
}
