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

Individual port bits
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Ttelmah
Guest







PostPosted: Sat Jun 11, 2005 4:09 am     Reply with quote

I should have thought of the macro bhaviour problem, in the past on anothr thread where a poster was having problems with bit masking. (Go and slap myself...).
This is a classic problem, and really means that CCS have been careless with their defines.
Basically, if you define a value like:

#define mask(n) 1<<n

and then use this in a statement like:

result= mask(2) & val;
or
result = mask(2) * 2;

you are 'reliant' on the C rules of precedence. The resulting statement, in the first case, will be:

result = 1<<2 & val;

In this case, the '&' statement has a lower precedence than the '<<', and the statement will work as expected. However in the second case, the result:

result = 1<<2 * val;

will multiply 2 by val, and then use this number for the shift. This will give totally different results for what may be expected....

This is why you will see some posters being very 'heavy handed' with brackets in #define statements, which allow you to force the evaluation to be in the 'expected' order. so changing the definition to:

#define mask(n) (1<<n)

ensures the statement evaluates as expected when used in a compound statement.

How such 'mis-expansion', affects a particular statement, depends on the code used, but i'd possibly suggest that the 'input_b' statement, is internally more complex than you might think (remember this has to test the flags for the port 'mode', and control tris if needed), and is possibly ending up applying the mask somewhere incorrectly, in this process...).

Fun!...

At least you have a solution that works now.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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