Variables / Memory control

Absolute RAM address assignment to a variable (1) #BYTE  intcon  = 0x0B
#BIT  
intflag = intcon.1
Constant declarations (1) CHAR CONST loop_delay = 0x07;
Creating a Constant Array (1) CHAR CONST array[4]={0x00,0x01,0x02,0x03};

//or

CHAR CONST array[4]={
    0x00,   //Element 0
    0x01,
    0x02,
    0x03}; 
//Element 3=4-1
Text replacement:
This may be done anywhere in code as long as no duplicate labels are used (Except when #UNDEFINE'd first).

Every occurrence of "mon_lamp" will be replaced by "PIN_A3" and "pointer" with "0x10" at compile time. In PICC, "PIN_A3" would actually then be replaced by a number indicating the memory bit that is the address of the actual pin (0x05 x 8 + 3 = 43d).

Be sure not to use labels that will replace text unexpectedly as in the second example.
//This is OK
#DEFINE mon_lamp PIN_A3
#DEFINE pointer 0x10


//This would not work, break is a statement
#DEFINE tempxx break
CHAR tempxx=0;
Ram variable declarations:
1.  Variable declarations within functions and sub blocks (Such as the IF/ELSE example) MUST occur before any actual code.

2.  sys_timer will be allocated and available to all functions (Global).
3.  sys_limit will be allocated, initialized to 0 and global.
4.  loop_limit will be allocated while main() is active.
5.  temp in the IF-ELSE structure will actually be two different memory locations:  Changing tempxx in the IF section will not modify tempxx used in the ELSE section.  Both will be available while main() is active, but only within their associated code blocks.  The IF temp will essentially look static as it is not initialized, the ELSE temp will not as it is always set to 5 before use.  Neither temp will be available to the lower level code of main(). This functionality was added in PICC 3.061.

CHAR sys_timer;
CHAR sys_limit=0;


VOID main(VOID){
CHAR loop_limit;

WHILE(true){
    //code
    IF (sys_limit==10)
        {
        CHAR temp;
        //code
        }
    ELSE
        {
        CHAR temp=5;
        //code
        }
    }
}
Note:
1.  Constants (#BYTE, #BIT and CONST) can't be declared within a function and must equate to a numeric value:  "=150" and "=50*3" are equivalent.

Any questions or comments?
 This page last updated on August 28, 2011