Decimals and Digits and Division Oh My!
I am working on this circuit to measure distances in inches. I want to get inches to the tenths place and extract that value into variables to display but my math must be wrong. I am using the SRF Ultrasonic Range Finder and basing my information off of http://www.rentron.com/Micro-Bot/Sonar.htm "If we're using 4MHz, then 90 / 15 = 6 inches. Viola. Using 4MHz simply divide the resulting pulse width returned by PULSIN by 15 instead of 74 as in the 20MHz example." 90 is an example of the variable raw shown below.
Raw VAR WORD 'raw measurement
Distance VAR WORD 'converted value
Tens Var word
Ones VAR WORD
Tenths VAR WORD
if (raw >= 150) then
Distance = (Raw / 15)
Tens = distance dig 0
ones = distance dig 1
tenths = distance dig 2
hserout ["Distance = ", Dec Tens, DEC1 ones, ".", dec2 tenths, " inches", 10, 13]
else
Distance = (Raw / 15)
ones = distance dig 0
tenths = distance dig 1
HSEROUT ["Distance = ", DEC ones, ".", dec1 tenths, " inches", 10, 13]
endif
The reason I have the if statement if raw is greater than or equal to 150 is because thats when if you divide by 15 the sensors result is 10 inches and the tens place is needed. It kinda works but there is horrible percision for the tenths place I am a little bit confused on the 32 bit math and using */ and ** and DIV32 and scaling everything up and then down but i think i got the extraction part of it correct. In the PBP document is says the DIG number 0 is the right most digit, well in my case if my math is right not counting the percision its the left most digit for me. I basically want raw / 15 to be a 3 digit integer if the sensor is less than less 10 inches and a 4 digit integer if its greater than 10 inches and stick the decimal in there accordingly. I don't understand even if you do the division how you would truncate it to the 3 and 4 digits which matter if DIG 0 is the right most digit and doesn't if DIG 0 is the left most. I don't have to worry about 100+inches because its not needed for my application. Any help on the math is appreciated.
- Matthew