Code Snippets

Olympic Averaging

Douglas Butler March 29, 20132 comments Coded in C
int16 adcval,sum,max,min;
int8 i;
#define samples 10

min=0xFFFF;    //Set max and min to the limits of the range.

for (i=0;i<samples;i++){
    if (adcval>max) max=adcval;    //Update max and min on the fly.
    if (adcval<min) min=adcval;
    sum+=adcval;    //Generate sum of all samples.
sum-=max;    //Remove the maximum, and minimum values
sum-=min;    //as likely outliers.

return(sum/(samples-2)); //Average the remaining total.
     //If samples = 10 then the division becomes sum<<3