Keil Logo

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 thare 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.

Related Knowledgebase Articles

  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.