sfr
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:
Code:
sfr P0 = 0x80; /*Port-0, address 80h */
sfr P1 = 0x90; /*Port-0, address 90h */
sfr P2 = 0xA0; /*Port-0, address A0h */
sfr P3 = 0xB0; /*Port-0, address B0h */
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.) This constant expression must lie in the SFR address range (0x80 to 0xFF).
sbit
With typical 8051 applications, it is often necessary to access individual bits with 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:
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. 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).
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: