#include "../main/SystemInclude.h" //============================================================================== //u16 L; u16 SAMPLES[ANX][FILTER_ANX]; //u16 tempV, *filterPointer, tempMax=0, tempMin=0xffff; //****************************************************************************** u16 ReadDataFromMovingAverage(u16 channel) { u16 L; u32 voltageSum; // calculate voltageSum voltageSum = 0; //for (L = 1; L <= FILTER_SIZE; L++) voltageSum += (u32)SAMPLES[channel][L]; //voltageSum /= (u32)FILTER_SIZE; for (L = 0; L < FILTER_ANX; L++) voltageSum += (u32)SAMPLES[channel][L]; voltageSum /= (u32)FILTER_ANX; return (u16)voltageSum; } //****************************************************************************** u16 MovingAverage(u16 voltageBuffer, u16 updateScale, u16 channel) { u16 L, tempV, *filterPointer, tempMax=0, tempMin=0xffff; u16 voltageFilter; u32 voltageSum; voltageFilter = SAMPLES[channel][FILTER_END]; voltageSum = (u32)voltageFilter; // voltageSum *= (u32)updateScale; // updateScale = (u16)(voltageSum >> 9); // if(voltageBuffer > voltageFilter) tempV = voltageBuffer - voltageFilter; // else tempV = voltageFilter - voltageBuffer; // if(tempV > updateScale) { filterPointer = &SAMPLES[channel][0]; for (L = 0; L <= FILTER_END; L++) *filterPointer++ = voltageBuffer; return voltageBuffer ; } filterPointer = &SAMPLES[channel][1]; voltageSum = 0; for (L = 1; L <= FILTER_END; L++) { voltageFilter = *filterPointer; filterPointer--; *filterPointer = voltageFilter; filterPointer += 2; voltageSum += (u32)voltageFilter; if(voltageFilter > tempMax) tempMax = voltageFilter; if(voltageFilter < tempMin) tempMin = voltageFilter; } SAMPLES[channel][FILTER_END] = voltageBuffer; voltageSum += (u32)voltageBuffer; if(voltageBuffer > tempMax) tempMax = voltageBuffer; if(voltageBuffer < tempMin) tempMin = voltageBuffer; voltageSum -= (u32)tempMax; voltageSum -= (u32)tempMin; voltageSum >>= FILTER_SHIFT; return (u16)voltageSum ; }