CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

signed int 16 or signed int 32

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
pop



Joined: 18 Sep 2004
Posts: 22

View user's profile Send private message

signed int 16 or signed int 32
PostPosted: Sun Mar 20, 2005 11:19 am     Reply with quote

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: 1907

View user's profile Send private message

PostPosted: Sun Mar 20, 2005 11:25 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Mar 20, 2005 5:54 pm     Reply with quote

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: 1907

View user's profile Send private message

PostPosted: Sun Mar 20, 2005 5:59 pm     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Sun Mar 20, 2005 6:43 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Mar 20, 2005 6:48 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Mar 21, 2005 5:23 pm     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group