✨ Core Value
Tired of button debounce issues? Struggling with long and short press detection?Button_det — A struct that solves all button detection challenges!
🎯 Summary in One Sentence
c
KeyState myButton ={100};// Initialization, done!
With just this line of code, you have a fully functional button detector!
🚀 Four Core Features
1. ⏰ Intelligent Debounce
c
if(myButton.HI_press)
{
printf("Pressed! Not a bounce");
myButton.HI_press =0;
}
2. ⏱️ Long and Short Press Recognition
c
if(myButton.HI_T >3000){printf("Long press for 3 seconds!"); myButton.HI_T =0;}
3. 🔄 Edge Detection
c
if(myButton.UP){printf("Button pressed momentarily"); myButton.UP =0;}
4. 📊 Real-time Timing
c
if(myButton.HI >1000){printf("Pressed for 1 second");}
🛠️ Minimal Usage
Step 1: Create Object
c
KeyState powerButton ={50};// 50ms debounce
Below are the objects included in the struct
typedef struct{uint Jitters; // Debounce time uint HI; // High level time, initial value=0 when the system powers on, first rising edge is valid, =1 when invalid uint LO; // Low level time, initial value=0 when the system powers on, first falling edge is valid, =1 when invalid char HI_press;// High level debounce pressed flag, must be manually cleared by the user char LO_press;// Low level debounce pressed flag, must be manually cleared by the user uint HI_T; // Last high level time, must be manually cleared by the user uint LO_T; // Last low level time, must be manually cleared by the user char UP; // Rising edge flag, must be manually cleared by the user char DO; // Falling edge flag, must be manually cleared by the user}KeyState;
Step 2: 1ms Interrupt Detection
c
void Timer_ISR(){KeyDET(&powerButton, P20);}
Step 3: Main Loop Response
c
if(powerButton.HI_press){do_something(); powerButton.HI_press =0;}
💡 Why Choose It?
| Traditional Solutions | Button_det |
|---|---|
| ❌ Repeated debounce code | ✅ Automatic debounce |
| ❌ Complex long and short press logic | ✅ Built-in duration detection |
| ❌ Redundant code for multiple buttons | ✅ Object-oriented design |
| ❌ Edge detection is troublesome | ✅ Rising and falling edges are directly usable |
🎮 Practical Application Scenarios
Smart Home 🏠
c
if(button.HI_T >5000){enter_pairing_mode();// Long press for 5 seconds to enter pairing mode}
Industrial Control 🏭
c
if(button.UP && last_press_time <200){double_click_action();// Double click detection}
Consumer Electronics 📱
c
if(button.LO_press){wake_up_device();// Wake up on button release}
📝 Expert Reminder
⚠️ Remember to clear the flags in a timely manner! Just like washing dishes after a meal, clear the flags after use!
c
myButton.HI_press =0;// Clear after use, develop good habits
🎉 Get Started
Just three files:
-
<span>Button_det.h</span>– Header file -
<span>Button_det.c</span>– Implementation file -
Your main program
Say goodbye to reinventing the wheel for button detection!
💬 Reader Benefits
Discussion Topic: What is the most troublesome issue with button detection in your project? Feel free to leave comments for discussion!
📢 The code in this article is suitable for various MCUs such as 51, STM32, etc. To obtain the complete code, please reply “Button Detection” in the public account backend.