sbit and sfr in single chip c language

Publisher:asdfrewqppLatest update time:2016-02-25 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
1. Both bit and sbit are variable types extended by C51.

 

       

Bit is similar to int char, except that char = 8 bits and bit = 1 bit. They are all variables, and the compiler assigns addresses during the compilation process. Unless you specify, the address is random. This address is the entire addressable space, RAM+FLASH+extension space. Bit has only two values ​​0 and 1, and its meaning is a bit like BOOL in VC under Windows.

       sbit is a bit in the addressable space, which is from 20H to 2FH. Once the definition sbi xxx = REGE^6 is used, the sbit value determines the address. Most sbits are used in registers to facilitate operations on a certain bit of the register.


2. Bit scalar
       Bit scalar is an extended data type of C51 compiler. It can be used to define a bit scalar, but it cannot define a bit pointer or a bit array. Its value is a binary bit, either 0 or 1, similar to True and False in the Boolean type in some high-level languages.

3. sfr Special function register
      sfr is also an extended data type, which uses a memory unit and has a value range of 0 to 255. It can be used to access all special function registers inside the 51 microcontroller. For example, sfr P1 = 0x90 is used to define P1 as the register of the P1 port on the chip. In the following statements, we use statements such as P1 = 255 (set all pins of the P1 port to a high level) to operate the special function register.

sfr P1 = 0x90; //Define P1 I/O port, its address is 90H.
       The sfr keyword is followed by a name to be defined, which can be selected arbitrarily, but must comply with the naming rules of identifiers. It is best if the name has a certain meaning. For example, P1 port can be named P1, so that the program will become much easier to read. The equal sign must be followed by a constant, and expressions with operators are not allowed. Moreover, the constant must be within the address range of the special function register (80H-FFH). For details, please refer to the relevant table in the appendix.

         sfr is used to define an 8-bit special function register and sfr16 is used to define a 16-bit special function register.

For example, the T2 timer of 8052 can be defined as:
sfr16 T2 = 0xCC; //Here, define 8052 timer 2, address T2L = CCH, T2H = CDH.
When using sfr16 to define a 16-bit special function register, the equal sign is followed by its low-order address, and the high-order address must be above the physical
low-order address. Note that it cannot be used to define timers 0 and 1.
sbit can define bit-addressable objects, such as accessing a certain bit in a special function register. In fact, this application is often used. For example,
if you want to access the second pin P1.1 in port P1, you can define it as follows:
(1) sbit bit variable name = bit address
sbit P1_1 = Ox91;
This assigns the absolute address of the bit to the bit variable. Like sfr, the bit address of sbit must be between 80H and FFH.
(2) sbit bit variable name = special function register name ^ bit position
sft P1 = 0x90;
sbit P1_1 = P1 ^ 1; //First define a special function register name and then specify the position of the bit variable name.
This method can be used when the addressable bit is located in the special function register
(3) sbit bit variable name = byte address ^ bit position
sbit P1_1 = 0x90 ^ 1;
This method is actually the same as 2, except that the address of the special function register is directly represented by a constant. In the C51
memory type, there is a bdata memory type, which refers to a bit-addressable data memory located
in the bit-addressable area of ​​the microcontroller. The data that requires bit-addressable data can be defined as bdata, such as:
unsigned char bdata ib; //Define a variable ib of type ucsigned char in the bit-addressable area
int bdata ab[2]; //Define an array ab[2] in the bit-addressable area. These are also called addressable bit objects
sbit ib7=ib^7 //Use the keyword sbit to define a bit variable to independently access one of the addressable bit objects
sbit ab12=ab[1]^12;
The maximum value of the bit position after the operator "^" depends on the specified base address type, char0-7, int0-15, long0-31.

sfr is not a standard C language keyword, but Keil provides a new
keyword for direct access to SFR in 80C51. Its usage is:
sfrt variable name = address value.
2) The symbol P1_0 represents the P1.0 pin.
In C language, if you write P1.0 directly, the C compiler cannot recognize it, and P1.0 is not a legal C
language variable name, so you have to give it another name. Here it is named P1_0, but is P1_0 the same as P1.0
? You think so, but the C compiler doesn't think so, so they must be connected. Here, the Keil C
keyword sbit is used to define it. There are three ways to use sbit:
The first method: sbit bit variable name = address value
The second method: sbit bit variable name = SFR name ^ variable bit address value
The third method: sbit bit variable name = SFR address value ^ variable bit address value
For example, to define OV in PSW, you can use the following three methods:
sbit OV=0xd2 (1) Description: 0xd2 is the bit address value of OV
sbit OV=PSW^2 (2) Description: PSW must be defined with sfr first
sbit OV=0xD0^2 (3) Description: 0xD0 is the address value of PSW
Therefore, sfr P1_0=P1^0 is used here; that is, the symbol P1_0 is used to represent the P1.0 pin. If you like, you can also
start with P10 The name of a class can be changed in the following program.

*For the special function register table of AT89C51, please see Appendix 2

4. sfr16 16-bit special function register
         sfr16 occupies two memory cells, and its value range is 0 to 65535. sfr16 is used to operate special function registers like sfr, but it is used to operate registers that occupy two bytes, such as timers T0 and T1.

5. sbit addressable bit
        sbit is an extended data type in C51, which can be used to access the addressable bits in the RAM inside the chip or the addressable bits in the special function register. As we defined
sfr P1 = 0x90; //Because the register of the P1 port is bit addressable, we can define
sbit P1_1 = P1^1; //P1_1 is the P1.1 pin in P1
//Similarly, we can use the address of P1.1 to write, such as sbit P1_1 = 0x91;
In this way, we can use P1_1 to read and write the P1.1 pin in future program statements. Usually, these can be directly used in the preprocessing file provided by the system, which has defined the simple names of each special function register. Direct reference can save a little time. I have always used it. Of course, you can also write your own definition file with a name that you think is easy to remember.


Keywords:MCU Reference address:sbit and sfr in single chip c language

Previous article:MCU common I/O analog serial port
Next article:How to understand and distinguish between single chip microcomputer and PLC

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号