Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
10/19/02 16:50
Read: times


 
#31099 - RE: Division to 5 places
Brian:
Here is what you need to do. Arrange to have your input number in binary. All the best if it is a binary number with the LSB at the least resolution of the sensor or the measuring device you are using. Lets decide, for purpose of this discussion, that your input is in binary weighted units of .001 mm 1 x 10^^-3 mm) per LSB. Next decide how many decimal places you want in the resultant base 10 conversion such as four decimal places. Finally write this....
   SensorE-3mm * 1E4 decplaces
   -----------------------
        254E-1 mm/in 

This simplifies to...
   Sensor * 1E2
   ------------
       254

So you should be able to take your sensor reading and multiply it by 100 and divide by 254. The result when converted to base 10 display format should be in 4 digits after an implied decimal point between the fourth from right and fifth from right digits. You can take care of that formatting detail in your display code.

To do the multiply and divide look in the www.8052.com Code Library for the "Math Routines" download. In that source file find the routines umul16 and udiv32. Use the multiply to take your 16 bit sensor reading and multiply it by a 16-bit constant of 100. This produces a 32 bit result which you now use as the dividend input to the 32-bit by 16 bit divide routine. The 16-bit divisor would be the constant factor of 254. This will produce a 32 bit quotient and a 32 bit remainder. If you want to round off look if the remainder is above or below 127 (254/2) and if above then add 1 to the quotent.

You need to convert the 32 bit quotent to decimal digits by first dividing it by 100,000, and then its remainder by 10,000 and so on down to a final divide by 10 to get the last two digits. You cnn use the
same UDIV32 routine to do these divides if you wish.

Now lastly if you need to increase resolution in the low digits then just increase the numerator multiplier from 100 to 1000 and throw away the low decimal digit after the conversion to decimal.

In addition, if the sensor you have is not in even binary units of .001 mm / bit then you can include a scale factor to get it to that resolution by changing the 100 and 254 values appropriately.

I hope this helps.
Mike Karas




List of 11 messages in thread
TopicAuthorDate
Division to 5 places            01/01/70 00:00      
RE: Division to 5 places            01/01/70 00:00      
RE: Division to 5 places            01/01/70 00:00      
RE: Division to 5 places            01/01/70 00:00      
RE: Division to 5 places            01/01/70 00:00      
RE: Division to 5 places            01/01/70 00:00      
RE: Division to 5 places            01/01/70 00:00      
RE: Division to 5 places            01/01/70 00:00      
RE: Division to 5 places            01/01/70 00:00      
RE: Division to 5 places            01/01/70 00:00      
RE: Division to 5 places            01/01/70 00:00      

Back to Subject List