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.

MSP430F5438 - ADC Setup Verify



I am attempting to use the ADC on my mspf5438 to trigger an LED at this point and later on a initiate UART communication.  For now though I am trying to just light the LED.  I am using the following code but the input voltage seems to be different than what I'm actually getting from conversions.  My input is a sine wave with a changing peak to peak value.  It has an average value of 2.1V.  I want to use an externally supplied Vref+ of 3.3V and Vref- of 0V, which I have supplied to pins 9 and 10 of the MSP430.  My input to the ADC is denoted as P6.5/A5, which is pin 2 on the msp.  My LED is connected to P6.1/A1, pin 98.  I have a few specific questions in addition to asking someone to review my code.

1. Do I need to enable pins 9 and 10 for the reference voltage as inputs in addition to supplying the voltage?

2. If I set a state for a specific port, such as P6OUT |= 0X02, does it only affect the pins which I have configured as outputs?

/////////////////////ADC Setup//////////////////////////////
  ADC12CTL0 = ADC12SHT02 + ADC12ON + ADC12REFON;       // Sampling time, ADC12 on
  ADC12CTL1 = ADC12SHP;                                                                  // Use sampling timer
  ADC12IE = 0x01;                                                                                     // Enable interrupt
 
  ADC12CTL0 |= ADC12ENC;
 
  P6SEL |= 0x20;                            // P6.5 ADC option select
  P6DIR |= 0x02;                            // P6.1 output for LED X1
  P6OUT = 0x00;                            //Start LED in off state


while(1)                                     //Continuous conversions?
  {
    ADC12CTL0 |= ADC12SC;                   // Start sampling/conversion
       
    __bis_SR_register(LPM0_bits + GIE);     // LPM0, ADC12_ISR will force exit
    __no_operation();                                         // For debugger
  }
 
}                //End of main()
 

#pragma vector = ADC12_VECTOR
__interrupt void ADC12_ISR(void)
{
 

  switch(__even_in_range(ADC12IV,34))
  {
  case  0: break;                           // Vector  0:  No interrupt
  case  2: break;                           // Vector  2:  ADC overflow
  case  4: break;                           // Vector  4:  ADC timing overflow
  case  6:                                       // Vector  6:  ADC12IFG0
  
    last_state = ADC12MEM0;                 //This little bit of code lets me
    if(last_state > new_state)                  //store my highest value converted
    {                                                              //for later review
      new_state = last_state;
    }
         
    if(ADC12MEM0>= 0X9BB)                 // If voltage is larger than this number, light LED
      P6OUT |= 0x02;                                 // P6.1 = 1
    
    else
      P6OUT &= ~0x02;                       // NAND P6 Port with 0x02, should turn
                                                             //off LED ON P6.1
   
    __bic_SR_register_on_exit(LPM0_bits);   // Exit active CPU
    default: break;
  }
}

  • The external reference voltage does need to be selected inside the ADC12MCTLx register using the ADC12SREF (= 110b) bits. You would also want to set the PSEL bits for the I/O to select the analog function.

    See the adc12_05 code example on www.ti.com/msp430 > Code examples > F5438 C Code Examples.

    P6OUT |= 0X02 will affect the output if you have enabled the respective P6DIR bit to reflect an output GPIO function and have not set the PSELx or PREN bits. If the P6DIR is left as level 0 (GPIO input) and you enable the PREN bit, the POUT control whether an internal pullup resistor is pulled high or low. PSEL changes the functionality of the pin to the peripheral signal muxed to the pin as specified in the datasheet.

    Regards, ~Miguel

  • Thanks!  I had actually just discovered two things in my code when you posted.  The first was the reference voltage that needed to be set, very helpful thank you.  The second was that I was actually reading from A0 but my signal was coming into A5, which I needed to change with the code ADC12MCTL0 = ADC12INCH_5.  After I made those changes I was able to get quality data from my input.  Thank you very much for your response. 

    Now to get to my next challenge, making my UART and my ADC work simultaneously!  For some reason if I enable UART and the ADC at the same time, the UART will work but no conversions will take place.  I will most likely start a new post about this one if I cannot solve it soon.

**Attention** This is a public forum