Here is what I have come up with so far.....
it is getting closer.
Derived from Malanie's suggestion about parsing data and DT's averaging scheme * PIC18F2525 @ 8 MHz
Code:ByteData var byte CounterA var word ' sort counter CounterB var byte CounterC var byte DataA var word ' storage for sorted values DatSort var byte ' pointer to middle 8 values DatAvg var word ' averaged value of sorted data RawData var word[16] ' storage for raw data Clear AvgCount CON 8 ' = Number of samples to average FAspread CON 1000 ' = Fast Average threshold +/- INCLUDE "AllDigital.pbp" DEFINE OSC 8 OSCCON = %01110000 DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1 DEFINE HSER_CLROERR 1 ' Clear overflow automatically DEFINE HSER_SPBRG 34 ' 57600 Baud @ 8MHz, -0.79% SPBRGH = 0 BAUDCON.3 = 1 ' Enable 16 bit baudrate generator hserout["SortAverage",10,13] pause 500 goto Start 'Sort Array SUB ** collects 16 readings, sorts, and averages middle 8 ****** SortArray: CounterA=0 SortLoop: ' sorts 16 readings of RawData in order If RawData(CounterA+1) < RawData(CounterA) then DataA=RawData(CounterA) RawData(CounterA)=RawData(CounterA+1) RawData(CounterA+1+0)=DataA If CounterA > 0 then CounterA=CounterA-2 endif CounterA=CounterA+1 If CounterA < 15 then goto SortLoop ' for CounterC = 0 to 15 ' These three lines O/P the sort results ' hserout["Ordered[",dec CounterC,"]= ", dec RawData[CounterC],10,13] ' next CounterC '**** Trim and Average *************************************************** for DatSort = 4 to 11 ' pick out middle 8 readings IF ABS (DatAvg - RawData[DatSort]) > FAspread Then CatchUp IF ABS (DatAvg - RawData[DatSort]) < AvgCount Then RealClose DatAvg = DatAvg - (DatAvg/AvgCount) DatAvg = DatAvg + (RawData[DatSort]/AvgCount) GoTo AVGok CatchUp: DatAvg = DatAvg + (RawData[DatSort]/8) ' catch-up by 1/8 value of reading GoTo AVGok RealClose: DatAvg = DatAvg - (RawData[DatSort]/(AvgCount/4)) DatAvg = DatAvg + (DatAvg/(AvgCount/4)) AVGok: ' hserout["Middle[",dec datsort,"] = ",dec RawData[DatSort],10,13] 'SHOW mid 8 next DatSort Return '** END SUB************************ Start: FOR CounterB=0 to 15 HSERIN [RawData[CounterB]] NEXT CounterB FOR CounterB=0 to 15 HSEROUT["RawData[",DEC counterB,"] = ",DEC rawdata[CounterB],10,13] NEXT CounterB CALL SortArray HSEROUT ["Sorted Averaged result ",dec DatAvg,10,13] GOTO start




Bookmarks