Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing a variable and specifying the storage address the same time: is it possible?

Tags:

c

embedded

avr

In the codevision compiler for Atmel processors, there is a possibility to specify the storage address of a global variable, for example

int a @0x100; // will place the variable at the address 0x100 in RAM

Of course, as per standard C, variables can be initialized upon declaration

int a=42;

However, I did not find any possibility to do them both. int a @0x100 = 42 or int a = 42 @0x100; don't work, they cause compiler errors.

You might ask why it is so important to do it, because one could simply have

int a @0x100;

int main()
{
    a = 42;
    //...
}

However, if I have variables in the EEPROM, I need to initialize them, because this is the only way to automatically generate the eeprom file with the values in it. I can't assign those values later, because in that case it would actually write the values into the eeprom at each start of the program.

like image 249
vsz Avatar asked Aug 02 '12 05:08

vsz


1 Answers

Just a look at CodeVisionAVR help,

"The following procedure must be used if a global variable, placed at a specific address using the @ operator, must be initialized during declaration:

/* the variable will be stored in EEPROM at address 0x10 */

eeprom int abc @0x10;

/* and it will be initialized with the value 123 */ 

eeprom int abc=123; 
like image 75
user7372630 Avatar answered Sep 17 '22 09:09

user7372630