??? 09/08/08 12:57 Modified: 09/08/08 13:09 Read: times |
#158061 - That makes things clearer. Responding to: ???'s previous message |
Chris Bertrand said:
The device limits are say 0x04A8 to 0x08A8
When the pot is at 0v = 0x00 then the device should be sending 0x04A8, when the pot is at 0xFF, it should be sending 0x08A8, and when the pot is half way 0x80, it should be 0x06A8 ((0x04A8 + 0x08A8)/2) It doesn't have to be perfect by any means, but a close tracking would be all I'm looking for. Thanks, that makes things a lot clearer. The basic problem seems to be that 16 bits aren't enough to hold the result of the product of a 16 bit number (the limit) and an 8 bit number. If you have the memory and the CPU cycles to spare, and want the most accurate result, do the whole calculation with a long. After shifting, you will end up back in the integer range. For example: unsigned long HC_Gain_temp HC_Gain_temp = HC_GAIN_HIGH - HC_GAIN_LOW; HC_Gain_temp = HC_Gain_temp * pot_level; // Divide by 256 with rounding HC_Gain_temp = (HC_Gain_temp + (1 << 7)) >> 8; HC_Gain_scaled = (unsigned int) HC_Gain_temp + HC_GAIN_LOW; If you don't care about getting the best accuracy and want to fit everything in the existing variables, use only the upper 8 bits of the limit. Beware (!!!), though, that you might run into trouble if the upper and lower limit are too close to each other. HC_Gain_scaled = ((((HC_GAIN_HIGH - HC_GAIN_LOW) >> 8) * pot_level)); //Average and divide by 256 HC_Gain_scaled += HC_GAIN_LOW; You might want to round before the right-shift to avoid at least some of the precision loss associated with division. Chris Bertrand said:
I can live with rounding as long as its close to those limits.....even the original function wasn't perfect in the sense that it never makes it to the highest limit......i.e. limits of 20-40 would yield 20-39 so I simply added 1 to the upper limit to get my 40....... That sounds like the rounding was left out at some point during the calculations. |
Topic | Author | Date |
Scale offset using ints by byte position location | 01/01/70 00:00 | |
Does type casting make sense here?... | 01/01/70 00:00 | |
Show us more......... | 01/01/70 00:00 | |
Here are defs and original function | 01/01/70 00:00 | |
ints, at leastin Keil, at 16 bits wide. | 01/01/70 00:00 | |
Christoph, | 01/01/70 00:00 | |
That makes things clearer. | 01/01/70 00:00 | |
A detail | 01/01/70 00:00 | |
Take a look at the ranges | 01/01/70 00:00 | |
Brett, that is helpful, I should add.... | 01/01/70 00:00 | |
No floating-point math required. | 01/01/70 00:00 | |
Its working but I have a question on theory | 01/01/70 00:00 | |
Rounding instead of truncate | 01/01/70 00:00 | |
oh, that is interesting | 01/01/70 00:00 | |
Russ' comment is right, though.![]() | 01/01/70 00:00 |