This formula is missing a parethesis.
VELOCITY = (12 / ( RAW * ( 1 / 12000000 ) ) / 12
However, it will probably boil down to ....
VELOCITY = ( 12 * 12000000 ) / ( 12 * RAW )
.... which equals ......
VELOCITY = 12000000 / RAW
This is easy to calculate with DIV32 as long as the divisor is 15 bits or less. 12000000 is way less than 31 bits so that will not be a problem. If you can keep the divisor lower or equal to 15 bits you can use ......
Code:
R0.HIGHBYTE = $00 'stuff internal registers with 00B71B00hex=12000000dec
R0.LOWBYTE = $B7
R2.HIGHBYTE = $1B
R2.LOWBYTE = $00
Velocity = DIV32 Raw 'do the division, make sure Raw is <= 15 bits (32767)
This code can't go below 366 ft/s since Raw must not be larger than 32767(12000000/32768=366.21). However, there is a way to get around that. This code will scale the dividend and divisor as long as the divisor is larger than 15 bits.
Code:
Div32Value VAR BYTE[4]
Raw VAR BYTE[3]
DataIn VAR WORD
Velocity VAR WORD
TestBit VAR bit
'Blah blah blah
Div32Value[3]=$00 : Div32Value[2]=$B7 : Div32Value[1]=$1B : Div32Value[0]=$00
WHILE Raw[2] > 0
ASM
bcf STATUS,C ;Shift right(/2) until highbyte = 0
rrf _Div32Value+3,f
rrf _Div32Value+2,f
rrf _Div32Value+1,f
rrf _Div32Value+0,f
ENDASM
ASM
bcf STATUS,C ;Divide divisor by 2
rrf _Raw+2,f
rrf _Raw+1,f
rrf _Raw+0,f
ENDASM
WEND
IF Raw.0(15) THEN 'Do it again if necessary to get 15 bits
ASM
bcf STATUS,C
rrf _Div32Value+3,f
rrf _Div32Value+2,f
rrf _Div32Value+1,f
rrf _Div32Value+0,f
ENDASM
ASM
bcf STATUS,C
rrf _Raw+2,f
rrf _Raw+1,f
rrf _Raw+0,f
ENDASM
ENDIF
Datain.HIGHBYTE = Raw[1]
DataIn.LOWBYTE = Raw[0]
R0.HIGHBYTE = Div32Value[3] 'Put dividend into it's internal registers
R0.LOWBYTE = Div32Value[2]
R2.HIGHBYTE = Div32Value[1]
R2.LOWBYTE = Div32Value[0]
Velocity = DIV32 Datain ' Do the divison
Could be useful for those extremley slow bullets, or passing insects :-)
/Ingvar
Bookmarks