## divide routine needed?

5 posts / 0 new
Author
Message

Hello:

I have a 10 bit number "N" which I wish to scale by a factor of 0 to 1. The 0 to 1 scale factor is an 8 bit number, call it H
Thus I'd like to calc:

Value = (N*H)/250 or N*(H/250) H/250 thus is a 0-1 scale factor

I''d like the final result to be accurate to 10 bits, but N*H is 18 bits, thus I'd need an 18 bit mul & divide routine. I don't want to scale down N & throw away its 10 bit resolution. In some cases N willl be small, say a value of 11. In others N=1000. I don't want to throw away the 8 bit resolution of H either.
So I have (10bits*8bits)/8bits=10 bits
Using the Atmel assembly 16 bit mul & divide routines. any ideas? This problem appears simple, but turns out to be perplexing if I want the code to be simple

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Try this:
Value = (N*H)/255. H is between 0 and 255 and in this way you'll need only to shift 8 times right (I think) the result. This is simple, eh? The only problem could be if H MUST be in 0...250 from some of your reasons. Then you have two choices: admitting a ((255-250)/255*100)%<2% error or using an dividing routine. Enjoy!

sorry, I need a final 10 bit accuracy...N*H is 18 bits & the divisor is 8 bit (approx 250)....perhaps I can use multiple 16/16 bit divide routines...a 24/8 bit routine would be great!! --anyone got one?

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

I can do every thing with a 16x16==>32 bit multiply
Do this:

1) mult numerator, get 32 bit result (only 18 bits used)
2) shift right twice, keep 16 bits (dividing by 4)
3) to divide by 250, mult the 16 bits by 418hex (1048 decimal)
4) Keep the upper word (16 bits) of the 32 bit result
That's it & plenty accurate!!!

following this, for example (321*248)/250 gives 318 as a result
(512*256)/250 gives 524
Here is a handyy hex calculator:

http://www.nrg.org/hexcalc/HexCalc.html

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!