Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
;==========================================================
;Copy 16bit results to Temp regs
;40-45h = Time values
;d1,d2,d3,d4,d5,d6 = Temp Regs
;Median = d3,d4
;==========================================================
Cal_Median movf 40h,W
movwf d1
movf 41h,W
movwf d2
movf 42h,W
movwf d3
movf 43h,W
movwf d4
movf 44h,W
movwf d5
movf 45h,W
movwf d6
;
movlw .2
movwf Pass_Count
Next_1 call Comp_1_2
btfss STATUS,C
goto Next_2
Swap_1_2 movf d1,W
movwf Temp
movf d3,W
movwf d1
movf Temp,W
movwf d3
;
movf d2,W
movwf Temp
movf d4,W
movwf d2
movf Temp,W
movwf d4
Next_2 call Comp_2_3
btfss STATUS,C
goto Cal_Pass
Swap_2_3 movf d3,W
movwf Temp
movf d5,W
movwf d3
movf Temp,W
movwf d5
;
movf d4,W
movwf Temp
movf d6,W
movwf d4
movf Temp,W
movwf d6
Cal_Pass decfsz Pass_Count,F
goto Next_1
return ;d3,d4 = Median
;------------------------------------------
Comp_1_2 movf d3,W
subwf d1,W
btfss STATUS,Z
return
movf d4,W
subwf d2,W
return
Comp_2_3 movf d5,W
subwf d3,W
btfss STATUS,Z
return
movf d6,W
subwf d4,W
return
this:-
movf 0x20, w
xorwf 0x21, w
xorwf 0x20
xorwf 0x21
or this:-
movf 0x20, w
subwf 0x21, w
addwf 0x20
subwf 0x21
Cal_Median clrf Temp
movf 0x40, w
subwf 0x42, w
btfss STATUS,Z
goto comp_1_2_done
movf 0x41, w
subwf 0x43, w
comp_1_2_done btfss STATUS,C
bsf Temp, 0
movf 0x40, w
subwf 0x44, w
btfss STATUS,Z
goto comp_1_3_done
movf 0x41, w
subwf 0x45, w
comp_1_3_done btfss STATUS,C
bsf Temp, 1
movf 0x42, w
subwf 0x44, w
btfss STATUS,Z
goto comp_2_3_done
movf 0x43, w
subwf 0x45, w
comp_2_3_done movlw 0x3
btfss STATUS,C
xorwf Temp ;Temp is now 0 for 42/43
;1 for 40/41
;3 for 44/45
btfsc Temp, 1
goto use_44_45
btfsc Temp, 0
goto use_40_41
movf 0x42, w
movwf d3
movf 0x43, w
goto sort_done
use_44_45 movf 0x44, w
movwf d3
movf 0x45, w
goto sort_done
use_40_41 movf 0x40, w
movwf d3
movf 0x41, w
sort_done movwf d4
return
He asked for the median not the mean but I've no idea why.
Mike.
But the code he posted returns the median not the mean. I also thought it was a language thing but the code proves otherwise.From his user name it's a reasonable bet English isn't his first language, and the differences are pretty vague even if it is
The way that you swap two registers isn't the most efficient. You can save a couple of lines of code and a temporary register with either of these:-
Code:this:- movf 0x20, w xorwf 0x21, w xorwf 0x20 xorwf 0x21 or this:- movf 0x20, w subwf 0x21, w addwf 0x20 subwf 0x21
However, you don't need to actually swap the registers at all. You can just work out which is the median value and collect it at the end.
This code compares V1 with V2, then V1 with V3 and then V2 with V3. From the three comparisons, there are 8 (2^3) results, but it turns out that 2 of those aren't possible, leaving 6 possible combinations. I found that using the result of the comparison of V2 and V3 to invert the other results gave the same outcome, and only three possible combinations, and those are used to select the final answer.
This code is shorter, takes less than half the time, and only uses one register other than the input and result registers, instead of six.