This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

MSP430F5509 ADC Error reading

Other Parts Discussed in Thread: MSP430F5509

Hi I am a newbie in using MSP430F5509 and I have a problem in reading the ADC input value.

Here is my code. 

#include "msp430.h"

unsigned int ADC_Result;

int main()
{
  volatile unsigned int i;

  WDTCTL = WDTPW + WDTHOLD;                   // Stop WDT
  P6SEL |= BIT0;

  // Configure ADC
  ADC10CTL0 |= ADC10SHT_2 + ADC10ON;        // ADC10ON, S&H=16 ADC clks
  ADC10CTL1 |= ADC10SHP;                    // ADCCLK = MODOSC; sampling timer
  ADC10CTL2 |= ADC10RES;                    // 10-bit conversion results
  ADC10MCTL0 |= ADC10INCH_0;                // A0 ADC input select; Vref=AVCC
  ADC10IE |= ADC10IE0;                      // Enable ADC conv complete interrupt
  ADC10CTL0 |= ADC10ENC;


  P1DIR |= BIT0;                            // Set P1.0/LED to output direction
  P1OUT&= ~BIT0;

  while (1)
  {
    ADC10CTL0 |= ADC10ENC + ADC10SC;        // Sampling and conversion start

    __bis_SR_register(LPM0_bits + GIE);     // LPM0, ADC10_ISR will force exit
    __no_operation();                       // For debug only

    if (ADC_Result < 255)
      P1OUT &= ~BIT0;                       // Clear P1.0 LED off
    else
      P1OUT |= BIT0;                        // Set P1.0 LED on
  }
}

// ADC10 interrupt service routine
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR(void)
{
  switch(__even_in_range(ADC10IV,12))
  {
    case  0: break;                          // No interrupt
    case  2: break;                          // conversion result overflow
    case  4: break;                          // conversion time overflow
    case  6: break;                          // ADC10HI
    case  8: break;                          // ADC10LO
    case 10: break;                          // ADC10IN
    case 12: ADC_Result = ADC10MEM0;
             __bic_SR_register_on_exit(LPM0_bits);
             break;                          // Clear LPM0_bits from 0(SR)
    default: break;
  }
}

Upon debugging the contents of ADC_Result, I am getting a value of "613" although Analog input 0 is directly connected to the GND.  

Thank you very much in advance for your help.

  • Jeward Magat said:
    unsigned int ADC_Result;

    You should mark it volatile, as it is 'magically' changed by your ISR in the background and the compiler doesn't know this while compiling your main code.

    Jeward Magat said:
    ADC10MCTL0 |= ADC10INCH_0; // A0 ADC input select; Vref=AVCC

    You shouldn't use |= here. First, ADC10INCH0 is 0, which makes the operation superfluous (x |= 0 -> x). Also, using any different channel would set the bits of the selected channel, but would not clear any bits already set, resulting in the wrong channel selected if you switch between different ones.

    Are you sure your input is really connected to GND? Check with a scope.

**Attention** This is a public forum