Extended Data Types of c51

Source: Internet
Author: User
Tags scalar

Why can we identify sbit, SFR... type in Keil:

 

1. Bit and sbit are both variable types extended by C51.

Bit is similar to int char, except char = 8 bits and bit = 1 bits. All are variables, and the compiler allocates addresses during compilation. This address is random unless specified. This address is the entire addressable space, with Ram + flash + extended space. Bit has only two values: 0 and 1, which means a bit like bool in VC in windows.

Sbit is a single bit corresponding to the addressing space. The addressing area is 20 h ~ 2fh. Once SBI xxx = Rege ^ 6 is used, the address is determined for this sbit volume. Sbit is mostly used in registers to facilitate operations on a register.

2. Bit scalar
Bit scalar is an extended data type of the 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, which is either 0 or 1, similar to true or false in some advanced languages of the boolean type.

3. SFr special function register
SFR is also an extended data type, with a memory unit and a value range of 0 ~ 255. It can be used to access all the special function registers in 51 single-chip microcomputer. For example, if the SFR p1 = 0x90 clause is used to set P1 as the internal register of the P1 port, in the subsequent statements, we use statements such as P1 = 255 (set the height of all pins on port P1) to operate special function registers.

SFr p1 = 0x90; // defines the P1 I/O port. Its address is 90 H.
The key to SFR is a name to be defined, which can be selected at will. However, to comply with the naming rules of identifiers, it is recommended that the name have a certain meaning, for example, the P1 port can be named by P1.ProgramBetter reading. the equal sign must be followed by a constant, and an expression with an operator is not allowed. The constant must be within the address range of the special function register (80 h-ffh). For details, see the relevant table in the appendix.

SFR is a special 8-bit function register while sfr16 is used to define 16-bit special function register,

for example, the T2 timer of 8052 can be defined as:
sfr16 t2 = 0xcc; // here, the 8052 Timer 2 is defined, and the address is t2l = CCH, t2h = CDH
when sfr16 is used to define a 16-bit special function register, the equal sign is followed by its low-level address, and the high-level address must be located above the low-level address.
. note that the timer 0 and 1 cannot be defined.
sbit can define addressable objects. for example, you can access one of the special function registers. in fact, This is often the case for applications
if you want to access the 2nd pin p1.1. we can define it as follows:
(1) sbit bit variable name = bit address
sbit p1_1 = ox91;
in this way, the absolute bit address is assigned to the bit variable. like SFR, the bitwise address of sbit must be in the range of 80h-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 locate the location of the variable name. This method can be used when the addressing bit is in a special function register
(3) sbit bit variable name = byte address ^ bit location
sbit p1_1 = 0x90 ^ 1;
This method is actually the same as 2, only the addresses of special function registers are expressed directly with constants. there is a bdata storage type in the memory type of the C51
memory, which is a bit addressable data storage located in the bit addressing area of a single
machine, you can define the data that requires location recording as bdata, for example,
unsigned char bdata Ib; // defines the ucsigned char type variable IB in the bitable address area
int bdata AB [2]; // defines the array AB [2] In the addressable area. these are also known as 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 bit position after the operator "^" depends on the specified base address type, char0-7, int0-15, long0-31.

SFR is used as a keyword in the Standard C language, but Keil provides a new
Keyword, its usage is:
Sfrt variable name = address value.
2) The p1.0 pin is indicated by the p1_0 symbol.
In C, if p1.0 is directly written, the C compiler cannot recognize it, and p1.0 is not a valid C
Language variable name, so you have to give it another name. Here the name is p1_0, but is p1_0 p1.0
What about it? You think so, C compiler does not think so, so you must establish a connection for them. Here Keil C is used
It is defined by the keyword sbit. There are three ways to use sbit:
Method 1: sbit bit variable name = address value
Method 2: sbit bit variable name = SFr name ^ variable bit address value
Method 3: sbit bit variable name = SFr address value ^ variable bit address value
For example, you can use the following three methods to define OV in psw:
Sbit ov = 0xd2 (1) Description: 0xd2 is the ov bit address value.
Sbit ov = psw ^ 2 (2) Description: psw must be defined with Sfr.
Sbit ov = 0xd0 ^ 2 (3) Description: 0xd0 is the psw address value.
Therefore, SFR p1_0 = p1 ^ 0 is used here. It is defined to express the p1.0 pin with the symbol p1_0. If you want to, you can also
P10 class names, as long as the following program also changes.

4. sfr16 16-bit special function register
Sfr16 occupies two memory units with a value ranging from 0 ~ 65535. Sfr16 and SFR are also used to operate special function registers. The difference is that sfr16 is used to operate registers that occupy two bytes, with good timer T0 and T1.

5. sbit record-location
sbit is an extended data type in C51, which can be used to access the addressable bit in the ram inside the chip or the addressable bit in the special function register. As we previously defined
SFr p1 = 0x90; // because the registers of port P1 are bit-addressable, so we can define
sbit p1_1 = p1 ^ 1; // p1_1 as the p1.1 pin in P1
// we can also use the p1.1 address to write, for example, sbit p1_1 = 0x91;
in this way, we can use p1_1 in future program statements to read and write p1.1 pins. Usually these can directly use the pre-processing files provided by the system. The simple names of special function registers have been defined in the files. Direct reference can save a little time and I have always used them. Of course, you can also write your own definition file, using what you think is a memorable name.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.