I'm thinking you might have a winner there Bill.
But I have a question or two.
I've had another go at your code and am getting some promising results.
It's hard to follow with a calculator, so I made an interface to help test the routines.
Also, instead of reading/writing to EEPROM, I've modified your routine to use an array, so that I can pass it data from the PC.
The array is a circular buffer that overwrites the oldest value with the new sample, and on each iteration runs your code to average all 16 samples in the array.
I'm thinking that the circular buffer might be a problem.
Maybe if it only took 16 samples then averaged ???
The modified routine looks like this ...
Code:
X VAR BYTE ' First vector
Y VAR BYTE ' Second vector
Difference VAR BYTE ' Difference between vectors, always +
Average VAR WORD ' Average of two vectors
J VAR BYTE ' EEPROM memory address
BillsData VAR BYTE[32]
DataPos VAR BYTE
Bill_Avg:
DataPos = DataPos + 1
IF DataPos = 16 THEN DataPos = 0
BillsData(DataPos) = DIR
;DATA 250,230,240,34,200,10,10,23,23,21,125,0,34,255,123 ' 16 vectors
FOR J = 0 TO 28 STEP 2 ' Read pairs of vectors
; READ J, X
X = BillsData(J)
; READ J+1,Y
Y = BillsData(J+1)
if Y>X THEN SWAP X,Y ' X is always the larger variable
Difference = X-Y
Average = (Difference/2) + Y' Half difference and add to smaller vector
IF Difference>128 then
Average=Average+128 ' Reverse direction of vector
IF Average>255 THEN Average=Average-255 ' Whole revolution? Subtract 255 if necessary
ENDIF
; WRITE J/2 + 16,Average
BillsData(J/2 + 16) = Average
NEXT J
Bill_Value = Average
; LCDOUT $fe,$80,"Average: ",DEC3 Average
RETURN
The interface simply sends the Wind Vane readings to the PIC where it runs both of our averaging codes, and sends the results back for display.
Yours is on the right. Any thoughts on the sample buffer?
It works great unless there are large fast changes. (click image to play)
<object id='stUkhdQ01IR11XRFVbWlleU19R' width='711' height='444' type='application/x-shockwave-flash' data='http://www.screentoaster.com/swf/STPlayer.swf' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,115,0'><param name='movie' value='http://www.screentoaster.com/swf/STPlayer.swf'/><param name='allowFullScreen' value='true'/><param name='allowScriptAccess' value='always'/><param name='flashvars' value='video=stUkhdQ01IR11XRFVbWlleU19R'/></object>
Sorry Internet Explorer only.
Bookmarks