I think I've solved it for an array of 16 readings? The readings
1. A/D from a potentiometer
2. Range is 0 to 255
3. 0 is North, 64 East, 128 South,192 West
The rules:
1. Always work in pairs of vectors. Using EEPROM to store the vectors: 0 to 15 Stores the vectors. 8 pairs
16 to 23 Stores the next pairing. 4 pairs
24 to 27 Stores the next pairing. 2 pairs
28 to 29 Stores the last pair. 1 pair
30 Stores the result
2. If the difference between the vector pairs if <128 then average the vectors normally
3. If the difference between the vectors >128 then average normally, add 128, check for whole revolutions
I've run a fair number of tests and all seems to be OK?
Regards Bill Legge
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
DATA 250,230,240,34,200,10,10,23,23,21,125,0,34,255,123 ' 16 vectors
Main:
FOR J = 0 TO 28 STEP 2 ' Read pairs of vectors
READ J, X
READ J+1,Y
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
NEXT J
LCDOUT $fe,$80,"Average: ",DEC3 Average
END
Bookmarks