principle
Button Introduction
A touch switch is an electronic switch. When in use, lightly press the switch button to turn on the switch. When you release your hand, the switch is turned off. The switch we use is shown in the figure below:
Independent button principle
When the key is closed and opened, the contact will jitter.
Hardware debounce circuit
Software debounce
We can use the delay function
Code
Use independent keyboards K1, K2, K3, and K4 to control the on and off of the four LED lights on the left.
#include typedef unsigned char u8; typedef unsigned int u16; sbit K1=P3^1; //The first button sbit K2=P3^0; //The second button sbit K3=P3^2; //The third button sbit K4=P3^3; //The fourth button sbit led1=P2^0; sbit led2=P2^1; sbit led3=P2^2; sbit led4=P2^3; void delay(u16 i) { while(i--); } void main() { while(1) { if(K1==0)//Judge whether it is pressed { delay(100); //If you press the button to cancel the bounce if(K1==0)//Judge whether it is pressed { led1=~led1; while(!K1); //Check if the button is released } } if(K2==0)//Judge whether it is pressed { delay(100); //If you press the button to cancel the bounce if(K2==0)//Judge whether it is pressed { led2=~led2; while(!K2);//Check if the button is released } } if(K3==0)//Judge whether it is pressed { delay(100); //If you press the button to cancel the bounce if(K3==0)//Judge whether it is pressed { led3=~led3; while(!K3);//Check if the button is released } } if(K4==0)//Judge whether it is pressed { delay(100); //If you press the button to cancel the bounce if(K4==0)//Judge whether it is pressed { led4=~led4; while(!K4); //Check if the button is released } } } }
Previous article:51 single chip microcomputer learning - 7--8*8LED dot matrix display
Next article:51 MCU Learning——3-Buzzer
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!
- Rambus Launches Industry's First HBM 4 Controller IP: What Are the Technical Details Behind It?
- 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
- TMDSEVM572X development board link emulator
- [Sipeed LicheeRV 86 Panel Review] 3-System Burn-in Test
- Warehouse temperature alarm system based on GD32 --- DS18B20 data reading
- STM32MP157A-DK1 Review - 2. MPU Functions
- [AB32VG1 development board review] OLED screen display driver
- TI TMS320F2812 SVPWM Program
- Android 5.0 BLE low-power Bluetooth slave device application
- JHL HOOD 1969 small class A amplifier board
- Solutions to several difficult problems in the CCS debugging process
- Xunwei iTOP4418 development board Qt system ported 4G-EC20