初始化变量并指定存储地址的同时:这可能吗? [英] Initializing a variable and specifying the storage address the same time: is it possible?

查看:173
本文介绍了初始化变量并指定存储地址的同时:这可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

codevision 编译器的爱特梅尔的处理器,还有就是指定一个全局变量的存储地址,一个可能性,例如

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

当然,按照标准C,变量可以在声明中初始化

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

int a=42;

不过,我没有发现任何可能做他们两个。 int类型的@为0x100 = 42 int类型的= 42 @ 0x100的; 不工作时,会导致编译器错误。

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;
    //...
}

不过,如果我有在 EEPROM变量,我需要初始化它们,因为这是自动生成与它的值EEPROM文件的唯一途径。我不能在以后分配这些值,因为在这种情况下,它实际上在程序的每次启动值写入到EEPROM中。

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.

推荐答案

虽然我知道没有办法的EEPROM变量直接分配到具体地址和初始化,我发现这个链接非常有用:<一href=\"http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=106223&highlight=eeprom%20address\"相对=nofollow>在固定地址 EEPROM数据。

While I know of no way to directly assign an EEPROM variable to a specific address and initialize it, I found this link very helpful: EEPROM data at fixed address.

我使用的解决方案是在声明EEPROM中的结构和在你的程序中的所有EEPROM中的变量是结构的成员。您定义的结构的成员的顺序将它们链接器放置在EEPROM地址空间的顺序。由于该结构将成为全球唯一的EEPROM声明它是安全的说,这将解决到地址0x0000。因此,你会知道每个EEPROM变量的地址。

The solution I used was that of declaring a struct in EEPROM and having all EEPROM variables in your program be a member of that struct. The order you define the members of the struct will be the order they are placed in the EEPROM address space by the linker. Since the struct will be the only global EEPROM declaration it is safe to say it will be addressed to address 0x0000. Therefore, you will know the address of each EEPROM variable.

例如:

 typedef eeprom struct EEvars
{
    eeprom char    foo1;   // will be located at EEPROM address 0x0000
    eeprom char    foo2;   // will be located at EEPROM address 0x0001
    eeprom short   foo3;   // will be located at EEPROM address 0x0002
    eeprom long    foo4;   // will be located at EEPROM address 0x0004
    eeprom char[3] fooArr; // fooArr[0] @ 0x0008; fooArr[1] @ 0x0009; 
                           // fooArr[2] @ 0x000A 
} EEVARS;

可以然后在该结构的声明初始化的变量。当你编译,这将在已知的EEPROM地址创建初始化值.eep文件。

You can then initialize the variables in the declaration of the structure. When you compile, this will create the .eep file with the initialized values at the known EEPROM addresses.

eeprom EEVARS eepromInit = {0xAA, 0xBB, 0xCCDD, 0xEEEEFFFF, {0xF0, 0xF1, 0xF2}};

其中,AVR的引导程序部分用于升级FLASH和新的程序需要访问存储在EEPROM变量,这尤其适用于场景。它甚至可以让你只要你加入他们的跨软件更新添加EEPROM变量到结构的末尾,以免打扰那些变量的地址已经确定。

This works especially well in scenarios where the bootloader section of the AVR is used to upgrade the FLASH and the new program needs to access the stored EEPROM variables. It even allows you to add EEPROM variables across software updates as long as you add them to the end of the struct so as not to disturb the addresses of those variables already established.

这篇关于初始化变量并指定存储地址的同时:这可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆