Using the I/0 port of the microcontroller to do a key input experiment

Publisher:peon1989Latest update time:2012-11-13 Keywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.

Keywords:MCU Reference address:Using the I/0 port of the microcontroller to do a key input experiment

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

Design of a two-color clock without hands based on single chip microcomputer
  This paper proposes a design scheme of a two-color clock without hands based on a single-chip microcomputer. The hardware of this scheme uses the AT89S51 single-chip microcomputer as the control core, uses the 74LS154 (4-16 line decoder) to expand the I/O port to control the display of the clock, and uses 12 red LED
[Microcontroller]
Design of a two-color clock without hands based on single chip microcomputer
STC89C52RC MCU Extra Chapter | 02 - Understanding Serial Communication, Baud Rate and Data Packets
1 Parallel and serial communication There are two types of machine communication: parallel communication and serial communication. Parallel communication: Parallel communication refers to the transmission of multiple bits of data through parallel lines at the same time. This greatly improves the data transmission sp
[Microcontroller]
STC89C52RC MCU Extra Chapter | 02 - Understanding Serial Communication, Baud Rate and Data Packets
What are the advantages of the PIC series microcontrollers?
I. Introduction According to statistics, the annual capacity of single-chip microcomputers in my country has reached 100-300 million pieces, and it is growing at a rate of about 16% per year, but my country's share of the world market is less than 1%. This shows that the application of single-chip microcomputers in my
[Microcontroller]
What are the advantages of the PIC series microcontrollers?
Austrian Microelectronics' latest design separates sensor from microcontroller
ams has announced the launch of an Intelligent Battery Sensor reference design for automotive applications based on ams’ highly accurate AS8515 data acquisition front end. The intelligent battery sensor (IBS) from ams is designed to monitor the state of charge (SOC) and battery safety (SOH) of the latest lithium iron
[Automotive Electronics]
Austrian Microelectronics' latest design separates sensor from microcontroller
Design and implementation of aircraft heading control display teaching model based on single chip microcomputer
0 Introduction Intuitive teaching is an effective means to enhance the connection between theory and practice for flight trainees. With the development of multimedia technology, how to improve the irreplaceable demonstration function of traditional teaching models has become the key to restricting the quality of
[Microcontroller]
Design and implementation of aircraft heading control display teaching model based on single chip microcomputer
Digital display tube shows the key sequence number 89c51 single chip microcomputer programming example
Topic: AT89C51 single-chip microcomputer P1 port is connected to 8 buttons, P2 port is connected to a common cathode digital display tube, when a key is pressed, the digital display tube displays the key number Circuit Diagram: The source code is as follows:     org 00h     sjmp start     org 30h start:
[Microcontroller]
Using AT89C series microcontroller to realize remote control of 5-inch TFT-LCD
Due to its thinness, lightness and low power consumption, LCD has been widely used in industrial and civilian fields such as computer monitors, VCD display panels, and portable medical equipment. Among these products, the application of microcontrollers can greatly improve their intelligence and ease of use, bringing c
[Microcontroller]
MCU program memory ROM
  The English abbreviation of single-chip program memory is ROM. The data stored in ROM is generally written in advance before being installed in the whole machine. It can only be read out during the operation of the whole machine, and it cannot be quickly and conveniently rewritten like random access memory. The data
[Microcontroller]
Latest Analog Electronics Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号