ok, so scalerobotic's link to Melanie's 'sorting numbers' article, is exactly what I need, so I'm trying to get the thing to work.
i've basically got a small array that a prepopulate with some random numbers.
Code:
@ __CONFIG _FCMEN_OFF & _HS_OSC & _WDT_OFF & _MCLRE_OFF & _CP_OFF & _IESO_OFF & _BOR_OFF & _PWRTE_OFF
DEFINE OSC 20 ' set Oscillator at 20Mhz.
DEFINE NO_CLRWDT 1 ' PBP doesn't clear WDT automatically
DEFINE HSER_CLROERR 1
DEFINE HSER_TXSTA 24h ' Enable transmit
DEFINE HSER_SPBRG 129 ' set USART to 9600 baud @20Mhz
CounterA var Byte
DataA var Byte
RawData var Byte [6]
Averaged var byte
hserout [27,91,72] 'home cursor
hserout [27,91,50,74] 'clear screen
RawData[0] = 25
RawData[1] = 27
RawData[2] = 32
RawData[3] = 30
RawData[4] = 34
RawData[5] = 33
hserout ["before....", 13, 10]
hserout [dec rawdata[0],13,10]
hserout [dec rawdata[1],13,10]
hserout [dec rawdata[2],13,10]
hserout [dec rawdata[3],13,10]
hserout [dec rawdata[4],13,10]
hserout [dec rawdata[5],13,10]
hserout [13,10]
hserout [13,10]
CounterA =0
gosub SortArray
gosub Average_data
hserout ["after....", 13, 10]
hserout [dec rawdata[0],13,10]
hserout [dec rawdata[1],13,10]
hserout [dec rawdata[2],13,10]
hserout [dec rawdata[3],13,10]
hserout [dec rawdata[4],13,10]
hserout [dec rawdata[5],13,10]
hserout ["averaged = ", dec averaged,13,10]
goto end1
Average_data:
hserout [dec rawData[2]," + ", dec rawData[3], 13,10]
Averaged = (rawData[2]+rawData[3])/2 ' average the middle two of 6 ongoing samples
return
SortArray:
SortLoop:
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 < 5 then goto SortLoop
Return
end1:
end
here's my screen output (I average the middle two numbers Rawdata[2] & RawData[3] )....
Code:
before....
25
27
32
30
34
33
after....
6
25
27
30
32
33
averaged = 31
It all works (ie sorts & averages correctly) - but where on earth has the number 6 in the sorted numbers come from? (there was no number six in the original array - also, whatever number I place in that array position RawData[4] comes out sorted as 6!?!!)
Edit: Ok, think I've stumbled on the issue - it seems to be related to my array declaration ....because when I use RawData var Byte [8] ...everything works.
So why can't I declare an array 5 bytes 'deep'? ie RawData var byte [5] ???
Bookmarks