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 CCS Technical Support

Pre-processor expansion problem

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



Joined: 12 Aug 2004
Posts: 1634
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

Pre-processor expansion problem
PostPosted: Fri Jul 15, 2005 10:20 pm     Reply with quote

PCH 3.228 - PIC18F1320

With the following defines:
Code:

#define LATA 0x0F89
#define LED_Status   LATA, 4   // status LED - low to illumuniate
#define bit_toggle(my_port, my_bit) {#asm  btg  my_port, my_bit  #endasm}


This compiles sucessfully:
Code:
      bit_toggle(LATA, 4);


The following give a: *** Error 95 "xxx.c" Line 26(23,24): Expecting an opcode mnemonic

Code:
      bit_toggle(LED_Status);


I have previoulsy nested defines but with a single level (1:1) substitution. What am I doing wrong?

Thanks..
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Jul 16, 2005 12:07 am     Reply with quote

Did you know that CCS has a toggle function ?
Here's the .LST file. It's setting the TRIS, but that's because I'm
using Standard i/o. If you use Fast i/o mode, then it just does
the BTG instruction.
Code:

.................... output_toggle(PIN_A4); 
0020:  BCF    F92.4
0022:  BTG    F89.4
asmallri



Joined: 12 Aug 2004
Posts: 1634
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Sat Jul 16, 2005 12:25 am     Reply with quote

PCM programmer wrote:
Did you know that CCS has a toggle function ?
Here's the .LST file. It's setting the TRIS, but that's because I'm
using Standard i/o. If you use Fast i/o mode, then it just does
the BTG instruction.


Thanks - I did not realise this function worked on the LAT, I thought it operated directly on the port.

I would still like to know how to do the nested precompiler substitution.
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
bb



Joined: 06 Apr 2005
Posts: 7
Location: Germany

View user's profile Send private message

PostPosted: Sat Jul 16, 2005 1:27 am     Reply with quote

Hi Andrew,

Quote:
I would still like to know how to do the nested precompiler substitution

The problem is the comment.
Code:
#define LED_Status   LATA, 4   // status LED - low to illumuniate


The line
Code:
bit_toggle(LED_Status);

gets expanded by the preprocessor to:
Code:
bit_toggle(0x0F89, 4  // status LED - low to illumuniate);


When using a comment in the same line as '#define', you'll have to do it this way:
Code:
#define LED_Status   LATA, 4   /* status LED - low to illumuniate */


Best regards,
Bernd
asmallri



Joined: 12 Aug 2004
Posts: 1634
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Sat Jul 16, 2005 2:32 am     Reply with quote

Thanks Bernd but that did not work either. even removing the comment completely from the line did not work.

Regards, Andrew
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
bb



Joined: 06 Apr 2005
Posts: 7
Location: Germany

View user's profile Send private message

PostPosted: Sat Jul 16, 2005 4:49 am     Reply with quote

Try
Code:
#define LATA 0x0F89
#define LED_Status   LATA, 4
#define bit_toggle(my_bit) {#asm  btg  my_bit  #endasm}
   
bit_toggle (LED_Status);


Best regards,
Bernd
asmallri



Joined: 12 Aug 2004
Posts: 1634
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Sat Jul 16, 2005 7:17 am     Reply with quote

I tried it for interest and it did work. However I specifically want to be able to specify addreeses and bits separately in the call.

Thanks, Andrew
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
bb



Joined: 06 Apr 2005
Posts: 7
Location: Germany

View user's profile Send private message

PostPosted: Sat Jul 16, 2005 7:56 am     Reply with quote

Quote:
However I specifically want to be able to specify addreeses and bits separately in the call.

I don't think that this is possible when using a single '#define' for both arguments.

Quote:
When a function macro is being processed, the steps are as follows:

1. All of its arguments are identified.

2. Except in the cases listed in item 3 below, if any of the tokens in an argument are themselves candidates for macro replacement, the replacement is done until no further replacement is possible. If this introduces commas into the argument list, there is no danger of the macro suddenly seeming to have a different number of arguments; the arguments are only determined in the step above.

3. In the macro replacement text, identifiers naming one of the macro formal arguments are replaced by the (by now expanded) token sequence supplied as the actual argument. The replacement is suppressed only if the identifier is preceded by one of # or ##, or followed by ##.

The C book, chapter 7.3.2.1


The arguments of the function macro are identified first. Therefore 'LED_Status' is treated as one argument (the comma is meaningless) and the macro misses the second argument.

Best regards,
Bernd
kam



Joined: 15 Jul 2005
Posts: 59

View user's profile Send private message

The White Book
PostPosted: Sat Jul 16, 2005 4:53 pm     Reply with quote

Thanks Bernd for the complete explanation. What I got out of it was very interesting...

I am re-entering the "chip programming" world after many years of PC (Intel, Motorola) programming (so everyone watchout for questions!!! Razz ). I remember reading The White Book or K&R etc etc etc (boring stuff removed! Shocked ). But after all these years I just assumed that the compiler makers would "fix" or allow for a "fix" in there compilers. I just assumed that the compiler maker would have at least given a warning or something. I can imagine the head scrathing I would have done if I did not know that they are 100% K&R in the above question. So for any bizzar problems the best initial approach is the check out the asm and see what the compiler did and make sure it did that I thought! God what an pain! They need a "MAKE-IT-ANSI-2005-AND-ISSUE-WARNINGS-TO-HELP-THE-PROGRAMMER" switch! Razz Razz Razz

But again, thanks for the complete answer!

~Kam (^8*
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