关于单片机全局变量初始化的问题

关于单片机全局变量初始化的问题,第1张

  变量分为局部与全局,局部变量又可称之为内部变量。由某对象或某个函数所创建的变量通常都是局部变量,只能被内部引用,而无法被其它对象或函数引用。

  全局变量既可以是某对象函数创建,也可以是在本程序任何地方创建。全局变量是可以被本程序所有对象或函数引用。一个局部变量在被其它对象引用时,会是一个空值。但全局变量却不会出现这种情况。

  全局变量使用注意

  全局变量的存在主要有以下一些原因:

  关于单片机全局变量初始化的问题,关于单片机全局变量初始化的问题,第2张

  1,使用全局变量会占用更多的内存(因为其生命期长),不过在计算机配置很高的今天,这个不应该算什么问题,除非使用的是巨大对象的全局变量,能避免就一定要避免。

  2,使用全局变量程序运行时速度更快一些(因为内存不需要再分配),同样也快不了多少。

  3,对于局部变量的名字空间污染,这个在不使用太多变量时是可以避免的。

  4,当全局变量与局部变量重名的时候,起作用的是局部变量,全局变量被屏蔽掉。

  5,还可以用extern在函数外对全局变量声明,使全局变量的作用域从声明处到文件的结束。

  总之,全局变量可以使用,但是全局变量使用时应注意的是尽可能使其名字易于理解,而且不能太短,避免名字空间的污染;避免使用巨大对象的全局变量。

  关于单片机全局变量初始化的问题,关于单片机全局变量初始化的问题,第3张

  如下例所示声明全局变量:

  Global y As String

  全局变量使用

  面向对象语言中的使用在现代的面向对象语言如JavaC++++,C#,Ruby中,由于变量都是封装在类里面的,对别的类不可见,所以已经几乎完全抛弃了全局变量的概念。然而,可以通过把一个类定义为public staTIc,把类成员变量也定义为public staTIc,使该变量在内存中占用固定、唯一的一块空间,来实现全局变量的功能。

  关于单片机全局变量初始化的问题

  假如说在keil中定义了一个全局变量int i = 0x1234;这个i的初始化肯定是在上电之后,main函数之前。编译完之后debug,从地址0开始执行

  ;-------------------------------------------------------------------------

  ; STRUCTURE OF THE INITIALIZATION INFORMATION

  ; -------------------------------------------

  ; This section describes the initialization data generated by C51 for

  ; explicit variable initializations (in segment ?C_INITSEC)。

  ;

  ; Explicit variable initilizations at C source level are stored by C51 in

  ; the segment ?C_INITSEC. All partial segments are combined at linker level

  ; to one segment. The segment end value DB 0 is taken from this library module

  ; INIT.A51.

  ;

  ; Structure of the ?C_INITSEC information:

  ; 《Info》 (see below) [BYTE] ----+ repeated

  ; 《additional info》 [BYTES depend on Info] ----+ repeated

  ; 0x00 [BYTE] 《end of list mark》

  ;

  ; 《Info》 has the following format:

  ;

  ; Bit 7 6 5 4 3 2 1 0

  ; 《Info》 T T B L L L L L T=Type B=BIGBIT L=LENGTH

  ;

  ; If BIGBIT is set, another LENGTH BYTE FOLLOWS. The LENGHT

  ; info of the first byte is then the HIGH part.

  ;

  ; Typ is one of the following:

  ; 0 := IDATA init values; the following bytes follow:

  ; - 1 byte address

  ; - init data bytes according LENGTH specification

  ;

  ; 1 := XDATA init values; the following bytes follow:

  ; - 2 byte address (high byte first)

  ; - init data bytes according LENGTH specification

  ;

  ; 2 := PDATA init values; the following bytes follow:

  ; - 1 byte address

  ; - init data bytes according LENGTH specification

  ;

  ; 3, BIGBIT=0 := BIT init values; the followign bytes follow:

  ; - 1 byte for each bit according LENGTH specification

  ; this byte has the following format:

  ;

  ; Bit 7 6 5 4 3 2 1 0

  ; I B B B B B B B I := state of the bit

  ; B := bit address

  ;

  ; 3, BIGBIT=1 := HDATA init values; the following bytes follow:

  ; - another LENGTH byte (since BIGBIT is always 1)

  ; - 3 byte address (MSB first)

  ; - data bytes according LENGTH specification

  ;

  ;----------------------------------------------------------------------

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/dianzi/2717405.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-08-17
下一篇 2022-08-17

发表评论

登录后才能评论

评论列表(0条)

保存