|
|
View previous topic :: View next topic |
Author |
Message |
Eugeneo
Joined: 30 Aug 2005 Posts: 155 Location: Calgary, AB
|
Math in #define |
Posted: Sat Dec 13, 2014 12:41 pm |
|
|
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
|
|
|
Eugeneo
Joined: 30 Aug 2005 Posts: 155 Location: Calgary, AB
|
|
Posted: Sat Dec 13, 2014 3:19 pm |
|
|
Great! that is a good example. Thanks |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Mon Dec 15, 2014 2:53 am |
|
|
#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
|
|
Posted: Mon Dec 15, 2014 11:56 am |
|
|
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. |
|
|
|
|
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
|