SFR is used to assign a special function register of a microcontroller to a variable, so that the variable can be used to refer to the register in the subsequent program
. SBI is similar to SFR, except that SBI is a bit operation and is used to assign a specific bit in an SFR to a variable, so that the subsequent program can use the variable to clear or set the bit.
Next, we take the STC series 51 microcontroller as an example to briefly understand the special function register layout of the microcontroller, as follows:
In the MCS-51 microcontroller, except for the program counter PC and 4 groups of working registers, all other registers are special function registers (SPRs), which are scattered in the upper 128 bytes of the on-chip RAM area, with an address range of 80H~0FFH. There are 11 registers in the SFR that have bit addressing capabilities, and their byte addresses can be divided by 8, that is, the byte address ends with 8 or 0.
In order to directly access these SPRs, FranklinC51 provides an independent definition method, which is incompatible with the standard C language and is only suitable for C language programming of the MCS-51 series of microcontrollers. The general syntax format of the special function register C51 definition is as follows:
sfrsfr-name=intconstant;
"sfr" is the keyword of the definition statement, which must be followed by a special function register name that actually exists in the MSC-51 microcontroller. The "=" must be followed by an integer constant, and expressions with operators are not allowed. It is the byte address of the special function register "sfr-name". The value range of this constant must be within the SFR address range, located between 0x80 and 0xFF.
For example:
sfrSCON = 0x98; /* Serial port control register at address 98H*/
sfrTMOD = 0x89; /* Timer/Counter mode control register at address 89H*/
The number and type of special function registers in the MCS-51 series of microcontrollers vary, so it is recommended to put all special "sfr" definitions into a header file that should include the SFR definitions for the MCS-51 microcontroller series models. The "reg51.h" header file of the C51 compiler is such a file.
In the new MCS-51 series products, SFRs are often functionally combined into 16-bit values. When the high byte address of the SFR is located directly after the low byte, the value of the 16-bit SFR can be directly accessed. This is the case for Timer/Counter 2 of the 52 sub-series. In order to effectively access this type of SFR, the keyword "sfr16" can be used to define it. The syntax format of its definition statement is the same as that of 8-bit SFR, except that the address after "=" must use the low-byte address of the SFR where 16 is located, that is, the low-byte address is used as the definition address of "sfr16". For example:
sfr16T2=0xCC/*Timer/Counter 2; T2 low 8-bit address is 0CCH, T2 high 8-bit address is 0CDH*/
This definition is applicable to all new 16-bit SFRs, but cannot be used for timer/counter 0 and 1.
For the bits in the bit-addressed SFR, the expansion function of C51 supports the definition of special bits, which is not compatible with standard C like SFR, and uses "sbit" to define the bit addressing unit.
There are three general syntax formats for definition statements:
The first format: sbitbit-name=sfr-name^intconstant;
"sbit" is the keyword of the definition statement, followed by an addressing bit symbol name (the bit symbol name must be the bit name specified in the MCS-51 microcontroller), and the bit number in "sfr=name" after "=" must be a number in the range of 0~7. For example:
sfrPSW=0Xd0; /*Define the PSW register address as D0H*/
sfrOV=PSW^2; /*Define the OV bit as PSW.2, the address as D2H/*
sfrCY=PSW^7; /*Define the CY bit as PSW.7, the address as D7H^*/
The second format: sbitbit-name=intconstant^intconstant;
The intconstant after "=" is the byte address of the special function register where the addressing address is located, and the intconstant after the "^" symbol is the bit number of the addressing bit in the special function register. For example:
sbitOV=0Xd0^2; /*Define the OV bit address as the 2nd bit in the D0H byte*/
sbitCY=0XD0^7; /*Define the CY bit address as the 7th bit in the D0H byte*/
The third format: sbitbit-name=intconstant;
The intconstant after "=" is the absolute address of the addressing bit. For example:
sbitOV=0XD2; /*Define the OV bit address as D2H*/
sbitOY=0XD7; /*Define the CY bit address as D7H*/
The special function bit represents an independent definition class and cannot be interchanged with other bit definitions and bit fields.
After understanding the definition of special function registers, some people may have questions:
We use sfrP0=0×80 to represent P0, and sfrSP=0×81 to represent SP, which is unambiguous. But there is a question: if sbitP0_1=0×81 is used to represent the first bit of port P0, then what should I do if I want to represent the 0th bit of the SP register? If it is also defined as sbitSP_0=0×81, then there will be obvious ambiguity, and the compiler cannot understand it. In fact, this problem does not exist. As can be seen from Figure 1, SPR can be divided into two areas: bit-addressable area and non-bit-addressable area. The register address of the bit-addressable area can be divided by 8, while the register address of the non-bit-addressable area does not meet this requirement. Therefore, sbitSP_0=0×81 in the example is invalid for the SP register and should be written as sfrSP=0x81.
Previous article:51 MCU Learning Notes (V)_C51 Implementation of MCU Serial Communication Simulation
Next article:MCS51 MCU timer/counter concept very good register relationship diagram
- 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
- 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)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- CircuitPython Digital Clock
- (Take STM8/STM32 as an example) What is the significance of hardware i2c spi?
- Urgent help! Calculation of push-pull switching power supply transformer
- Advantages of new lora wireless module E30-M series and E32-M series
- Multimeter and infrared thermometer are waiting for you to disassemble! ---Start testing
- What did you learn from the Industrial Expo Electronics Exhibition?
- 10 Basic Rules for Power Supply PCB Layout
- What does transparent transmission in communication networks mean?
- What is the function of the INCREMENT COMPONENT PART NUMBER button in PROTEL99?
- MicroPython Retro Gaming on Wio