In the previous article about 51 MCU, we mainly talked about the application of the output function of MCU IO port, such as LED control, digital tube control, etc. In this article, we will explain how to use the MCU IO port to read the state of the key and control the LED according to the state of the key. For the introduction of the key input function of 51 MCU, please refer to the article "51 MCU (Twenty-four) - Working Principle of Independent Key"
In this article, the code of the example is shown below.
#define KEY P2 //Define key input port
#define LED P0 //Define LED output port
/************************************************************************/
/* Main function */
/************************************************************************/
void main (void)
{
KEY=0xff; //Set the key input port level to high
for(;;) //main loop
{
LED=KEY;
}
}
The code is very simple. The main code is only one line LED=KEY in the for loop. The purpose of this line of code is to assign the value of KEY to the LED. From the macro definition of the program, we can know that KEY is P2, which is the register of the 8 IO ports connected to the independent button. LED is P0, which is the register of the 8 IO ports connected to the LED. When an independent button is pressed, the level state of the IO port corresponding to P2 is pulled down, and the input is 0. When the button is released, the input is 1. In the for loop, the value of P2 is assigned to P0. When the button is pressed, the output of the IO port that controls the LED is 0, and the corresponding LED lights up. When the button is released, the output of the IO port that controls the LED is 1, and the corresponding LED goes out. Since the assignment code is in the for loop, the state of the LED will be updated in real time.
Previous article:51 single chip microcomputer (part 26) - independent button digital tube display
Next article:51 Single Chip Microcomputer (Twenty-four) - Working Principle of Independent Keys
- 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
- About the reset function of capacitor in microcontroller
- I recently used a chip that is compatible with both RS232 and RS485, but I don't know which pins are A and B for RS485.
- [ATmega4809 Curiosity Nano Review] Configuring GPIO using MCC
- Today at 10:30 AM Microchip Live | Manufacturing Logistics Challenges of Key Security Provisioning: Advantages of Discrete Secure Components
- Multisim circuit simulation does not oscillate?
- In order to solve network congestion, what black technologies does Wi-Fi 6 use?
- [Atria AT32WB415 Review] 1. Unboxing, installation and use of AT32 IDE development environment (especially error resolution)
- Please ask the experts about STM32, FSMC control LCD problem?
- Made a JLINK OB and shared the production process and experience
- Creating an embedded project using Rust