Preface
In the previous section, we completed the 51 single-chip microcomputer to control LED lights and achieved the effect of flowing lights. In this section, we will learn to operate independent buttons and use two buttons to turn on and off all LED lights respectively. Buttons are very common as input devices in the system development process. Generally speaking, buttons can set system parameters and control the system operation status.
1. Basic knowledge
1. Button Introduction
The switch used for common buttons is a mechanical elastic switch. When the mechanical contacts are opened and closed, the voltage signal will change. The common button switch is shown in the figure below, where the A-end pins are on the same side and are disconnected by default, and the B-end pins are not on the same side and are turned on by default. When the button is pressed, the A-end is turned on and the B-end is disconnected.
This type of switch usually does not turn on immediately when pressed. The moment of opening and closing is accompanied by a series of jitters, generally 5ms to 10ms. The specific jitter time is determined by the mechanical characteristics of the button. The jitter process is shown in the following figure:
In order to solve this problem, two methods, hardware debouncing and software debouncing, can usually be used. The principle of hardware debouncing is to use capacitors to smooth the wave, and then after Schmidt inverter shaping, a pulse wave without burrs is obtained. In most actual projects, in order to save costs, software filtering is usually used. Software filtering generally uses a delay method. When the key is first detected to be pressed, the software delays for about 10ms, and then checks the key status again. If it is still pressed, it is considered that the key is pressed.
2. Key detection principle
The schematic diagram of a simple independent button connection circuit is shown below
In the independent button circuit, one side of the button is connected to GND, and the other side is connected to the IO port of the microcontroller. When the button is pressed, 1 and 2 on both sides of the button are turned on, and the P2.5 pin of the microcontroller is directly connected to GND, and the level is low. Therefore, you only need to let the microcontroller continuously detect whether the I/O port of the P2.5 pin is at a low level. Once the program detects that the I/O port pin becomes a low level, it means that the button is pressed.
3. Development board button hardware circuit
The schematic diagram of the independent buttons of the development board used in the experiment is as follows. One end of the pins of the four buttons K1, K2, K3, and K4 are grounded, and the other ends are connected to different I/O ports of the microcontroller. Whether the button is pressed can be detected based on the electrical levels of different I/O ports.
2. Examples
1. Create a project
Copy the project from the previous section and change the name to 02-key
2. Program modification
#include sbit key1 = P2^4; sbit key2 = P2^5; unsigned char val = 0; void delay_ms(int ms) { int i = 0, j = 0; for (i = ms; i > 0; i--) { for (j = 110; j > 0; j--); } } int main(void) { while(1) { if (0 == key1 || 0 == key2) { delay_ms(10); if (0 == key1) { P1 = 0x00; } if (0 == key2) { P1 = 0xFF; } } delay_ms(5); } } The program detects the status of KEY1 and KEY2. When KEY1 is pressed, the eight LED lights LED3-LED10 are turned on. When KEY2 is pressed, LED3-LED10 are turned off. 3. Operation After compiling the code, download the generated project.hex file to the board according to the first section. When you press KEY1, the effect is as shown below: When you click KEY2, the light goes out. Since it’s quite simple, I won’t post the picture.
Previous article:51 MCU Basics of IoT 04-Buzzer
Next article:51 MCU Playing with the Basics of the Internet of Things 02-Light up the LED lights to achieve a marquee effect
- 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
- ST60 Short-range Contactless 60GHz Millimeter Wave Connector Different Obstacle Test
- Solve the problem of BQ76930 20 series circuit driving circuit
- Detailed discussion on embedded C programming experience
- You must know these contents - Nichicon's "Capacitor Doctor" series of short films are online
- Have you ever experienced or heard of such severance pay?
- Introduction to crystal oscillator circuit in single chip microcomputer (microcontroller)
- Internet radio design based on RSIC-V RVB2601 (1)
- EEWORLD University----[High Precision Laboratory] Motor Drive: Introduction
- Create a new micropython container in docker
- 1