<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.arm.com/utility/feedstylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Define act as function?</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/20877/define-act-as-function</link><description> 
I&amp;#39;ve been using defines as fucntions like this: 

 
sbit DRIVE_PIN05 = P1^3; 

 
#define DRIVE_PIN05_HIGH (P1MDOUT |= 0x08);(DRIVE_PIN05 = 0) 
#define DRIVE_PIN05_LOW (P1MDOUT |= 0x08);(DRIVE_PIN05 = 1) 
#define DRIVE_PIN05_OFF (P1MDOUT &amp;amp;= 0xF7);(DRIVE_PIN05</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: Define act as function?</title><link>https://community.arm.com/thread/74676?ContentTypeID=1</link><pubDate>Fri, 29 Dec 2006 20:38:09 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:e833b1db-fdc7-4c57-83ad-3d2b47b0efe1</guid><dc:creator>MC Potgieter</dc:creator><description>&lt;p&gt;&lt;p&gt;
Thanks to Dan and Hans-Bernhard.&lt;/p&gt;

&lt;p&gt;
I appreciate the help, solved the problem looking at your macro
defenitions and the link.&lt;/p&gt;

&lt;p&gt;
MC&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Define act as function?</title><link>https://community.arm.com/thread/88093?ContentTypeID=1</link><pubDate>Fri, 29 Dec 2006 14:34:23 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:91c1ceee-ac3d-4ea9-87d4-408b39ef0cae</guid><dc:creator>HansBernhard Broeker</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;in other words: is e.g. DRIVE_PIN05 defined earlier in the file
than the include of #define DRIVE_PIN05_OFF (P1MDOUT &amp;amp;=
0xF7);(DRIVE_PIN05 = 1). The preprocessor is not multi pass, if
something is not defined before it is used, no go.&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
Not quite. It&amp;#39;s not necessarily a problem if the definition of
DRIVE_PIN05 doesn&amp;#39;t precede the line&lt;/p&gt;

&lt;pre&gt;
#define DRIVE_PIN05_OFF (P1MDOUT &amp;amp;= 0xF7);(DRIVE_PIN05 = 1)
&lt;/pre&gt;

&lt;p&gt;
It only has to precede every actual &lt;b&gt;expansion&lt;/b&gt; of
DRIVE_PIN05_OFF, i.e. each time it&amp;#39;s DRIVE_PIN05_OFF is used outside
of a macro body.&lt;/p&gt;

&lt;pre&gt;
#define B A
#define A C
B
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
preprocesses to&lt;/p&gt;

&lt;pre&gt;
C
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
as intended.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Define act as function?</title><link>https://community.arm.com/thread/47483?ContentTypeID=1</link><pubDate>Fri, 29 Dec 2006 14:27:38 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:738e3af0-6fc8-4e48-8096-a9b95ef20979</guid><dc:creator>HansBernhard Broeker</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;It gives me problems when I include the above define into
another define, why?&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
Impossible to tell, from the given info. You forgot to disclose
both the actual problems this gave you, and an actual, compilable
example case. So nobody can see the error messages you&amp;#39;re seeing.&lt;/p&gt;

&lt;p&gt;
That leaves guesswork, most of which would reduce to:&lt;br /&gt;
you want to (re-)read the C FAQ, in particular this node: &lt;a href="http://c-faq.com/cpp/multistmt.html"&gt;c-faq.com/.../multistmt.html&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Define act as function?</title><link>https://community.arm.com/thread/88090?ContentTypeID=1</link><pubDate>Fri, 29 Dec 2006 11:05:32 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:2ed0e469-19d4-4844-a9e3-732d3f95ee57</guid><dc:creator>Dan Henry</dc:creator><description>&lt;p&gt;&lt;p&gt;
I forgot to mention that the style convention for function-like
macros is to have them look like function invocation where they are
used; that is, with a parameter list that is optionally empty.&lt;/p&gt;

&lt;pre&gt;
#define SUPPLY_ON()      do {DRIVE_PIN05_HIGH;DRIVE_PIN24_LOW;} while (0)
#define SUPPLY_OFF()     do {DRIVE_PIN05_OFF; DRIVE_PIN24_OFF;} while (0)

void supply(unsigned char on) {
    if (on)
        SUPPLY_ON();
    else
        SUPPLY_OFF();
}
&lt;/pre&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Define act as function?</title><link>https://community.arm.com/thread/47484?ContentTypeID=1</link><pubDate>Fri, 29 Dec 2006 10:27:09 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:fd52f6c0-7d03-4332-a7ba-bfee4932acf9</guid><dc:creator>Dan Henry</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;&amp;quot;Maybe my syntax is incorrect or it can&amp;#39;t be done?&amp;quot;&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
For multi-statement macros, the convention is to wrap them with
do/while(0). The parentheses are unnecessary.&lt;/p&gt;

&lt;pre&gt;
#define DRIVE_PIN05_HIGH do {P1MDOUT |= 0x08; DRIVE_PIN05 = 0;} while (0)
#define DRIVE_PIN05_LOW  do {P1MDOUT |= 0x08; DRIVE_PIN05 = 1;} while (0)
#define DRIVE_PIN05_OFF  do {P1MDOUT &amp;amp;= 0xF7; DRIVE_PIN05 = 1;} while (0)
#define SUPPLY_ON        do {DRIVE_PIN05_HIGH;DRIVE_PIN24_LOW;} while (0)
#define SUPPLY_OFF       do {DRIVE_PIN05_OFF; DRIVE_PIN24_OFF;} while (0)
&lt;/pre&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Define act as function?</title><link>https://community.arm.com/thread/50418?ContentTypeID=1</link><pubDate>Fri, 29 Dec 2006 07:36:21 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:2359caab-f3fe-4d70-847d-2a3a38d07dec</guid><dc:creator>erik  malund</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;Is it there anything wrong with doing it like this?&lt;br /&gt;
It gives me problems when I include the above define into another
define, why?&lt;br /&gt;&lt;/i&gt;&lt;br /&gt;
I, occasionally, run into the same problem, and always I find that a
define using another define is there before the define of the define
used.&lt;/p&gt;

&lt;p&gt;
in other words: is e.g. DRIVE_PIN05 defined earlier in the file
than the include of #define DRIVE_PIN05_OFF (P1MDOUT &amp;amp;=
0xF7);(DRIVE_PIN05 = 1). The preprocessor is not multi pass, if
something is not defined &lt;b&gt;before&lt;/b&gt; it is used, no go.&lt;/p&gt;

&lt;p&gt;
Erik&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>