Technical Note 27787

How to place a constant at specific address in code memory

EW targets: PIC
EW component: C/C++ compiler
Keywords: "@" / #pragma locate, constant
Last update: April 20, 2000
1) Why can I not use #pragma locate and/or the"@" syntax for constant variables?

Regarding the "@" and the #pragma locate syntax for located variables - located variables can not be initialized when defining them, thus it is not very useful for const variables. This is true up to and including v2.00A of ICCPIC (Spring 2000), the special case of constant and located variables may be handled better in future versions.

2) How can I put a constant, i.e., a RETLW #, at a specific location in code memory?

To place a RETLW constant in code memory at a specified address, you could use #pragma constseg and define a segment of your own as the example below. Note that there is a problem with pragma constseg in ICCPIC version 1.23, this is fixed in version 2.00A. As a workaround for version 1.23A, you can use the assembler example below.

3) How can I put a constant, i.e., a plain number, at a specific location in code memory?

This is useful only for chips with table read. Suggested way is to use assembly, as the example below.

Examples of how to define constants in code memory:

-- Assembler file

    PUBLIC    myasmconst1
    PUBLIC    myasmconst2
    RSEG myacseg1:CODE(1)
    DB  0x34,0x56
    RSEG myacseg2:CODE(1)
    RETLW 0x78

-- C-file

extern unsigned int __rtable myasmconst1;
extern const unsigned char myasmconst2;
#pragma constseg=myccseg
const unsigned char mycconst=0x12;
#pragma constseg=default

void main(void)
  int i;


-- Corresponding lines to add to copy of linker xcl-file (example for 17c756)