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 support@ccsinfo.com

char conversion

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







char conversion
PostPosted: Sat Jul 14, 2007 1:18 pm     Reply with quote

Running into a bit of a snag that I believe I know why it is happening, but not sure how to fix it

I am reading data (temperature) from an I2C device (8bits of data), and the device returns it as celcius. I want to then convert it to fahrenheit using the following equation

int temp;
temp = I2C_data_read(); // function that returns the I2C data

temp = (((temp * 9) / 5) +32;

but when (temp *9) is higher than 255 my temp does not get displayed correctly on my LCD. I seems that even though I specify temp as an int, it still seems to be treated as a char and anything above 255 wraps around.

What am I doing wrong?
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Sat Jul 14, 2007 1:40 pm     Reply with quote

Quote:
What am I doing wrong?
You did not read the manual...

Code:
CCS Manual, Jan-2007, page 23
Basic Types

Type-Specifier
int1   Defines a 1 bit number   
int8   Defines an 8 bit number
int16  Defines a 16 bit number
int32  Defines a 32 bit number
char   Defines a 8 bit character
float  Defines a 32 bit floating point number
short  By default the same as int1
int    By default the same as int8         <<-------------
long   By default the same as int16
void   Indicates no specific type


In the C language the int datatype is defined to have the default size for the processor it is compiled for. In a Windows PC this is 32 bits, for the PIC it is 8 bits.

Because you can never assume the int datatype to have a certain size it is good programming practice to use other datatypes with a garuanteed size: int8, int16, etc.
croc45
Guest







thanks ckielstra
PostPosted: Sat Jul 14, 2007 1:45 pm     Reply with quote

I thought it was something stupid that I was doing, those tend to be the hardest things to see ;-).

I'm new to the pics so I'm making assumptions that I should not, and your advise of reading the manual is well taken
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Sat Jul 14, 2007 2:41 pm     Reply with quote

Quote:
I thought it was something stupid that I was doing, those tend to be the hardest things to see ;-).
I totally agree. That said, there are no 'stupid' questions, better to ask soon before spending days searching. The problem is always to ask the 'right' question...
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