|
发表于 2009-6-30 09:05:12
|
显示全部楼层
关于变量是否要赋初值的问题,要根据所使用的编译器决定。
实际上现在大多数的这类专用的C编译器都会在你的代码前自动加上一段启动代码的,这段代码中要对堆栈指针、以及一些寻址寄存器等做初始化,其中包括变量初始化(为0)。因此你的代码中可以不对变量做初始化为0的工作。具体你应该查看所使用编译器的帮助书册。
下面是CVAVR中的说明:
Using an External Startup File
In every program the CodeVisionAVR C compiler automatically generates a code sequence to make the following initializations immediately after the AVR chip reset:
1. interrupt vector jump table
2. global interrupt disable
3. EEPROM access disable
4. Watchdog Timer disable
5. external SRAM access and wait state enable if necessary
6. clear the R2...R14 registers ===> 变量清零部分!
7. clear the SRAM ===> 变量清零部分!
8. initialize the global variables located in SRAM
9. initialize the Data Stack Pointer register Y
10. initialize the Stack Pointer register SP
11. initialize the UBRR register if necessary
The automatic generation of code sequences 2 to 8 can be disabled by checking the Code Generation|Use an External Startup Initialization File check box in the Project|Configure|C Compiler|Code Generation dialog window. The C compiler will then include, in the generated .asm file, the code sequences from an external file that must be named STARTUP.ASM . This file must be located in the directory where your main C source file resides.
You can write your own STARTUP.ASM file to customize or add some features to your program. The code sequences from this file will be immediately executed after the chip reset.
A basic STARTUP.ASM file is supplied with the compiler distribution and is located in the ..\BIN directory.
Here's the content of this file:
;CodeVisionAVR C Compiler
;(C) 1998-2006 Pavel Haiduc, HP InfoTech s.r.l.
;EXAMPLE STARTUP FILE FOR CodeVisionAVR V1.24.1 OR LATER
.EQU __CLEAR_START=0X60 ;START ADDRESS OF SRAM AREA TO CLEAR
;SET THIS ADDRESS TO 0X100 FOR THE
;ATmega128 OR ATmega64 CHIPS
.EQU __CLEAR_SIZE=256 ;SIZE OF SRAM AREA TO CLEAR IN BYTES
CLI ;DISABLE INTERRUPTS
CLR R30
OUT EECR,R30 ;DISABLE EEPROM ACCESS
;DISABLE THE WATCHDOG
LDI R31,0x18
OUT WDTCR,R31
OUT WDTCR,R30
OUT MCUCR,R30 ;MCUCR=0, NO EXTERNAL SRAM ACCESS
;CLEAR R2-R14
LDI R24,13
LDI R26,2
CLR R27
__CLEAR_REG:
ST X+,R30
DEC R24
BRNE __CLEAR_REG
;CLEAR SRAM
LDI R24,LOW(__CLEAR_SIZE)
LDI R25,HIGH(__CLEAR_SIZE)
LDI R26,LOW(__CLEAR_START)
LDI R27,HIGH(__CLEAR_START)
__CLEAR_SRAM:
ST X+,R30
SBIW R24,1
BRNE __CLEAR_SRAM
;GLOBAL VARIABLES INITIALIZATION
LDI R30,LOW(__GLOBAL_INI_TBL*2)
LDI R31,HIGH(__GLOBAL_INI_TBL*2)
__GLOBAL_INI_NEXT:
LPM
ADIW R30,1
MOV R24,R0
LPM
ADIW R30,1
MOV R25,R0
SBIW R24,0
BREQ __GLOBAL_INI_END
LPM
ADIW R30,1
MOV R26,R0
LPM
ADIW R30,1
MOV R27,R0
LPM
ADIW R30,1
MOV R1,R0
LPM
ADIW R30,1
MOV R22,R30
MOV R23,R31
MOV R31,R0
MOV R30,R1
__GLOBAL_INI_LOOP:
LPM
ADIW R30,1
ST X+,R0
SBIW R24,1
BRNE __GLOBAL_INI_LOOP
MOV R30,R22
MOV R31,R23
RJMP __GLOBAL_INI_NEXT
__GLOBAL_INI_END:
The __CLEAR_START and __CLEAR_SIZE constants can be changed to specify which area of SRAM to clear at program initialization.
The __GLOBAL_INI_TBL label must be located at the start of a table containing the information necessary to initialize the global variables located in SRAM. This table is automatically generated by the compiler.
==========================================================================================================
如果考虑标准或移植,你可以加上初始化语句,但在上面的情况下就是多余了,并且浪费了程序存储器的资源(单片机内部的资源是有限的,需要尽量节约)
学习使用单片机,以及单片机的编程与通常在PC上编程有许多的不同,要求具备的基础更加高,尤其是对于要想真正“会”用的人来讲。比如你应该(也可以讲是必须的)具备汇编的基础,掌握基本的使用汇编编写代码的能力和概念。这样你就知道,在汇编中必须要首先初始化堆栈指针寄存器,而在C中你就不要管了。这不是说使用C就不需要了,而是C编译器把常规的东西自动帮你加上去了。 |
|