We must be patient with the unique program syntax of the 51 microcontroller and the basic operating process of the Keil software.
We mainly use C language to program microcontrollers, and some microcontrollers have a few very special and unique programming sentences. The 51 microcontroller has them. I will introduce two of them first.
The first sentence is: sfr P0 = 0x80;
The keyword sfr is unique to the 51 microcontroller. Its function is to define a special function register of the microcontroller. There are many small modules inside the 51 microcontroller. Each module lives in a room with a unique room number. At the same time, each module has 8 control switches. P0 is a functional module that lives in the room 0x80. We set the 8 switches of this module inside P0 to make the 8 IO ports of P0 of the microcontroller output high level or low level. There are many registers inside the 51 microcontroller. If we want to use them, we must make an sfr statement in advance. However, Keil software has pre-written all these statements and saved them in a special file. If we want to use them, we only need to add a line of #include at the beginning of the file. This will be explained in detail later.
The second sentence is: sbit LED = P0^0;
This sbit is the definition of one of the eight switches in the SFR just mentioned. After passing the second sentence above, in the future, as long as we write LED in the program, it will represent the P0.0 port (the "^" symbol is above the numeric key 6). Note that this P must be capitalized, which means that we P0.0 was given a more vivid name called LED.
After understanding these two sentences, let's take a brief look at the special function registers of the microcontroller. Please note that each type of microcontroller will be equipped with a data sheet (Datasheet) written by the manufacturer, so let’s take a look at the STC89C52 data sheet. From pages 21 to 24, they are all introductions and addresses of special function registers. Mapping list. Before we use this register, we must analyze the address of this register. Are there too many colorful things that you can’t remember? This is irrelevant and you don’t need to remember it. Just understand it. I will explain most of them to you step by step later. When you need to use some of them, you can come and check them yourself. A manual is enough. Doing skills is not just for exams. You can read the manual at any time to find the materials you need.
As shown in Figure 2-7, it is an interception of the table at the bottom of page 22 of the manual.
Figure 2-7 IO port special function register
Let's take a look at this table. Among them, the P4 port STC89C52 expands the standard 51. Let's ignore it first and only look at the previous four P0, P1, P2, and P3. Each P port itself has 8 control ports. We can refer to our development board schematic diagram or Figure 2-1 to see that our microcontroller has a total of 32 IO ports (IO, Input and Output, respectively, input and output).
Among them, the address of the P0 port address is 0x80. There are a total of 8 IO port control bits from 7 to 0, followed by a Reset Value. This is very important. It is a parameter that we must look at when looking at the register. There are 8 controls. The bit reset values are all 1.
This is to tell us that every time the microcontroller is powered on and reset, the value of all pins is 1 by default, which is high level. We must also fully consider this issue when designing the circuit.
So for the two sentences above, when we write sfr, we must write them according to this address (Add) in the manual. When we write sbit, we can directly take out one of the bits in a byte. When we program, there are also ready-made header files with register addresses written in them. We can just include the header file directly, and there is no need to write them one by one.
Previous article:How does Keil C51 support double data pointers and what are the special requirements?
Next article:Failure analysis of 51 microcontroller programming chip failure to work
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- The applications of TI audio chips used in smart speakers are revealed.
- ART-Pi evaluation application RL-TCPnet realizes Ethernet transceiver function and DTU
- Is it just simple impedance control? Answer with real examples!
- Common integrator applications: signal processing, sensor conditioning, signal generation, filtering
- What are the advantages of electronic load switches over MOS or transistors as output control? Now many domestic semiconductor...
- Lighter igniter electric shock/ignition principle
- This is the second time I haven't seen any replies in the past few days. The pattern is: there are replies on the 20th floor, but no more on the 21st floor; there are replies on the 40th floor, but no more on the 41st floor...
- Find TI, NXP, ALTERA, XILINX, ST, Microchip factory stock chips
- Record the use of TM1668 driver
- Newbie help, ESP8266 program problem