
#define NUM_SAMPLES 6        // 6 == 4+2  (and 10 == 8+2)

__sfr __at(0xab) AD1DAT2;

// combination of glitch removal and averaging
int adc_smoothed()
{
  unsigned int value = 0;
  unsigned char index;
  unsigned char max = 0x00;
  unsigned char min = 0xff;
  unsigned char tmp;

  for (index = 0; index<NUM_SAMPLES; index++)
  {
    tmp = AD1DAT2;
    value += tmp;

    if( tmp > max )
      max = tmp;
    if( tmp < min )
      min = tmp;
  }

  // removing highest and lowest ADC reading from sum
  value -= min;
  value -= max;

  // return average of the rest
  return value/(NUM_SAMPLES-2);
}