View previous topic :: View next topic |
Author |
Message |
tmach
Joined: 20 Dec 2003 Posts: 14
|
problem with the code (compiler problem???) |
Posted: Thu Dec 16, 2004 7:05 pm |
|
|
Hello All,
I am using PCM C compiler (Ver 3.178) and PIC16F877 MCU. Attached is the portion of the code I have problem with.
*******************************************
value = input_c(); ---------(1)
temp = ((value * 10) + 900)/10; ---------(2)
B = (int) (temp / 8); ---------(3)
A = temp - (B * 8); ---------(4)
********************************************
In my program value varies from 0 to 50. If value <=25, I read correct values of A and B. If value > 25, I see that A and B are calculated taking value = 0 and incrementing. I mean, if value = 26, i get A = 2, B = 11. This is the same value I get if value = 0, if value = 27, i get A = 3, B = 11. This is the same value I get if value = 1.
I see that this problem might be because, if value>25 then (value*10) in statement (2) is greater than 255 and the compiler might be wrapping it to 0, which it shouldn't do.
Please advise on this.
Regards.
tmach |
|
|
tmach
Joined: 20 Dec 2003 Posts: 14
|
problem desc... |
Posted: Thu Dec 16, 2004 7:08 pm |
|
|
I just forgot to mention the data types I am using.
Value, A is int8
temp and B are int 16
thanks
tmach |
|
|
Darren Rook
Joined: 06 Sep 2003 Posts: 287 Location: Milwaukee, WI
|
|
Posted: Thu Dec 16, 2004 7:20 pm |
|
|
You need to typecast. If you mix 8 and 16 bit math in one operation it will use 8bit routines to save ROM, so you need to specifically tell it 16bit. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Dec 16, 2004 9:38 pm |
|
|
Quote: | temp = ((value * 10) + 900)/10; ---------(2)
|
You do realize that this is actually the same as
temp = value + 90
If value is never greater than 50 then temp can never be more than 255 so
Quote: | B = (int) (temp / 8); ---------(3)
A = temp - (B * 8); ---------(4)
|
All that does nothing but waste code and processor time. |
|
|
|