View previous topic :: View next topic |
Author |
Message |
croc45 Guest
|
char conversion |
Posted: Sat Jul 14, 2007 1:18 pm |
|
|
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
|
|
Posted: Sat Jul 14, 2007 1:40 pm |
|
|
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 |
Posted: Sat Jul 14, 2007 1:45 pm |
|
|
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
|
|
Posted: Sat Jul 14, 2007 2:41 pm |
|
|
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... |
|
|
|