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.

Averaging the ADC12 samples

Hi, I'm acquiring the analog signals uisng CCS430F5137 and I want to smooth them by averaging the ADC samples.

I found this similar threads

Averaging with 4 samples : http://e2e.ti.com/support/microcontrollers/msp430/f/166/t/19335.aspx

Averaging with 8 samples :http://e2e.ti.com/support/microcontrollers/msp430/f/166/t/148277.aspx

The implementation of 8 samples similar above is used with ADC12 instead of ADC10  but I am not getting anything on the hyperterminal.Is there anything I need to change to use ADC10 averaging code to work with ADC12

main
{
ADC channel settings()
while(1)
{
           VoltCalibrated = results[oldest];
	  for (i=0; i < 8; i++)
	  VoltMeasured[i] = VoltCalibrated;
	  VoltAverage = VoltCalibrated;
	  VoltMeasured[VoltMeasuredPosition++] =results[oldest];
	  if (VoltMeasuredPosition == 8)
	  VoltMeasuredPosition = 0;
	  VoltAverage = 0;
	  for (i = 0; i < 8; i++)
	  VoltAverage += VoltMeasured[i];
	  VoltAverage >>= 3; // Divide by 8 to get average
           UART_send(VoltAverage); 
	  ADC12CTL0 |= ADC12SC;  
    __bis_SR_register(CPUOFF + GIE);
  }
}
#pragma vector=ADC12_VECTOR 
__interrupt void ADC12ISR(void) 
{
  if (oldest >= Size) 
  {
        oldest =0; 
      }
    else
    {
  oldest = oldest +1; 
  }
  results[oldest] = ADC12MEM0;
  __bic_SR_register_on_exit(CPUOFF);
}

 I just have a doubt of how this works ( or what this will do) VoltAverage >>= 3; // Divide by 8 to get average

Can anyone suggest the changes for this.

Thanks.

  • Your ADC ISR put data into results[] array while you are calculating average for VoltMeasured[]. Please tripple-check your code, debug it before posting

  • stefan 12 said:

     I just have a doubt of how this works ( or what this will do) VoltAverage >>= 3; // Divide by 8 to get average

    Can anyone suggest the changes for this.

    Voltaverage = Voltaverage / 8;

    // this will give same result, divide it by 8

  • Ilmars said:
    Voltaverage = Voltaverage / 8;
    // this will give same result, divide it by 8

    It will give the same result value, but it will take significantly longer to do a division than a shift operation. So the overall resulting behavior (besides the plain numerical result) is different.
    Unfortunately, the compiler doesn't detect by itself that a division by 8 is the same as a shift by 3. (same for other divisions or multiplications with constants of a power of 2)

  • Jens-Michael Gross said:
    Unfortunately, the compiler doesn't detect by itself that a division by 8 is the same as a shift by 3

    Unfortunately you are using wrong compiler then ;) For example IAR C compiler code disassembly:

    i = i / 8;
    00FC22 C312 clrc
    00FC24 1021 rrc.w @SP
    00FC26 1121 rra.w @SP
    00FC28 1121 rra.w @SP

  • Ilmars said:
    Unfortunately you are using wrong compiler then ;)

    Or it has changed since I last checked this specific detail.
    I never used IAR anyway.

**Attention** This is a public forum