Mbed button length to achieve

Label: EmbeddedStm32
94 people read comment(0) Collection Report

1, timer interrupt mode

#define LONG_PRESS 100 / 20ms*100=2s,
#define SHORT_PRESS 5 / 20ms*5=100ms,

BTN InterruptIn (USER_BUTTON);
Btn_now int = 0;
Btn_last int = 0;
Btn_long bool = false;
Btn_short bool = false;
Btn_sta bool;
Count int = 0;
Interval float = 0.02;

Key_scan void ()
{
Btn_last = btn_now;
Btn_now = btn.read ();
If (btn_last==1 & & btn_now ==0) ~ / / A
Btn_sta = true;
If (btn_last==0 & & btn_now ==1)'s / D
Btn_sta = false;
		
//printf ("%d\r\n btn_sta=", btn_sta);
		
If (btn_now = = btn_last btn_sta) {
Count++;
//printf ("%d \r\n count=", count);
If (count > LONG_PRESS) {
Printf ("is a long press \r\n button");
Btn_sta = false;
Count = 0;
					
I2c.write (0xA0, NULL, sizeof (stm32f411xx_baseboard_id struct) + 2);
Wait (0.02);
SoftReset ();
}
}
{else
If (count > SHORT_PRESS) {
Printf ("is a short press \r\n button");
Btn_sta = false;
Count = 0;
}
{else
Count = 0;
}
}
	
//printf ("is%d \r\n button", btn.read ());
}

Timer_key.attach (&key_scan, interval);

2, key interrupt and timer interrupt mode

Mbed.h "#include"
 
PC Serial (USBTX, USBRX);
MyButton InterruptIn (USER_BUTTON);
Myled DigitalOut (LED1);
Timer1 Timeout;
Timer2 Timeout;

Flag bool;
 
Float delay = 1; / / 1 sec

Long_press void ()
{
If (! Mybutton.read) ()
//myled =! Myled;
Pc.printf ("pressed \r\n long");
Else
Pc.printf ("pressed \r\n short");
}

Short_press void ()
{
If (mybutton.read ())
//myled =! Myled;
Pc.printf ("pressed \r\n short");
}
 
Pressed void ()
{
Timer1.attach (&long_press, 0.2*10);
Timer2.attach (&short_press, 0.2);
}
 
Main int ()
{
Pc.baud (115200);
Mybutton.fall (&pressed);
While (1) {
Wait (0.1);
}
}



top
Zero
step on
Zero

Reference knowledge base

Guess you're looking for
View comments
* the above user comments only represent their personal views, does not represent the views or position of the CSDN website
    personal data
    • Visit:1390 times
    • Integral:Forty-seven
    • Grade:
    • Rank:Thousands of miles away
    • Original:2
    • Reproduced:8
    • Translation:0
    • Comments:0
    Classification of articles