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.
2. Bit scalar
3. sfr Special function register
sfr P1 = 0x90; //Define P1 I/O port, its address is 90H.
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
5. sbit addressable bit
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.
Previous article:MCU common I/O analog serial port
Next article:How to understand and distinguish between single chip microcomputer and PLC
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Keysight Technologies Helps Samsung Electronics Successfully Validate FiRa® 2.0 Safe Distance Measurement Test Case
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Today's live broadcast: Battery management chip solution design and precautions
- [Repost] 7 PCB layout principles, good PCB design depends on it
- How to choose electromagnetic relay
- It’s terrible. Two manufacturers in the PCB industry are engaged in a price war. Did you get any benefits yesterday?
- 【McQueen Trial】Python Programming (4)
- Partial Discharge in Switching Power Supplies
- Crazy Shell AI open source drone interruption (key detection)
- How does GaN meet the challenges brought by 5G?
- PCB
- (Help) [New version of Zhongke Bluexun AB32VG1 RISC-V development board] - 6: "COM is closed"