Keil™, An ARM® Company

Technical Support

C51: ACCESSING SPECIAL FUNCTION REGISTERS IN C


Information in this article applies to:

  • C51 All Versions

QUESTION

How do I access the 8051's Special Function Registers (SFRs) in my C programs?

ANSWER

The C51 Compiler offers 3 extensions to the C programming language that allow you to access the SFRs of the 8051. These keywords are:

  • sbit: For accessing bit-addressable SFRs,
  • sfr: For accessing byte-addressable SFRs,
  • sfr16: For accessing 16-bit SFRs.

You use these keywords to define the name and address for an SFR. The name may be any valid C variable name. The address must be the address that is associated with the desired SFR. Refer to your databook for the SFR addresses.

The following example defines the SFR for Port 1. P1 is used as the name and 0x90 is the address.

sfr P1 = 0x90;

The following example defines the SFR bit for Port 1.1. P1_1 is the name and 0x91 is the address of P1.1.

sbit P1_1 = 0x91;

The following example defines the 16-bit SFR for Timer 2. T2 is the name and 0xCC - 0xCD is the address.

sfr16 T2 = 0xCC;

MORE INFORMATION

SEE ALSO

Last Reviewed: Sunday, March 12, 2006


Did this article provide the answer you needed?
 
Yes
No
Not Sure