The I/0 port of a typical microcontroller is a quasi-bidirectional port, which can be used as both an output port and an input port. In specific operations, we write data out of the I/0 port to use it as an output port; if we directly call the current state of the I/0 port in the program, assign values to our defined variables, or use it for operations such as logical judgment and branch control, we use it as an input port.
The figure below shows the circuit that uses the P2.0 port to read the button control.
As can be seen from the figure below, in this circuit, port P2.0 is an input application. When the button is not pressed, +5V is connected to P2.0 through the pull-up resistor R1, and the voltage of port P2.0 is +5V, which is logic "1". If the button is pressed, P2.0 is connected to the power ground through the button, and the voltage of port P2.0 is 0V, which is logic "0".
To sum up, when no key is pressed, the state of P2.0 is "1"; when a key is pressed, the state of P2.0 is "0".
If there is no resistor R1, when the button is pressed, the +5V power supply will be short-circuited to the ground through the button, which may seriously damage the power supply. This is absolutely not allowed.
Assume that the microcontroller is connected to the circuits in the above and below figures at the same time, use P2.0 as the input port, read the key: use P3.4 to control the buzzer. Write a program that makes the buzzer sound when the key is pressed and stops when the key is not pressed. The program is as follows:
#include
sbitBuzzer=P3^4;sbitKey=P2^0;//[Note 1]
//Main program voiDMAin(void){for(;;){if(Key==0){//【Note 2】
Buzzer=0; //The button is pressed and the buzzer sounds}
else{Buzzer=1;//The button is not pressed, the buzzer stops beeping}
}
【Note 1】:The bit variable Key is defined as the P2.0 port of the microcontroller. The subsequent operations on the variable Key are the operations on the P2.0 port.
【Note 2】: Conditional statement, directly use the state of Key (that is, the state of P2.0 port) to control the operation of the program. If the key is pressed, the state of P2.0 is 0, Key=0 is true, the program executes Buzzer=0, and the buzzer sounds; if the key is not pressed, the state of P2.0 is 1, Key=0 is false, the program executes Buzzer=1, and the buzzer stops sounding.
Previous article:Application Guide for USB Reader/Writer with RS232 Interface
Next article:Three-way segment switch based on single chip microcomputer design
Recommended ReadingLatest update time:2024-11-16 16:48
- Popular Resources
- Popular amplifiers
- Wireless Sensor Network Technology and Applications (Edited by Mou Si, Yin Hong, and Su Xing)
- Modern Electronic Technology Training Course (Edited by Yao Youfeng)
- Modern arc welding power supply and its control
- Small AC Servo Motor Control Circuit Design (by Masaru Ishijima; translated by Xue Liang and Zhu Jianjun, by Masaru Ishijima, Xue Liang, and Zhu Jianjun)
- High signal-to-noise ratio MEMS microphone drives artificial intelligence interaction
- Advantages of using a differential-to-single-ended RF amplifier in a transmit signal chain design
- ON Semiconductor CEO Appears at Munich Electronica Show and Launches Treo Platform
- ON Semiconductor Launches Industry-Leading Analog and Mixed-Signal Platform
- Analog Devices ADAQ7767-1 μModule DAQ Solution for Rapid Development of Precision Data Acquisition Systems Now Available at Mouser
- Domestic high-precision, high-speed ADC chips are on the rise
- Microcontrollers that combine Hi-Fi, intelligence and USB multi-channel features – ushering in a new era of digital audio
- Using capacitive PGA, Naxin Micro launches high-precision multi-channel 24/16-bit Δ-Σ ADC
- Fully Differential Amplifier Provides High Voltage, Low Noise Signals for Precision Data Acquisition Signal Chain
- 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
- EEWORLD University Hall----Live Replay: Nexperia's ESD Solutions for Connected Vehicle Applications
- TDA2030 waveform output is wrong
- Schematic diagram - how does DZ1 protect the MOS tube?
- Some records of 4680 high nickel battery research
- 【GD32450I-EVAL】+11 function integration
- Full-bridge converter circuit
- What does the accuracy and resolution of a servo encoder affect?
- MSP430G2553 clock system configuration considerations
- 【Running posture training shoes】No.005-Posture recognition and step frequency calculation
- 【TMS320 frequency measurement】Newbie's second post! TMS320F28379D Launchpad environment configuration