View previous topic :: View next topic |
Author |
Message |
pat
Joined: 07 Sep 2003 Posts: 40 Location: Adelaide, Australia
|
Macro help |
Posted: Wed Sep 24, 2003 8:15 am |
|
|
I'm trying to write a macro to improve readability
Here's the definition
Code: | #define turnfeton (a, b) {if (a) output_low (b); else output_high (b)}
#define bitpos_invert_fet_1 4 // Bit position for trig 1 invert flag
#define bitpos_invert_fet_2 5 // Bit position for trig 2 invert flag
#define bitpos_invert_fet_3 6 // Bit position for trig 3 invert flag
#define bitpos_invert_fet_4 7 // Bit position for trig 4 invert flag
#define fet_1 pin_b2 |
Here's how I call it from the code
Code: | int8 invert_flags = 0xf0;
turnfeton (bit_test (invert_flags, bitpos_invert_fet_1), fet_1); |
The compiler says undefined identifier 'a'. Is there another way of doing this? I tried replacing the curly brackets with normal, but no difference.
I tried writing a function, but couldn't pass a pin as a parameter, even when I defined the parameter as a const int8.
Thanks |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Sep 24, 2003 9:11 am |
|
|
#define turnfeton (a, b) {if (a) output_low (b); else output_high (b)}
------------------------------------------------------------------------
I got it to compile without errors after I did this:
1. Removed the space between turnfeton and (a,b).
2. Removed the { } braces. |
|
|
pat
Joined: 07 Sep 2003 Posts: 40 Location: Adelaide, Australia
|
|
Posted: Wed Sep 24, 2003 4:51 pm |
|
|
Excellent thanks PCM, I will try it out tonight. It looks like the first element of the macro must be without spaces.
One more question, could I extend the macro to do this?
Code: | #define turnfeton(a,b) if (bit_test (invert_flags, a)) output_low (b); else output_high (b) |
I would call this macro from the code as follows
Code: | int8 invert_flags = 0xf0;
turnfeton (bitpos_invert_fet_1, fet_1); |
Thanks again |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Wed Sep 24, 2003 5:25 pm |
|
|
What not just write functions for them. The macros are not going to save any code space verses inline functions.
Something like:
Code: |
void turnfet1on(int8 bit)
{
if (bit_test (invert_flags, bit))
output_low (fet_1);
else
output_high (fet_1);
}
void turnfet2on(int8 bit)
{
if (bit_test (invert_flags, bit))
output_low (fet_2);
else
output_high (fet_2);
}
turnfet1on (bitpos_invert_fet_1);
turnfet2on (bitpos_invert_fet_2);
|
or you could use a switch:
Code: |
#define fet1 1
#define fet2 2
void turnfeton(int8 bit, int8 fet)
{
switch (fet)
{
case fet1:
if (bit_test (invert_flags, bit))
output_low (fet_1);
else
output_high (fet_1);
break;
case fet2:
if (bit_test (invert_flags, bit))
output_low (fet_2);
else
output_high (fet_2);
break;
default:
break;
}
}
turnfeton(bitpos_invert_fet_1,fet1);
turnfeton(bitpos_invert_fet_1,fet2);
|
|
|
|
pat
Joined: 07 Sep 2003 Posts: 40 Location: Adelaide, Australia
|
|
Posted: Wed Sep 24, 2003 8:37 pm |
|
|
Thanks Mark, I initially tried using a function, but I couldn't work out how to pass a port pin as a parameter. I later saw in the help that you need to do a switch as you suggest.
Reducing code space wasn't my primary concern, I was trying to improve the readability of my code.
I actually like your first solution of having a function for each fet (I have 4 in total).
I've not used macros before, and I'd like to understand how to use them also. |
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
|
Posted: Thu Sep 25, 2003 7:54 am |
|
|
I have had to deal with a motor that runs the other direction when the line power is phased CBA instead of ABC. I do a phase check and set a phase bit. If I want Clockwise rotation I set CW and for Counter Clockwise direction CCW. I knkow this is not what your tring to do but it may give you some ideas
Code: |
if(CW)
{ Fet1=phase;
Fet2=!phase;
Fet3=phase;
Fet4=!phase;
}
if(CCW)
{ Fet1=!phase;
Fet2=phase;
Fet3=!phase;
Fet4=phase;
}
if(!CW&&!CCW)
{ Fet1=0;
Fet2=0;
Fet3=0;
Fet4=0;
}
|
|
|
|
|