syntax error, expecting declaration seen while compiling C code for 8051 microcontroller

826 views Asked by At

I have a piece of C code in a header file used for a 8051 microcontroller as below -

#define Sfr(x, y)       sfr x = y
#define Sbit(x, y, z)   sbit x = y^z
#define Sfr16(x,y)      sfr16 x = y

/*----------------------------------------*/
/* Include file for 8051 SFR Definitions  */
/*----------------------------------------*/

/*  BYTE Register  */

Sfr(P0 , 0x80);      
Sbit (P0_7 , 0x80, 7);
Sbit (P0_6 , 0x80, 6);

When compiling, I get the error line 17: syntax error, expecting declaration. Any mistake in the usage of the macro?

2

There are 2 answers

4
alpartis On BEST ANSWER

Each time you use any of your macros, it declares the same variable 'x' over and over.

There's also no evidence that the compiler knows what sfir or sbit orsfr16 are.

1
user3629249 On

a macro must have the '(' immediately after the macro name. the posted code has a space between the macro name and the '('. that is why the compiler is expecting a function name.

Also, these macros will not work for all special function registers (SFRs) because many of the registers are affected by reading, so the normal read/modify/write cycle is not acceptable. For such registers, it is MUCH better to keep an image of the register in RAM. modify the image, then write the whole register. Therefore the SBIT macro cannot be used for all of the special function registers. there are also restrictions on which opcodes can be used to manipulate bits in the SFRs, again, because of the read/modify/write cycle having undesirable side effects.