I recently learned about single-chip microcomputers. When making buttons, I added anti-shake, but accidentally turned it into continuous pressing. If the anti-shake time is extended, it will affect the program execution. Finally, I added a button release detection to solve the problem of accidentally turning it into continuous pressing.
//----------------------Independent buttons-------------------------------------------------------------------
//4-bit independent button test, with anti-shake function and anti-long press function
//--------------------------------------------------------------------------------------------------
#include #include "Keyboard.h" void Key_Init() { TRISC=0xF0; //High 4 bits input, key detection, low 4 bits output, sound output //Beep=0; } //--------------------------------------------------------------------------------------------------- //Key prompt tone //n is the number of beeps. When l=0, it beeps about 50ms. When l>0, it beeps about 500ms. //--------------------------------------------------------------------------------------------------- void Beep_Beep(INT8U n,INT8U l) { do { Beep=1; if(l>0) __delay_ms(500); else __delay_ms(50); Beep=0; if(l>0) __delay_ms(500); else __delay_ms(50); }while(1 //--------------------------------------------------------------------------------------------------- //Detect the button to prevent long press. Long press will only display one press. //--------------------------------------------------------------------------------------------------- char Key() { if(Key1==0) //If the key is pressed { __delay_ms(10); //delay 10ms if(Key1==0) //Continue to check whether it is pressed. If it is, it means the button is pressed. If not, it means it is shaking. { while(!Key1); //Wait for the button to be released before returning to the button value to prevent continuous pressing due to untimely release return 1; } } if(Key2==0) { __delay_ms(10); if(Key2==0) { while(!Key2); return 2; } } if(Key3==0) { __delay_ms(10); if(Key3==0) { while(!Key3); return 3; } } if(Key4==0) { __delay_ms(10); if(Key4==0) { while(!Key4); return 4; } } return 0; } head File //----------------------Independent buttons-------------------------------------------- //4-bit independent button test, with anti-shake function //-------------------------------------------------------------------------- #define Beep RC3 #define Long_Beep 1 #define Short_Beep 0 #define Key1 RC4 #define Key2 RC5 #define Key3 RC6 #define Key4 RC7 #define _XTAL_FREQ 4000000UL #define INT8U unsigned char //---------------------------------------------------------------------------- //Function declaration //---------------------------------------------------------------------------- void Beep_Beep(INT8U n,INT8U l); void Key_Init(); char Key();
Previous article:PIC16F1933 lights up LCD1602 (assembly)
Next article:Design of DC motor speed regulation with PIC16F874 microcontroller as the central processor
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
- On the last day of 2019, make a New Year’s wish for 2020!
- How to use Labview to perform secondary development on Ginkgo 2
- Design principle and usage of true random number generator of low power MCU RJM8L303
- How to make an elevator controller based on 8952 microcontroller?
- Interpretation of JTAG interface for embedded development ARM technology
- EEWORLD University ---- Jixin STM32 Smart Car
- Experience in using PWM of 28069 and 28377D of C28x series
- At 10:00 this morning, we invite you to listen to the award-winning live broadcast: ADI's digital active noise reduction headphone solution allows technology to calm us down~
- Common MOS tube models and parameter comparison table
- From terminals to architecture, TE Connectivity (TE) helps you connect to the 5G high-speed future. Watch the video and answer questions to win gifts!