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

problem with the code (compiler problem???)

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



Joined: 20 Dec 2003
Posts: 14

View user's profile Send private message

problem with the code (compiler problem???)
PostPosted: Thu Dec 16, 2004 7:05 pm     Reply with quote

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

View user's profile Send private message

problem desc...
PostPosted: Thu Dec 16, 2004 7:08 pm     Reply with quote

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

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

PostPosted: Thu Dec 16, 2004 7:20 pm     Reply with quote

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

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

PostPosted: Thu Dec 16, 2004 9:38 pm     Reply with quote

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.
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