C51: LOCATING INITIALIZED VARIABLES AT ABSOLUTE ADDRESSES 如何初始化绝对定址的变量
http://www.keil.com/support/docs/301.htm
QUESTION 问题描述
I want to locate some initialized constants and variables in memory, but I can't initialize and locate them with the _at_ keyword. What's wrong?
我想初始化一些常量和变量,但我无法初始化成功当我使用_at_绝对定址后。
ANSWER 答案
C51 does not allow you to both locate a variable using the _at_ keyword and initialize it. You may use the linker to locate the variable.
Keil C51常量或变量使用_at_关键词绝对定址后,将无法直接初始化。但是可以先初始化变量,然后通过编译器设置绝对定址。
For example, if you want to locate an initialized constant array into code space...
举例如下:
- Declare your array as follows: 首先在C文件里申明并初始化变量
char const code My_Array[] = { 1,2,3,4,5 };
- Go to your linker map file a see what the segment name is for this array. Note that during this process you will probably get a warning about an unused function or something similar. This is because the compiler is expecting your array to be a function since you specified that it is in code space. Don't worry about this warning.
查看Map文件,寻找数组的segment的名字。
如果程序中没有用到数组,可能会有warning提示。
这个警告可以忽略。我们需要知道的是segment的名字。
例如Map文件:
C文件名称是Temp.c,所以其segment名字是“ ?CO?TEMP”
- Use the BL51 CODE directive or the LX51 SEGMENTS directive to specify the location of the constant segment. If the C source file name is TEMP.C the constant segment is named ?CO?TEMP. The entry ?CO?TEMP(0x5000) in the linker directive locates the segment to absolute memory address 5000h.
如果是BL51,填入: ?CO?TEMP(0x5000)。如下图所示,0x5000表示地址。
If you use µVision2 and BL51 enter ?CO?TEMP(0x5000) under Options for Target - BL51 Locate - Code:. For LX51, enter ?CO?TEMP(C:0x5000) under Options for Target - LX51 Locate - User Segments.
设置完后,重新编译,查看Map文件,我们就会发现temp.c被定址到了0x5000。
这个方法有个很大的缺点,就是只能定址segment,由于文件名先天性就是一个segment,所以将数组单独放在一个c文件里,间接实现了数组的绝对定址。
SEE ALSO
- C51: AVOIDING STARTUP INITIALIZATION OF STATIC VARIABLES
- BL51: INITIALIZING & LOCATING A VARIABLE TO A FIXED ADDRESS
- C51: INITIALIZING AN ABSOLUTELY LOCATED VARIABLE