I am reading some C code and came across this declaration in the program:
unsigned char serv_ctr @0x0002;
Can someone point me to documentation, or explain what the "@0x0002" is for in the Mplab XC8 v1.35 C compiler?
This is to specify an absolute address to place the variable at.
From the XC8 compiler manual page 27, section 2.5.2 Absolute Addressing:
Variables and functions can be placed at an absolute address by using the
The 8-bit compilers have used an @ symbol to specify an absolute address
@ is a common non-standard extension to the C language which allows you to declare a variable at a specific address. It can be used for memory-mapped hardware registers etc. In which case the variable must also be declared
volatile, so your example is incorrect.
Other compilers use something like
#pragma ..., all of it non-standard C.
The only rational reason (if any) why tool chains do this, is to enable register debugging on crappy debuggers. The non-standard syntax will ensure that the register becomes part of the linker output and debug info. Which in turn allows you to watch the register in the crappy debugger just as you can watch any other variable.
If you have a good debugger, it will have support and awareness of your specific MCU. Then you don't need non-standard crap in the C code, but you can write pure, portable standard C instead:
#define serv_ctr ( *(volatile uint8_t*)0x0002u )
A short extension:
This is no longer working since xc8 2.0 and up. You now had to write:
unsigned char serv_ctr __at(0x0002);
to put a variable (
serv_ctr) at an absolute address (
With XC8 2.0 it is possible to compile your old code using the
@ syntax if you set the compiler settings to use "C90" format. The setting looks like this, it is under "XC8 Global Options" and is called "C standard".