# c51 - Special Function Registers(sfr and sbit) 1. sfr(Special Function Register特殊功能暫存器) 2. sbit(特殊功能暫存器位) - sfr. sbit 與 int. char 定義不同,式座位特殊功能的暫存器所引用,或許可以叫做別名 ## [c51 官方解釋](http://www.keil.com/support/man/docs/c51/c51_le_sbit.htm) 1. The sbit type defines a bit within a special function register (SFR). sbit 定義了 SFR 的位元 ```c= sbit name = sfr-name ^ bit-position; sbit name = sfr-address ^ bit-position; sbit name = sbit-address; /* 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. */ ``` 2. 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. 宣告 SFR bit 在地址 0xAF,在 8051 這個 sbit enable all bit in the interrupt enable register ```c= 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. */ ``` :::info 1. sbit 的儲存方式為 LSB 和 int. long 的儲存方式不同。 Storage of objects accessed using sbit is assumed to be little endian (LSB first). This is the storage format of the sfr16 type but it is opposite to the storage of int and long data types. Care must be taken when using sbit to access bits within standard data types. 3. sfr 的範圍在 0x80~0xFF ::: ## 範例 1. 變體 1 ```c= sbit name = sfr-name ^ bit-position; sfr PSW = 0xD0; sfr IE = 0xA8; sbit OV = PSW^2; sbit CY = PSW^7; sbit EA = IE^7; ``` 2. 變體 2 ```c= sbit name = sfr-address ^ bit-position; sbit OV = 0xD0^2; sbit CY = 0xD0^7; sbit EA = 0xA8^7; ``` 3. 變體 3 ```c= sbit name = sbit-address; sbit OV = 0xD2; sbit CY = 0xD7; sbit EA = 0xAF; ```