variable

Publisher:等风来88888Latest update time:2011-02-27 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The variable mentioned in the class is a quantity whose value can change continuously during the execution of the program. To use a variable in a program, you must first use an identifier as the variable name and indicate the data type and storage mode used, so that the compiler system can allocate the corresponding storage space for the variable. The format for defining a variable is as follows:
[Storage type] Data type [Memory type] Variable name table
In the definition format, except for the data type and variable name table, the others are optional. There are four storage types: automatic (auto), external (extern), static (static) and register (register), and the default type is automatic (auto). The specific meaning and usage of these storage types will be further studied in Lesson 7 "Variable Storage".
The data type here is the same as the definition of the data type we learned in Lesson 4. After describing the data type of a variable, you can also choose to describe the memory type of the variable. The description of the memory type is to specify the storage area used by the variable in the C51 hardware system and accurately locate it during compilation. Table 6-1 shows the memory types that KEIL uVision2 can recognize. Note that in the AT89C51 chip, only the lower 128 bits of RAM are used. The upper 128 bits from 80H to FFH are only useful in the 52 chip and overlap with the special register address. For the address table of the special register (SFR), please see Appendix II AT89C51 Special Function Register List

Memory Type

illustrate

data

Direct access to internal data memory ( 128 bytes), fastest access speed

bdata

Bit-addressable internal data memory ( 16 bytes), allowing mixed bit and byte access

the data

Indirect access to internal data memory ( 256 bytes), allowing access to all internal addresses

pdata

Paging access to external data memory ( 256 bytes), accessed using MOVX @Ri instruction

xdata

External data memory (64KB), accessed using MOVX @DPTR instruction

code

Program memory ( 64KB), accessed with MOVC @A+DPTR instruction


Table 6-1 Memory Types

如果省略存储器类型,系统则会按编译模式SMALL,COMPACT或LARGE所规定的默认存储器类型去指定变量的存储区域。无论什么存储模式都可以声明变量在任何的8051存储区范围,然而把最常用的命令如循环计数器和队列索引放在内部数据区可以显著的提高系统性能。还有要指出的就是变量的存储种类与存储器类型是完全无关的。
SMALL存储模式把所有函数变量和局部数据段放在8051系统的内部数据存储区这使访问数据非常快,但SMALL存储模式的地址空间受限。在写小型的应用程序时,变量和数据放在data内部数据存储器中是很好的因为访问速度快,但在较大的应用程序中data区最好只存放小的变量、数据或常用的变量(如循环计数、数据索引),而大的数据则放置在别的存储区域。
COMPACT存储模式中所有的函数和程序变量和局部数据段定位在8051系统的外部数据存储区。外部数据存储区可有最多256字节(一页),在本模式中外部数据存储区的短地址用@R0/R1。
LARGE存储模式所有函数和过程的变量和局部数据段都定位在8051系统的外部数据区外部数据区最多可有64KB,这要求用DPTR数据指针访问数据。
之前提到简单提到sfr,sfr16,sbit定义变量的方法,下面我们再来仔细看看。
sfr和sfr16可以直接对51单片机的特殊寄存器进行定义,定义方法如下:
sfr 特殊功能寄存器名=特殊功能寄存器地址常数;
sfr16 特殊功能寄存器名= 特殊功能寄存器地址常数;
我们可以这样定义AT89C51的P1口
sfr P1 =0x90; //定义P1 I/O口,其地址90H
sfr关键定后面是一个要定义的名字,可任意选取,但要符合标识符的命名规则,名字最好有一定的含义如P1口可以用P1为名,这样程序会变的好读好多。等号后面必须是常数,不允许有带运算符的表达式,而且该常数必须在特殊功能寄存器的地址范围之内(80H-FFH),具体可查看附录中的相关表。sfr是定义8位的特殊功能寄存器而sfr16则是用来定义16位特殊功能寄存器,如8052的T2定时器,可以定义为:
sfr16 T2 =0xCC; //这里定义8052定时器2,地址为T2L=CCH,T2H=CDH
用sfr16定义16位特殊功能寄存器时,等号后面是它的低位地址,高位地址一定要位于物理低位地址之上。注意的是不能用于定时器0和1的定义。
sbit可定义可位寻址对象。如访问特殊功能寄存器中的某位。其实这样应用是经常要用的如要访问P1口中的第2个引脚P1.1。我们可以照以下的方法去定义:
(1)sbit 位变量名=位地址
sbit P1_1 = Ox91;
这样是把位的绝对地址赋给位变量。同sfr一样sbit的位地址必须位于80H-FFH之间。
(2)Sbit 位变量名=特殊功能寄存器名^位位置
sft P1 =0x90;
sbit P1_1 =P1 ^ 1; //先定义一个特殊功能寄存器名再指定位变量名所在的位置
当可寻址位位于特殊功能寄存器中时可采用这种方法
(3)sbit 位变量名=字节地址^位位置
sbit P1_1 =0x90 ^ 1;
这种方法其实和2是一样的,只是把特殊功能寄存器的位址直接用常数表示。
在C51存储器类型中提供有一个bdata的存储器类型,这个是指可位寻址的数据存储器,位于单片机的可位寻址区中,可以将要求可位录址的数据定义为bdata,如:
unsigned char bdata ib; //在可位录址区定义ucsigned char类型的变量ib
int bdata ab[2]; //在可位寻址区定义数组ab[2],这些也称为可寻址位对象
sbit ib7=ib^7 //用关键字sbit定义位变量来独立访问可寻址位对象的其中一位
sbit ab12=ab[1]^12;
操作符"^"后面的位位置的最大值取决于指定的基址类型,char0-7,int0-15,long0-31。
下面我们用上一课的电路来实践一下这一课的知识。同样是做一下简单的跑马灯实验,项目名为RunLED2。程序如下:

sfr P1 =0x90; //No predefined file is used here,
sbit P1_0 =P1 ^ 0; //Instead, we define the special register
sbit P1_7 =0x90 ^ 7; //The predefined file we used before actually has this function
sbit P1_1 =0x91; //Here we define the P1 port and P10, P11, P17 pins respectively

void main(void)
{
unsigned int a;
unsigned char b;
do{
for (a=0;a<50000;a++)
P1_0 =0; //Light up P1_0
for (a=0;a<50000;a++)
P1_7 =0; //Light up P1_7
for (b=0;b<255;b++)
{
for (a=0;a<10000;a++)
P1 =b; //Use the value of b to make a marquee pattern
}
P1 =255; //Turn off the LED on P1
for (b=0;b<255;b++)
{
for (a=0;a<10000;a++) //P1_1 flashes
P1_1 =0;
for (a=0;a<10000;a++)
P1_1 =1;
}
}while(1);
}

Reference address:variable

Previous article:constant
Next article:Operators and expressions (1)

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号