View previous topic :: View next topic |
Author |
Message |
pop
Joined: 18 Sep 2004 Posts: 22
|
signed int 16 or signed int 32 |
Posted: Sun Mar 20, 2005 11:19 am |
|
|
Hi,
What is the range of signed int 16? I am familiar with unsigned integer ranges, but I am not sure how signed numbers are handled in a PIC.
After doing some temperature based calculations I can have positive or negative temperatures values scaled by 1000 (to avoid floating point math). Since these values are likely to be in 16bit range (due to scaling), can they fit into signed int16 variables?
In other words is signed int16 range between -65535 and 65536? Or do I have to use signed int32 or worst yet a float?
I hope that somebody can shed some light on this small but confusing problem.
Thanks |
|
|
newguy
Joined: 24 Jun 2004 Posts: 1908
|
|
Posted: Sun Mar 20, 2005 11:25 am |
|
|
Signed integers are handled the same no matter the processor. If the most significant bit is a 1, then the number is negative.
unsigned int8: 0 to 255
signed int8: -128 (0x80) to +127 (0x7f)
unsigned int16: 0 to 65535
signed int16: -32768 (0x8000) to +32767 (0x7fff)
Signed or unsigned, they're stored the same way. |
|
|
pop
Joined: 18 Sep 2004 Posts: 22
|
|
Posted: Sun Mar 20, 2005 5:54 pm |
|
|
Thanks newguy
So if I have something like the following can I store it into an signed int 16
signed int 16 temperature;
temperature=203593-222*(adcValue);
Due to linearization (hardware: Resistor in series with NTC thermistor),
my adcValue is always such that temperature range is;
848<adcValue<963
-10000<temperature<15000 (fits in a 16bit datatype)
Now considering that 203593 is well outside int16 range will the PIC give me a correct result?
Thanks |
|
|
newguy
Joined: 24 Jun 2004 Posts: 1908
|
|
Posted: Sun Mar 20, 2005 5:59 pm |
|
|
Good question. When I saw the large constant in your calculation (200,000+), the first thing I thought was "that won't work."
I really don't know if it will work or not. You can try it and see what happens. If it doesn't work, just declare temperature as a signed int32. That will work for sure, at the expense of being a little slower to run. |
|
|
rwyoung
Joined: 12 Nov 2003 Posts: 563 Location: Lawrence, KS USA
|
|
Posted: Sun Mar 20, 2005 6:43 pm |
|
|
pop wrote: | Thanks newguy
So if I have something like the following can I store it into an signed int 16
signed int 16 temperature;
temperature=203593-222*(adcValue);
Due to linearization (hardware: Resistor in series with NTC thermistor),
my adcValue is always such that temperature range is;
848<adcValue<963
-10000<temperature<15000 (fits in a 16bit datatype)
Now considering that 203593 is well outside int16 range will the PIC give me a correct result?
Thanks |
You realize your ADC range is only about 11% of the working range of the converter. Perhaps you should go back to your analog circuit and rework it to expand the working range for a better fit.
Barring that, you could subtract 848 from each ADC value resulting in values from 0 to 115 and now propagate that change through your formula to see if your constant can be reduced allowing you to use 16 bit math.
Or just be lazy and go with 32 bit math. _________________ Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month! |
|
|
bluetooth
Joined: 08 Jan 2005 Posts: 74
|
|
Posted: Sun Mar 20, 2005 6:48 pm |
|
|
You will need to use signed int32's for the intermediate calculation. You will NOT get the right answer if you don't.
It's possible you could re-evaluate your equation since you are using such a small range from the a/d - only 115 counts. Maybe you could scale it all down if you take that bias out of the a/d reading, making it fit (possibly) into signed int16's.
Unless processing power or memory is lacking, I'd use the signed int32's and move on.
Good luck!
----------------------
Adding:
It looks like it could fit into signed int16's with the following approximate equation derived from your numbers, assuming the 848 bias is removed from the a/d reading:
temp = -217 * (adc - 848) + 15000
You'll have to apply your real numbers to find the actual slope and intercept... also, you'll inject a little error due to integer math (e.g., the slope above is really 217.39 based on X1 = 115, Y1 = -10000, X2 = 0, Y2 = 15000).
With the slope of -217, as long as your adc-848 stays less than about 150, this will fit into signed int16's.
As another thought, since you only have 115 readings, you might consider a lookup table. Precise and fast, again if memory is not at a premium.
Happy "mathing".... |
|
|
pop
Joined: 18 Sep 2004 Posts: 22
|
|
Posted: Mon Mar 21, 2005 5:23 pm |
|
|
Thanks for the help!
I will probably go with the lazy way and use signed int32 as that could also be less costly (e.g. not much memory left on my pic:D) than using a lookup table.
I did consider re-designing my thermistor-linearization circuit but since I am operating in -10°C to 15°C, I found that my current linearization design gives me the smallest error (worst case +/-2%).
Once again thanks for all the responses. |
|
|
|