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

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
11/04/08 08:40
Read: times


 
#159616 - Possible simplification
Responding to: ???'s previous message
If you multiply 20.48 by 100 it is suddenly an integer. And not just any integer, but an integer on the form 2^n.

Think about what happens if you take your 12-bit value and multiply by 100 and then divide this value by 2048. Anything surviving such a shift-right is an integer part ready to emit to a display.

The problem here is that your max value 2047 times 100 would be too large to fit in a 16-bit unsigned integer. But note that 100 can be divided by four, so you could instead multiply your ADC value by 25 (for a max of 2047*25=51175 and 25*20.48 will be 512 - still a 2^n value.

Now having a division by 2^n means that you can get the integer part with the above multiplication followied by a 9-bit shift right.

The 9 bits you would shift out can be extracted with a bit-and operation. The mist significant bit would represents 0.5, the next bit 0.25 etc. So if you need to convert to a decimal value (with fraction) for printing, you could use a 9-iteration loop on the fractional part where each iteration looks at one bit of the fraction and then conditionally add a fraction value. For each loop you shift the bit to check one step, and also shift the constant value you add in by one step.

List of 2 messages in thread
TopicAuthorDate
12 bit division using 8 bit microcontroller            01/01/70 00:00      
   Possible simplification            01/01/70 00:00      

Back to Subject List