View previous topic :: View next topic |
Author |
Message |
asmallri
Joined: 12 Aug 2004 Posts: 1635 Location: Perth, Australia
|
Pre-processor expansion problem |
Posted: Fri Jul 15, 2005 10:20 pm |
|
|
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
|
|
Posted: Sat Jul 16, 2005 12:07 am |
|
|
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: 1635 Location: Perth, Australia
|
|
Posted: Sat Jul 16, 2005 12:25 am |
|
|
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
|
|
Posted: Sat Jul 16, 2005 1:27 am |
|
|
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: 1635 Location: Perth, Australia
|
|
Posted: Sat Jul 16, 2005 2:32 am |
|
|
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
|
|
Posted: Sat Jul 16, 2005 4:49 am |
|
|
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: 1635 Location: Perth, Australia
|
|
Posted: Sat Jul 16, 2005 7:17 am |
|
|
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
|
|
Posted: Sat Jul 16, 2005 7:56 am |
|
|
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
|
The White Book |
Posted: Sat Jul 16, 2005 4:53 pm |
|
|
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!!! ). I remember reading The White Book or K&R etc etc etc (boring stuff removed! ). 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!
But again, thanks for the complete answer!
~Kam (^8* |
|
|
|