Press to connect, release to disconnect.
There are four pins in total. The two pairs that are farther apart are turned on in the initial state. The other two pairs are turned on after being pressed.
TXD connects to P3.0 port, RXD connects to P3.1 port
K1, 2, 3, 4 are connected to P31, P30, P32, P33 respectively, and the other ends are connected to the ground, that is, the port will be pulled low when the button is pressed.
Internal structure map:
Input 1, then output 1;
Input 0, then output 0.
(For example, when the input is 1, the NOT gate outputs 0, the diode is not conducting, and the IO directly outputs 1 from VCC; when the input is 0, the NOT gate outputs 1, the diode is conducting, and the IO outputs 0 from GND)
There is a jitter phenomenon when the electric shock is closed and opened.
Software debouncing or hardware debouncing circuit is required (as shown below)
Hardware debouncing is achieved through RC charge and discharge time. (Higher cost and more complex circuit)
Generally, software is used to eliminate jitter, with a delay of 10ms
Use the independent button k1 to control the state of led1 to reverse. Press it once to reverse. The program is as follows:
#include typedef unsigned char u8; typedef unsigned int u16; sbit led=P2^0; //D1 sbit k1=P3^1;//K1 void delay(u16 i) { while(i--); } void key() { if(k1==0) { delay(1000); //10ms delay for debounce if(k1==0) { led=~led; //Reverse the state of led while(!k1); //Exit only when k1=1, that is, when released } } } void main() { while(1) { key(); } } Use the independent button k1 to control the LED1 to reverse. When pressed, it turns off, and when released, it turns on. The procedure is as follows: #include typedef unsigned char u8; typedef unsigned int u16; sbit led=P2^0; //D1 sbit k1=P3^1;//K1 void delay(u16 i) { while(i--); } void key() { if(k1==0) { delay(1000); //10ms delay for debounce if(k1==0) { led=~led; //Reverse the state of led while(!k1);//Until release, the loop stops and the following program is performed } } if(k1==1) //used for release { delay(1000); if(k1==1) { led=~led; while(k1); //Until closed, the loop stops and the following program is performed } } } void main() { while(1) { key(); } } Use independent buttons to control whether the dynamic digital tube is displayed. Release the button to display it and press it to turn it off: #include typedef unsigned char u8; typedef unsigned int u16; sbit LSA=P2^2; sbit LSB=P2^3; sbit LSC=P2^4; sbit k1=P3^1; u8 code smgduanxuan[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; void delay(u16 i) { while(i--); } void saomiao() { u8 i; for(i=0;i<8;i++) { switch(i) { case 0:LSA=0;LSB=0;LSC=0;break; case 1:LSA=1;LSB=0;LSC=0;break; case 2:LSA=0;LSB=1;LSC=0;break; case 3:LSA=1;LSB=1;LSC=0;break; case 4:LSA=0;LSB=0;LSC=1;break; case 5:LSA=1;LSB=0;LSC=1;break; case 6:LSA=0;LSB=1;LSC=1;break; case 7:LSA=1;LSB=1;LSC=1;break; } P0=smgduanxuan[i]; delay(100); P0=0x00; } } void main() { while(1) { saomiao(); while(!k1); //When k1=0, i.e. pressed, the loop will continue, the program will not proceed further, and the digital tube will go out } }
Previous article:10. Matrix buttons
Next article:8. Dynamic digital tube
- 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
- Nordic Bluetooth ICnRF52832 and 52840 Differences
- [SAMR21 New Gameplay] 3. Introduction to Graphical Programming
- Design of PCIJMC2000 Computer Data Encryption Card Based on DSP
- Review Weekly: New arrivals, Qinheng wireless charging kit, Beineng cost-effective ATSAMD51 board are waiting for you~20221107
- The ultimate setup for your next Python project
- 12. [Learning LPC1768 library functions] PWM experiment
- How to Actually Build a Drone
- TLV5616 has no output voltage
- What is the DMD lens action mechanism of the projector?
- Design of Watchdog Circuit Based on DSP Processor TMS320F2812