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.

Problem with ADC12 single conversion - repeated



I've written a function to perform a single conversion using the ADC12. I pass the channel that I want to convert into the function (for example, analog input A3). The function works the first time I call it, but I get inconsistent and wrong results on subsequent calls to the function. I've read through the users guide and do not see anything wrong with my code. Can someone please take a look at the code below? Thanks.

int ADCread(int Channel)

{                                

  ADC12CTL0 |= ADC12ON;                               // ADC12 core on

  ADC12CTL0 &= ~ADC12ENC;                         // Disable conversion

  ADC12CTL1 |= ADC12SHP;                             // Use sample timer

  ADC12CTL0 |= ADC12SHT02 + ADC12SHT01 + ADC12SHT00;           // sample time = 192 clocks

  ADC12MCTL0 |= ADC12SREF2;                       // select external reference

  ADC12MCTL0 |= Channel;                                 // select input channel

  ADC12CTL0 |= ADC12ENC + ADC12SC;          // enable and start conversion

  while (ADC12CTL1 & ADC12BUSY);                 // Wait for conversion to complete

  ADC12CTL0 &= ~ADC12ON;                             // ADC12 core off

  return ADC12MEM0;                                          // Return conversion result

}

 

  • Well, solved this one myself. Problem is this line of code ...

          ADC12MCTL0 |= Channel;                                 // select input channel

    It works fine the first time the function is called because the channel selection bits bits[3:0] are initialized low. But on subsequent calls to the function the line of code above only sets the bits high that it references, it doesn't clear any that need clearing. For example, if I passed Channel = 5 = 0101 the first time it works fine and selects analog input A5. If I call the function a second time and pass Channel = 2 = 0011, the line above only changes the last two bits and the result is 0111. Thus I select analog input A7 instead of the intended A2.

    Don't know why I didn't see this initially!

     

**Attention** This is a public forum