Displaying Fractional Inches (eg 3-11/16)
Lawrence Shafer
Posts: 12
I am building a measuring system for a band saw mill with a stamp counting pulses on a hall switch . Lets say the stamp counts to 1275, how would I display it as 12-3/4 inches? with the limited math it has available?
Thanks,
Lawrence
Thanks,
Lawrence
Comments
You represent fractions as integers (like 1275 for 12.75 inches). You can display this with or without the decimal point. If you really want to display fractional inches, you might want to store your data internally in terms of fractional inches and you have to decide what your basic unit is. For example, you might keep all your data in terms of 1/16th of an inch, so 3-11/16 comes out to 3 x 16 + 11 = 59. You can add and subtract in these units and multiply and divide by integer values (like 2 or 3). When it's time to display the value, you divide by the fraction divisor (16) to display the whole inches and take the remainder (modulus) to get the number of 1/16ths of an inch to display. If you want to get fancy and figure out the smallest denominator, you can just reduce the fraction by dividing numerator and denominator (initially 16) by 2 if the numerator is even (if the remainder of the division by 2 is 0) repeatedly until the numerator is odd (checking for zero initially).
There are ways to do the same with decimal fractions. If you keep your data in hundreths as you've shown, you can have a table in EEPROM that contains the upper limit for a decimal fraction part and the characters to display. For quarters, this could be:
You'd go through this table, checking the fractional part against the first byte of each entry. If it's greater or equal to the first byte, you'd display the string starting at the 2nd byte. If not, you'd skip over the string and it's zero delimiter and check the next entry. You'd need to check for zero before searching this table because that's a special case (with no fraction). Clearly, if you want more precision (like 3 decimal digits), you'd have to use a word for the numeric part of the entry rather than a byte value. If all of your measurements are positive, you could represent up to 65 inches with 3 decimal places.
For example (untested and probably horribly inefficent):
Harrison
Post Edited (Harrison.) : 3/16/2007 5:05:07 AM GMT
The above does not round off. It rounds down, so 99/100 becomes 15/16. Maybe the mechanism is set already with the offset. Otherwise it could be done in the math. The operation (t=NCD (n REV 4)) is a trick. Suppose x=1275, so n=12 (meaning 12/16). In binary the numerator is %1100, and the REV4 turns that into %0011. The NCD finds the highest bit that is a one, in this case t=2. So then it shifts both the numerator and denominator two bits right, so 12/16 becomes 3/4.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
Also another one I have been playing with is trying to get this to work with signed negative numbers. I thought I could get it to work by doing something like adding 4000 to x and subtracting 40 from p and using SDEC, but it did not work. Is there a way to divide signed negative numbers on a stamp at all?
Thanks a Million!
Lawrence Shafer
Hmmm. It seems to work okay for me. Enclose the whole routine in DO and LOOP so you can enter different values quickly in succession.
I've attached a modified program that steps through a sequence of values from 1.27 down to -1.28 (yes, negative) and displays the fractional equivalent. Here is the start of that sequence as displayed. 127 is the Stamp's internal integer representation of decimal 1.27.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com