c51中sbit/sfr 用法 您所在的位置:网站首页 单片机中的sfr c51中sbit/sfr 用法

c51中sbit/sfr 用法

2024-07-06 10:21| 来源: 网络整理| 查看: 265

  c51中sbit/sfr 用法

  1 首先区分bit 和sbit

  bit和int char之类的差不多,只不过char=8位, bit=1位而已。都是变量,编译器在编译过程中分配地址。除非你指定,否则这个地址是随机的。这个地址是整个可寻址空间,RAM+FLASH+扩展空间。bit只有0和1两种值,意义有点像Windows下VC中的BOOL。

  sbit是对应可位寻址空间的一个位,可位寻址区:20H~2FH。一旦用了sbi xxx = REGE^6这样的定义,这个sbit量就确定地址了。sbit大部分是用在寄存器中的,方便对寄存器的某位进行操作的。

  sbit的用法有三种:

  第一种方法:sbit 位变量名=地址值

  第二种方法:sbit 位变量名=SFR名称^变量位地址值

  第三种方法:sbit 位变量名=SFR地址值^变量位地址值

  如定义PSW中的OV可以用以下三种方法:

  sbit OV=0xd2 (1)说明:0xd2是OV的位地址值

  sbit OV=PSW^2 (2)说明:其中PSW必须先用sfr定义好

  sbit OV=0xD0^2 (3)说明:0xD0就是PSW的地址值

  因此这里用sfr P1_0=P1^0;就是定义用符号P1_0来表示P1.0引脚,如果你愿意也可以起P10一类的名字,只要下面程序中也随之更改就行了。

  注意:“^”实质代表的是异或运算,可以算一下,恰好符合。

  2.Sfr用法

  sfr 似乎不是标准C 语言的关键字,而是Keil 为能直接访问80C51 中的SFR 而提供了一个新的关键词,其用法是:sfrt 变量名=地址值。

  例:sfr P1 = 0x90;

  这样的一行即定义P1 与地址0x90 对应,P1 口的地址就是0x90.

  SFR的定义在头文件reg51.h或reg52.h中。

  [Keil 帮助]

  sfr Types

  SFRs are declared in the same fashion as other C variables. The ONLY difference is that the data type specified is sfr rather than CHAR or int. For example:

  sfr P0 = 0x80; /* Port-0, address 80h */

  sfr P1 = 0x90; /* Port-1, address 90h */

  sfr P2 = 0xA0; /* Port-2, address 0A0h */

  sfr P3 = 0xB0; /* Port-3, address 0B0h */

  P0, P1, P2, and P3 are the SFR NAME declarations. Names for sfr variables are defined just like other C variable declarations. Any symbolic NAMEmay be used in an sfr declaration.

  The address specification after the equal sign (‘=’) must be a numeric constant. Expressions with operators are not allowed. Classic 8051 devicesSUPPORT the SFR address range 0x80 to 0xFF. The PHILIPS 80C51MX provides an additional extended SFR space with the address range 0x180 to 0x1FF.

  Note

  sfr variables may not be declared inside a function. They must be declared outside of the function body.

  COPYRIGHT (c) Keil SOFTWARE, Inc. and Keil Elektronik GmbH. All rights reserved.

  sbit Types

  With typical 8051 applications, it is often necessary to access individual bits within an SFR. The Cx51 Compiler makes this possible with the sbit data type which provides access to bit-addressable SFRs and other bit-addressable objects. For example:

  sbit EA = 0xAF;

  This declaration defines EA to be the SFR bit at address 0xAF. On the 8051, this is the enable all bit in the interrupt enable register.

  Any symbolic NAME can be used in an sbit declaration. The expression to the right of the equal sign (‘=’) specifies an absolute bit address for the symbolic NAME. There are three variants for specifying the address:

  Variant 1

  sbit NAME = sfr-NAME ^ bit-position;

  The previously declared SFR (sfr-NAME) is the base address for the sbit. It must be evenly divisible by 8. The bit-position (which must be aNUMBER from 0-7) follows the carat symbol (‘^’) and specifies the bit position to access. For example:

  sfr PSW = 0xD0;

  sfr IE = 0xA8;

  sbit OV = PSW^2;

  sbit CY = PSW^7;

  sbit EA = IE^7;

  Variant 2

  sbit NAME = sfr-address ^ bit-position;

  A CHARacter constant (sfr-address) specifies the base address for the sbit. It must be evenly divisible by 8. The bit-position (which must be aNUMBER from 0-7) follows the carat symbol (‘^’) and specifies the bit position to access. For example:

  sbit OV = 0xD0^2;

  sbit CY = 0xD0^7;

  sbit EA = 0xA8^7;

  Variant 3

  sbit NAME = sbit-address;

  A CHARacter constant (sbit-address) specifies the address of the sbit. It must be a VALUE from 0x80-0xFF. For example:

  sbit OV = 0xD2;

  sbit CY = 0xD7;

  sbit EA = 0xAF;

  Note

  Not all SFRs are bit-addressable. ONLY those SFRs whose address is evenly divisible by 8 are bit-addressable. The lower nibble of the SFR‘s address must be 0 or 8. For example, SFRs at 0xA8 and 0xD0 are bit-addressable, whereas SFRs at 0xC7 and 0xEB are not. To calculate an SFR bit address, add the bit position to the SFR byte address. So, to access bit 6 in the SFR at 0xC8, the SFR bit address would be 0xCE (0xC8 + 6)。

  Special function bits represent an independent declaration class that may not be interchangeable with other bit declarations or bit fields.

  The sbit data type declaration may be used to access individual bits of variables declared with the bdata MEMORY type specifier. REFER to Bit-addressable Objects for more information.

  sbit variables may not be declared inside a function. They must be declared outside of the function body.

  COPYRIGHT (c) Keil SOFTWARE, Inc. and Keil Elektronik GmbH. All rights reserved.



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有