View previous topic :: View next topic |
Author |
Message |
logicchild
Joined: 16 Nov 2013 Posts: 14
|
Token concatenation in #define statement containing the (.) |
Posted: Sun Jan 12, 2014 10:31 am |
|
|
Hi,
I want to write a macro that performs the following:
Code: |
#define PORT_PIN(__x,__y) __x##.F##__y
|
so, when calling it with:
Code: |
#define LEDS PORTB
#define LED1 0
PORT_PIN(LEDS, LED1) = 1;
|
the last line should be:
but I get error that says it is "No a valid preprocessing token..."
please help |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Jan 12, 2014 2:57 pm |
|
|
Quote: | the last line should be:
PORTB.F0 = 1;
|
Your desired output is MikroC code:
http://www.mikroe.com/forum/viewtopic.php?f=13&t=25027
The output of your macro (if it worked) will not compile on CCS,
because CCS doesn't accept MikroC-specific code. Why did you post
this question on the CCS forum ? It's a question for the MikroC forum. |
|
|
logicchild
Joined: 16 Nov 2013 Posts: 14
|
It is How to |
Posted: Mon Jan 13, 2014 3:00 am |
|
|
Hi,
Thanks for the reply.
The idea is that I want to concatenate the token that contains the (.) period char. regardless of what language that I use! |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
Re: It is How to |
Posted: Mon Jan 13, 2014 3:43 am |
|
|
logicchild wrote: | The idea is that I want to concatenate the token that contains the (.) period char. regardless of what language that I use! |
You always need to be aware of the capabilities and limitations of specific implementation of any language.
In this case CCS C does not have a full spec. preprocessor. It is limited and does not do some things like concatenation and stringization nor can it split or slice tokens. Neither is it possible to view the output of the pre-processor. It appears that the the pre-processing is not a separate step as has been traditional in C, instead it appears to be just a text process done in-line during compilation.
The simple answer is CCS C won't do what you want, and begs the question why you need this particular functionality. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Mon Jan 13, 2014 4:32 am |
|
|
Actually the CCS preprocessor, does do catenation fine.
However his output, is MicroC instructions, which can't work with CCS.
CCS won't accept the port reference with '.', unless you have declared a structure to make this work, hence the failure.
For instance:
Code: |
struct bits
{
int b0:1;
int b1:1;
int b2:1;
int b3:1;
int b4:1;
int b5:1;
int b6:1;
int b7:1;
};
struct bits PORTA,PORTB;
#byte PORTA = getenv("SFR:PORTA")
#byte PORTB = getenv("SFR:PORTB")
#define bit_ref(PNUM, bit) PORT##PNUM.b##bit
void main()
{
bit_ref(A,2)=1;
while(TRUE)
{
//Something
}
}
|
Because I have generated the 'dotable' references, this then accesses PORTA.B2
Best Wishes |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1345
|
|
Posted: Mon Jan 13, 2014 8:53 am |
|
|
yeah, I've been using a lot of those pre processor features for a while now. I won't say they are bug free, but they work for what I have been doing for the last few years.
I agree with Ttelmah in that this is a MikroC trying to be used in CCS issue. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Mon Jan 13, 2014 9:11 am |
|
|
It was PCM, who pointed out the problem first...
They key though is understanding that it is not the catenation that is the problem, but the syntax of the result. Using '.' to access a bit, is not standard C. It can be adapted, by creating bitfields as I show.
Best Wishes |
|
|
|