Special function register SFR declaration
If we want to use the IO port of the microcontroller, we must first define its address. We can find the address corresponding to the IO port from the chip data (you can also search directly online)
Microcontroller C language SFR statement:
sfr P0 = 0x80;
sfr P1 = 0x90;
sfr P2 = 0xA0;
sfr P3 = 0xB0; //Define P0~P3
After defining it, by controlling P0~P1, we can control their corresponding 8 IO ports at the same time. Of course, this time we only need to light up one or two LEDs and only need to use one or two IO ports, so we can use the following The method defines an IO port separately.
sbit LED0 = P1 ^ 0;
sbit LED1 = P1 ^ 1;
sbit LED2 = P1 ^ 2;
sbit LED3 = P1^3;
sbit LED4 = P1 ^ 4;
sbit LED5 = P1 ^ 5;
sbit LED6 = P1 ^ 6;
sbit LED7 = P1 ^ 7; // Define the 8 IO ports of P1 respectively
Note: The IO port must be defined in capital letters.
head File
Earlier we used sfr to define the IO port. It would be very troublesome if we had to enter sfr one by one every time to define it. At this time, we can use the header file method instead of the previous method. The header file of the 52 microcontroller C language is This header file contains most of the definitions, which is convenient and fast. So why do we still need to talk about the sfr statement? In fact, it is to better understand the core content of the header file. Code reference Assigning a value of 0 to led0 allows its corresponding IO port to send out a low-level signal, thereby lighting up the LED. #include //sfr P1 = 0x90; //Define P1 register address sbit led0 = P1^0;//led0 1st led sbit led7 = P1^7;//led7 8th led void main() { // led0 = 0; //Low level lights up led0 // while(1);//Stop the program here (infinite loop) while(1) { led0 = 0; led7 = 0; } } Below is the final result picture. In the black frame are the first and eighth LEDs we lit. Reference content link: https://www.bilibili.com/video/BV1Kt411Q7aV?p=4.
Previous article:Four methods of 52 microcontroller to realize running water lamp
Next article:Microcontroller AT89C51--3. Light up the LED light
Recommended ReadingLatest update time:2024-11-16 15:56
- 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
- Circuit Analysis
- Tms320VC5502 and isp1581 high-speed data acquisition solution
- 2019FPGA Employment Competition and Guidance
- What kind of activities do you hope the RF/Radio Frequency section will carry out? Please leave a message in the thread.
- Oscilloscope usage tips you don't know
- Free Trial | Keysight Accelerates Signal Integrity and Power Integrity Testing
- Learning PLC technology is super easy
- Advantages of modules
- EEWORLD University Hall----Live Replay: What to listen to when stuck in traffic? New generation of in-car audio systems and software-defined cars
- Beautiful Pico Quad Keyboard