energia / msp430-lg-core

15 stars 12 forks source link

MSP430 analogRead does not switch channels cleanly #22

Open robertinant opened 8 years ago

robertinant commented 8 years ago

From @BigVulcanDeal on April 7, 2013 22:49

The Analog Read function does not seem to allow for quick switching between input pins. Often, when switching to a new pin, the routine will return a sample from the previously sampled pin instead.

Below are two routines that might permit for a cleaner implementation of Analog Read. This logic has been used in a system that samples three analog signals, on a round-robin basis, in excess of 30k times/second (hence over 90k samples per second).

 // --- analogPinSelect is a routine that is used by 
 //     myAnalogRead(int pin) in order to select the designated A/D pin
void analogPinSelect(int pin){
  if (pin < 8){
    ADC10CTL0 &= !ENC;      // disable conversion
    ADC10CTL1 = pin << 12;  
    ADC10CTL0 |= ADC10ON + ENC + ADC10SHT_0; 
  }
}

// --- myAnalogRead(int pin) is a routine that reads a given A/D
//     port on an MSP430G2553.  The routine is blocking in nature
//     and it is not particularly efficient, but it avoids some of the 
//     problems of the build in Energia "analogRead(int pin)" routine
//    in particular, this routine can cleanly switch across several A/D input ports 
//    whereas Energia's analogRead() has problems when using more than one port.
int myAnalogRead( int pin){
  analogPinSelect(pin); // select the pin
  ADC10CTL0 |= ADC10SC; // start conversion
  while(ADC10CTL1 & ADC10BUSY);
  return ADC10MEM;
}

Copied from original issue: energia/Energia#223