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

Math in #define

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



Joined: 30 Aug 2005
Posts: 155
Location: Calgary, AB

View user's profile Send private message

Math in #define
PostPosted: Sat Dec 13, 2014 12:41 pm     Reply with quote

Can you do math in a #define?


Code:

#define CLK_SPEED 80 000 000
#define SP3_SW_BAUD 19200

FOSC_DIV 2

#if CLK_SPEED == 80 000 000
#define _INT_OH 25
#endif

#define SP3_BIT_DELAY_RX ((1 / SP3_SW_BAUD) / (CLK_SPEED / FOSC_DIV)) - _INT_OH
...
setup_timer4(TMR_INTERNAL | TMR_DIV_BY_1, SP3_BIT_DELAY_RX);   



So the second last line is where it calculates the hardcoded delay value, is there a way to do this?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Dec 13, 2014 1:30 pm     Reply with quote

Example of calculating i2c baudrate register value:
http://www.ccsinfo.com/forum/viewtopic.php?t=50247&start=6
Eugeneo



Joined: 30 Aug 2005
Posts: 155
Location: Calgary, AB

View user's profile Send private message

PostPosted: Sat Dec 13, 2014 3:19 pm     Reply with quote

Great! that is a good example. Thanks
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Mon Dec 15, 2014 2:53 am     Reply with quote

#define doesn't "do" maths, its does text substitution. Its just a way of "defining" a more convenient, shorthand version of some longer or more awkward text. That means that as long as the result of substituting the text is valid C then it should be OK.

You do have to be careful about how such text substitutions work, because its easy for something to not do what you expect. For example consider:

Code:

#define AB a + b

...

d = c * AB;

// Substituting the text for AB gives:

d = c * a + b;

// ...and not

d = c * (a + b);

// ...as might be expected. Its is generally a good idea to put any defined arithmetic expression in brackets to ensure its treated as expected:

#define AB (a + b)


Spaces in numeric literals, e.g. 80 000 000, will cause trouble as it will be interpreted as three separate values, 80, a zero and then another zero. Also other separators are bad too, such as commas as in: 80,000,000. C requires such literals to have no separators. So you need 80000000, not 80 000 000 or 80,000,000.

But yes, you can do maths in a define, in a sense, if you are careful - I do it all the time, particularly for things such as ADC scaling. But, whether its in a define or not has nothing to do with it: its whether the resulting text gives sensible C.

Many Cs used to have the pre-processor, that did macro expansion (which dealt with #define), as a separate process, and it was possible to look at the result as a listing. That enabled programmers to check that things like defines produced the expected result. In CCS C, and probably many other Cs today, there is no actual separate pre-processor as such, its just a function of the compiler, and a pre-processor output listing is not produced.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Dec 15, 2014 11:56 am     Reply with quote

Quote:
and a pre-processor output listing is not produced.

That's true, but you can load the pre-processor symbol into a temp
variable, and then go look at the .LST file to see the value that the
compiler produced. Or, use printf to display it in the MPLAB simulator.
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