Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

1.

bit

The bit type defines a single-bit variable. It is used as follows:

bit name <[>= value<]>;

Where
name is the name of the bit variable.
value is the value to assign to the bit.

The bit type may be used for variable declarations, argument lists, and function-return values.

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, 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:

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


 bit *ptr; /* invalid */

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

 bit ware [5]; /* invalid */

2. sbit

The sbit type defines a bit within a special function register (SFR). It is used in one of the following
ways:

sbit name = sfr-name ^ bit-position;


sbit name = sfr-address ^ bit-position;
sbit name = sbit-address;

Where
name is the name of the SFR bit.
sfr-name is the name of a previously-defined SFR.
bit-position is the position of the bit within the SFR.
sfr-address is the address of an SFR.
sbit-address is the address of the SFR bit.

With typical 8051 applications, it is often necessary to access individual bits within an SFR. The sbit
type provides access to bit-addressable SFRs and other bit-addressable objects. For example:

sbit EA = 0xAF;
This declaration defines EA as 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 a number 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 a number 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.

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

3. sfr

The sfr type defines a special function register (SFR). It is used as follows:

sfr name = address;

Where
name is the name of the SFR.
address is the address of the SFR.

SFRs are declared in the same fashion as other C variables. The only difference is that the 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 name may 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 devices support the SFR address range 0x80-0xFF. The NXP
80C51MX provides an additional extended SFR space with the address range 0x180-0x1FF.

Note

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

4. sfr16

The sfr16 type defines a 16-bit special function register (SFR). It is used as follows:

sfr16 name = address;

where
name is the name of the 16-bit SFR.
address is the address of the 16-bit SFR.
Some 8051 derivatives have 16-bit SFRs that are created using consecutive addresses in SFR memory
to specify 16-bit values. For example, the 8052 uses addresses 0xCC and 0xCD for the low and high
bytes of timer/counter 2 respectively. The Cx51 Compiler provides the sfr16 data type to access two
8-bit SFRs as a single 16-bit SFR.

Access to 16-bit SFRs using sfr16 is possible only when the low byte immediately precedes the high
byte (little endian) and when the low byte is written last. The low byte is used as the address in the
sfr16 declaration. For example:

sfr16 T2 = 0xCC; /* Timer 2: T2L 0CCh, T2H 0CDh */


sfr16 RCAP2 = 0xCA; /* RCAP2L 0CAh, RCAP2H 0CBh */

In this example, T2 and RCAP2 are declared as 16-bit special function registers.

The sfr16 declarations follow the same rules as outlined for sfr declarations. Any symbolic name can
be used in an sfr16 declaration. The address specification after the equal sign ('=') must be a numeric
constant. Expressions with operators are not allowed. The address must be the low byte of the SFR
low-byte, high-byte pair.

Note

 Most 16-bit SFRs are composed of two consecutive addresses that contain the MSB and LSB
(in either order) of the 16-bit register. Depending on the device, writing to the MSB or LSB
"latches" the 16-bit value into the register. There are four possible configurations for a 16-bit
SFR. They are:
 LSB Read First, LSB Written Last (sfr16 does this).

 MSB Read First, LSB Written Last.

 LSB Read First, MSB Written Last.

 MSB Read First, MSB Written Last.

The sfr16 keyword may be used to define some (but not all) 16-bit Special Function
Registers. The two SFRs accessed by sfr16 must be organized so that the low byte is stored in
memory first (or at the lower address). For example, if the LSB is at SFR address 0x9E and
the MSB is at SFR address 0x9F, sfr16 may be used.

When you write to an srf16, the code generated by the Keil Cx51 Compiler writes to the high
byte first and then the low byte. In many cases, this is not the desired order.

If the order in which the bytes are written is important (this is the case with MANY 8051
devices) you must use the sfr keyword to define and access the SFRs one byte at a time.
Then, you are assured of the order in which the SFRs are accessed.

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

You might also like