Difference between bit and sbit?

msc picture msc · Feb 5, 2016 · Viewed 11.4k times · Source

What is the difference between the bit and sbit keywords in Keil C51 for the 8051 Microcontroller?

When should sbit be used and when bit?

Some examples would be very helpful.

Answer

lsunny picture lsunny · Feb 5, 2016

This should help you :

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

Source : BIT and SBIT