Kim wrote:
Hi David,
I'm trying to make a fault code which display onto the LCD.
I wish to make it like a circular buffer which has up to, say 10, faults recorded. Also I wish to display the most significant fault onto the LCD.
The way I visualise it, is that I give each fault a number, ie 0 to 255 (or to whatever the highest fault number is) and then sort the faults and display the highest priority first, until cleared, and then the next fault etc.
However, I'm having trouble doing a sort in splat, any ideas ?
Kim,
I just wrote a simple bubble sort (at the end of the post). I would not use an actual circular buffer structure - just build it as a linear FIFO buffer and shift data.
I've also attached a rather elaborate fault recording function I wrote some time ago for HVAC applications. It stores faults in time order (no sorting) but has a number of other interesting concepts.
I will incorporate this into future releases of SPLat/PC in the Examples folder.
David
I just wrote a simple bubble sort (at the end of the post). I would not use an actual circular buffer structure - just build it as a linear FIFO buffer and shift data.
I've also attached a rather elaborate fault recording function I wrote some time ago for HVAC applications. It stores faults in time order (no sorting) but has a number of other interesting concepts.
I will incorporate this into future releases of SPLat/PC in the Examples folder.
David
;bblsort1.spt Simple bubble sort
;------------------------------------------------------------
;Test code
bArray defByte 10
Test:
LoadI bArray ;Fill test array
iSetMem 0,25
iSetMem 1,17
iSetMem 2,250
iSetMem 3,2
iSetMem 4,0
iSetMem 5,5
iSetMem 6,67
iSetMem 7,76
iSetMem 8,17
iSetMem 9,14
LoadX bArray ;Prepare to sort
LoadX 10
GoSub BblSort
Here GoTo Here
;------ Bubble sort -------------------------------------------------
;Sort an array of n values into ascending order (lowest first)
;Call with X = n)umber of values, Y=start of array
;Clobbers stack and I
;Method: Scan the array, comparing pairs of values. If two values are found in the
;wrong order, swap them and continue the scan. Repeat until you get a scan where
;no swaps take place.
;Note: This function could consume many mS of processor time depending on the array size.
sBBL_Swap defSEM ;Notes a swap took place
bBBL_N: defBYTE ;Array size
bBBL_Addr: defBYTE ;Array address
bBBL_Count: defBYTE ;Comparison counter
BblSort:
Store bBBL_N
Store bBBL_Addr
BBL_OuterLoop:
ClrS sBBL_Swap
Recall bBBL_Addr
XtoI
Recall bBBL_N
Store bBBL_Count
BBL_InnerLoop: ;Back here to compare 1 pair of entries
;;; ClrInstCount ;For D>=18, if inside a MultiTrack task
iRecall 0 ;Get two consecutive elements
iRecall 1
CompareR
BranchR
Target BBL_Equal ;No swap
Target BBL_XGTY ;No swap
Target BBL_XLTY ;Swap
BBL_XLTY:
iStore 0 ;Save in swapped order
iStore 1
SetS sBBL_Swap
;Fall thru
BBL_Equal:
BBL_XGTY:
IncI ;Array pointer
DMGNZ bBBL_Count,BBL_InnerLoop ;End of array?
;Done one scan. Test if any swaps were done
GoIfST sBBL_Swap,BBL_OuterLoop
Return
