Hi folks,
Well, another learning moment! For the past week or so, I have been working on the previously mentioned sorting algorithm. I have been peeking in on the forum, but saw Al's name as the last one that replied so I didn't open it. Of course, I missed that there were actually 2 replies and that masked that there had been activity.
Thanks for the suggestion. I will study it and see how long it takes me to understand it.
In the mean time, this is where I have been churning. Puzzles me greatly, because it doesn't give anything near what I think it should.
Code:
'* : 18F1330 *
'****************************************************************
DEFINE OSC 32
OSCCON = %11111100 ' INTOSC primary, 8MHz
OSCTUNE = %11111111 ' ENABLE 4x PLL = 32mHz
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 138 ' 57600 Baud @ 32MHz, -0.08%
SPBRGH = 0
BAUDCON.3 = 1 ' Enable 16 bit baudrate generator
DataA var WORD
RawData var WORD [16] 'full program uses 16 and strips top
DatAvg var byte ' and bottom readings
DatSort var byte '
DatCnt var byte ' count the 16 data entries
AvgCount con 8 ' number of readings to average (1/x)
DatAvg = 2500 ' seed number to start averaging
RawData[4]= 20000 ' this data represents 8 different
RawData[5]= 21000 ' readings that come from a TMR0
RawData[6]= 22000 ' capture that is working. Fixed
RawData[7]= 23000 ' values here for testing
RawData[8]= 24000
RawData[9]= 25000
RawData[10]= 26000
RawData[11]= 27000
pause 2000
'-- Average middle 8 readings ---------------------------------------
Average:
for DatSort = 0 to 7 ' get 8 readings
Hserout["DA=",#DatAvg,", RD",#DatSort,"=",#RawData[DatSort]]
DatAvg = DatAvg - (DatAvg/AvgCount) 'subtract 1/x from Avg
hserout[", 1/x=",#(DatAvg/AvgCount)," DA- = ",#DatAvg]
DatAvg = DatAvg + (RawData[DatSort]/AvgCount) 'add 1/x of new reading
Hserout[", DA+ = ",#DatAvg,13,10]
next DatSort
hserout[13,10]
' hserout[", DA ",#DatAvg,13,10]
goto Average
Here is a RealTerm screen of the output
Code:
DA=219, RD4=20000, 1/x=24 DA- = 192, DA+ = 132
DA=132, RD5=21000, 1/x=14 DA- = 116, DA+ = 181
DA=181, RD6=22000, 1/x=19 DA- = 159, DA+ = 93
DA=93, RD7=23000, 1/x=10 DA- = 82, DA+ = 141
DA=141, RD8=24000, 1/x=15 DA- = 124, DA+ = 52
DA=52, RD9=25000, 1/x=5 DA- = 46, DA+ = 99
DA=99, RD10=26000, 1/x=10 DA- = 87, DA+ = 9
DA=9, RD11=27000, 1/x=1 DA- = 8, DA+ = 55
DA=55, RD4=20000, 1/x=6 DA- = 49, DA+ = 245
DA=245, RD5=21000, 1/x=26 DA- = 215, DA+ = 24
DA=24, RD6=22000, 1/x=2 DA- = 21, DA+ = 211
DA=211, RD7=23000, 1/x=23 DA- = 185, DA+ = 244
DA=244, RD8=24000, 1/x=26 DA- = 214, DA+ = 142
DA=142, RD9=25000, 1/x=15 DA- = 125, DA+ = 178
DA=178, RD10=26000, 1/x=19 DA- = 156, DA+ = 78
DA=78, RD11=27000, 1/x=8 DA- = 69, DA+ = 116
I can't yet find a pattern or a reason. Maybe its time for a bit of sleep. I will work on it more later.
Thanks
Bo
Bookmarks