Hello!
Sorry for the delay!
Hello Dora, thanks for your reply. In my case the idea is to encode relative humidity percent as an unsigned
integer in the range from 0 to 10000 with 0 meaning 0% and 10000 meaning 100.00%. I read your post but
I don´t know how to implement this, i.e. how to encode a 1/100 using an integer?.
Basically andre_teprom explained it...
Here is what I was trying to say, with a little bit more detail.
Here is how to perform a division with a multiplication.
1. I was telling you to decide upon your own fixed point scheme. Some processors have a hardware floating
point multiplication (which is in fact an integer multiplication).
I suppose we can agree that multiplying 2 numbers of 16 bits will give you one 32-bit value.
Now if you consider a 32-bit value, let's call it ACC, then you can split it into 2 16-bit values, let's call them ACC_L and ACC_H.
Now you can decide for example that ACC_L full scale (0x00010000) means 1. If you multiply any 16-bit integer with
this value, you will obtain the same integer, but shifted by 16 bits on the left (i.e. multiplied by 65536).
Suppose now that you use 0x00008000, which is half of the above number, then if you multiply any number by this one, you
will get half of your number in the upper ACC.
You can continue with 1/4 (0x00004000), 1/8 (0x00002000), etc, in which case you will have numbers that, multiplied
with any 16-bit value, give you 1/2, 1/4, 1/8 ... of the former value in the upper ACC.
Since the multiplication distributes over the addition, you can also verify that a 16-bit value multiplied by (0x00008000 + 0x00004000)
would give you 3/4 of that value in the upper ACC.
2. Now how to divide by 100? Multiply by 1/100.
If 1 corresponds to 0x00010000 or 2^16 or 65536, then 1/100 corresponds to 655.36 that you have to cut down to 655.
So if you want to divide any 16-bit value by 100, you multiply it by 655 and you get the result in the upper ACC.
3. Let's try the trick!
Example: you want to write 3209 as 32.09.
Multiply 3209 by 655. You get 2101895, which can be written 0x00201287 in hexa.
So your integer part will be 0x0020 (which is 32. Good news, that's what we are looking for).
And then for the 09, you just calculate 3209 - 32 * 100, and you will get 9. At this point, you should be careful that the 0 disappears
and you have to add it yourself if the decimal part is less than 10.
I just tried a float division (with a MSP430) and it takes 400 cycles. And the calculation abouve should be feasible in very few
cycles, maybe 10 or 20.
That's about it.
Dora.