bit 和 sbit 的区别?

Difference between bit and sbit?

Keil C51中8051单片机的bitsbit关键字有什么区别?

什么时候应该使用sbit,什么时候使用bit

一些例子会很有帮助。

检查这个 forum:

The main difference between the bit and sbit is that you can declare sbit a varible in a unit in such way that it points to a specific bit in the SFR register. In the main program you need to specify to which register this sbit points to.

dim Abit as sbit sfr external ' Abit is precisely defined in some external file, for example 

in the main program unit
...
implements
....
end.

The mikroBasic PRO for PIC compiler provides a bit data type that may be used for variable declarations. It can not be used for argument lists, and function-return values, there are no pointers to bit variables, and an array of type bit is not valid.

dim bf as bit  ' bit variable

sbit is not a new variable and does not take extra memory space, while with a bit different, will the new variable, which further defines and takes additional space in memory.

同时检查参考资料(由 nos 添加到评论中):

这应该对您有所帮助:

BIT

C51 provides you with a bit data type which may be used for variable declarations, argument lists, and function return values. A bit variable is declared just as other C data types are declared. For example:

static bit done_flag = 0; /* bit variable */

bit testfunc (    /* bit function */
  bit flag1,  /* bit arguments */
  bit flag2)
{
.
.
.
return (0);   /* bit return value */
}

All bit variables are stored in a bit segment located in the internal memory area of the 8051. Because this area is only 16 bytes long, a maximum of 128 bit variables may be declared within any one scope.

Memory types may be included in the declaration of a bit variable. However, because bit variables are stored in the internal data area of the 8051, the data and idata memory types only may be included in the declaration. Any other memory types are invalid.

The following restrictions apply to bit variables and bit declarations:

  • Functions which use disabled interrupts (#pragma disable) and functions that are declared using an explicit register bank (using n) cannot return a bit value. The C51 compiler generates an error message for functions of this type that attempt to return a bit type.

  • A bit cannot be declared as a pointer. For example:

    bit *ptr

  • An array of type bit is invalid. For example:

    bit ware [5]

SBIT

With typical 8051 applications, it is often necessary to access individual bits within an SFR. The C51 compiler makes this possible with the sbit data type. The sbit data type allows you to access bit-addressable SFRs. 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.

NOTE:

Not all SFRs are bit-addressable. Only those SFRs whose address is evenly divisible by 8 are bit-addressable. These SFR’s lower nibble will be either 0 or 8; for example, SFRs at 0xA8 and 0xD0 are bit-addressable, whereas SFRs at 0xC7 and 0xEB are not. SFR bit addresses are easy to calculate. Add the bit position to the SFR byte address to get the SFR bit address. So, to access bit 6 in the SFR at 0xC8, the SFR bit address would be 0xCE (0xC8 + 6).

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:

 sfr_name ^ int_constant

This variant uses a previously-declared sfr (sfr_name) as the base address for the sbit. The address of the existing SFR must be evenly divisible by 8. The expression following the carat symbol (^) specifies the position of the bit to access with this declaration. The bit position must be a number in the range 0 to 7. For example:

sfr PSW = 0xD0;
sfr IE = 0xA8;

sbit OV = PSW ^ 2;
sbit CY = PSW ^ 7;

sbit EA = IE ^ 7;

Variant 2:

int_constant ^ int_constant

This variant uses an integer constant as the base address for the sbit. The base address value must be evenly divisible by 8. The expression following the carat symbol (^) specifies the position of the bit to access with this declaration. The bit position must be a number in the range 0 to 7. For example:

sbit OV = 0xD0 ^ 2;
sbit CY = 0xD0 ^ 7;

sbit EA = 0xA8 ^ 7;

Variant 3:

int_constant

This variant uses an absolute bit address for the sbit. For example:

sbit OV = 0xD2;
sbit CY = 0xD7;

sbit EA = 0xAF;

NOTES :

Special function bits represent an independent declaration class that may not be interchanged 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

来源: BIT and SBIT

sBIT 是 8051 微控制器中使用的一种特殊类型的寄存器,用于访问用 bdata 声明的各个位,而 Bit 用于定义单个位变量。