Automatically Initializing Data Segment Values in MAX-IDE

[09-11 23:02:11]   来源:http://www.88dzw.com  单片机学习   阅读:8704

文章摘要:This approach works well enough, but there are several problems with it. The location of each variable must be determined in advance. This task can be time-consuming, especially if it is decided later to move all variables to a different area of data memory. Care must be taken not to accidentally us

Automatically Initializing Data Segment Values in MAX-IDE,标签:单片机开发,单片机原理,单片机教程,http://www.88dzw.com
This approach works well enough, but there are several problems with it.
  • The location of each variable must be determined in advance. This task can be time-consuming, especially if it is decided later to move all variables to a different area of data memory.
  • Care must be taken not to accidentally use the same location for more than one variable. If this mistake is made, it can be difficult to track bugs.
  • Any initial (starting) values for variables must be explicitly loaded by application code, as shown in the last line above. This action can consume a large amount of code space if there are many variables to initialize in this manner.
A more efficient approach takes advantage of MAX-IDE's mechanism to declare separate code and data segments. This method allows the application author to specify which portions of the assembly code file are destined for code space and which portions are destined for data space.
segment code

   move  DP[0], #VarA       ; Point to VarA
   move  Acc, @DP[0]        ; Get current value of VarA
   add   #1                 ; Increment it
   move  @DP[0], Acc        ; Store value back in VarA

segment data

VarA:
   dw    0394h              ; Initial value for VarA

In the above approach, addresses for variables declared in the data segment are determined automatically by the assembler as it parses the file with the same method used to assign addresses to labels in code space. Labels are used to assign symbolic names to these variable addresses, and the dw and db statements can be used to initialize word-sized and byte-sized variables with starting values. In this case, assuming that no previous segment data directive was found in the assembly file, the assembler will begin the data segment at address 0000h. This means that VarA will be stored at word address 0000h. As in code space, the org statement can force variables to be located at the beginning of a specified address.

Initializing the Data Segment

In the previous code listing, variable VarA is defined (using the dw statement) to have an initial value of 0394h. But this value is never loaded into VarA in the code. How, then, is this value initialized? The answer is that the initialization of the data segment is performed automatically by MAX-IDE when the project is compiled and executed.

The MaxQAsm assembler responds to the segment data directive by generating a secondary hex output file. Normally, a hex file is generated for a project containing code data. For example, if project "example.prj" is compiled, a hex file will be created named "example.hex" containing the code data generated by assembling the project files. If a data segment is defined, an additional hex file will be created named "example_d.hex", which contains the data assembled in this segment.

When the project is executed, MAX-IDE checks to see if a data segment file (ending in _d.hex) was generated during project compilation. If a data segment file exists, MAX-IDE uses the standard JTAG loader to load the data from this segment into the data SRAM of the device. This is done after the standard hex file has been loaded into program memory.

This method works well during the development cycle, when the device is connected to a JTAG adapter and MAX-IDE reloads the code and segment data before each application run. However, once the device is powered off and on and allowed to run independently (with no debugger connected), MAX-IDE no longer has the ability to load the data segment with the proper values before each run. The variables will no longer be set to their expected values, and the application may execute incorrectly as a result. This type of failure can be difficult to analyze, because as soon as the device is hooked back up to the debugger, MAX-IDE will begin loading the data segment again before each run and the problem will instantly vanish.

上一页  [1] [2] [3] [4] [5]  下一页


Tag:单片机学习单片机开发,单片机原理,单片机教程单片机学习
分类导航
最新更新
热门排行