3741 views|19 replies

931

Posts

3

Resources
The OP
 

GD32E231 DIY Contest (4) - How to get long and short keys? [Copy link]

 
    There is only one user button "Wakeup" on the development board, but my project requires at least two states of user input. The simplest method is to identify the user's long press or short press. This is how I deal with it. Set the button to interrupt mode, and set the global variable keys = 1 in the interrupt handler. This is the code of the interrupt handler: void EXTI0_1_IRQHandler(void) { if(RESET != exti_interrupt_flag_get(EXTI_0)){ keys = 1; gd_eval_led_toggle(LED4); } exti_interrupt_flag_clear(EXTI_0); } In the main loop, when keys>0 is detected, jump to the key processing program. The code is as follows: if(keys > 0) //A key is pressed key_processing(); In the key processing program, detect whether the key is still pressed after a delay. The code is as follows: delay_1ms(100); if(SET == gpio_input_bit_get(GPIOA,GPIO_PIN_0)) keys = 3; delay_1ms(200); if(SET == gpio_input_bit_get(GPIOA,GPIO_PIN_0)) keys = 6; The interrupt processing and main loop judgment are normal, but the purpose cannot be achieved in the key processing. Regardless of the length of the key press, keys is always 1. I changed the keys = 1 in the interrupt processing program to keys++, and commented out the key processing code. The returned keys value will change, but it cannot stably correspond to the length of the key press. I have been debugging repeatedly throughout the May Day holiday, but I still cannot get a satisfactory result. Please help me find out the reason and what method should be taken to achieve the requirement. Thank you in advance!

This post is from GD32 MCU

Latest reply

Newbies in electronic design, come and learn and see how the masters think.  Details Published on 2023-8-15 09:11
 

1366

Posts

6

Resources
2
 
链接已隐藏,如需查看请登录或者注册
This post is from GD32 MCU

Comments

Thanks for the link! I'm still learning, and I'll ask you if there's anything I don't understand.  Details Published on 2019-5-6 14:53
 
Personal signature

1084534438 欢迎交流  [加油,一切皆有可能]

 
 

5303

Posts

454

Resources
3
 
Why don't we use a timer to query the buttons? If the buttons are read once every 10ms and the cumulative number of presses is greater than 200, it is considered a long press!

This post is from GD32 MCU

Comments

Is it possible to set the timing time to 10ms, and then get the key status in the interrupt processing, and increase the keys value by 1 when the key is pressed.  Details Published on 2019-5-6 14:26
 
 
 

931

Posts

3

Resources
4
 
I also encountered this problem on the GD32E230 development board, and finally had to enable two buttons to solve it. Fortunately, there are 3 buttons on the 230 board (including the reset button). I have implemented it in a similar way under STM32F103, and it can correctly distinguish long and short buttons.
This post is from GD32 MCU
 
 
 

931

Posts

3

Resources
5
 
Blue Rain Night posted on 2019-5-6 14:16 Why don't you use a timer to query the key? Read once every 10ms, and if the cumulative number of presses is greater than 200, it is considered a long press!
Is it possible to set the timer time to 10ms, and then get the key status in the interrupt processing, and the keys value increases by 1 when the key is pressed.
This post is from GD32 MCU

Comments

If you must use the response to external interrupt, then press start to start the timer, read the timing value when releasing it, and then judge the length of time. What do you think?  Details Published on 2019-5-6 15:28
 
 
 

931

Posts

3

Resources
6
 
RCSN published on 2019-5-6 14:02 https://github.com/0x1abin/MultiButton Check it out
Thanks for the link! I am still learning, and I will ask you if there is anything I don’t understand.
This post is from GD32 MCU
 
 
 

5303

Posts

454

Resources
7
 
hujj posted on 2019-5-6 14:26 Is it possible to set the timing time to 10ms, and then get the key status in the interrupt processing, and the keys value increases by 1 when the key is pressed.
If you must use the response to external interrupts, then press Start to start the timer, read the timing value when it is released, and then judge the length of time. What do you think!

This post is from GD32 MCU
 
 
 

931

Posts

3

Resources
8
 
I'll try again.
This post is from GD32 MCU
 
 
 

931

Posts

3

Resources
9
 
A simple problem has been complicated. I have been working on this key length issue for 6-7 days, but I have never been able to achieve my goal. During this period, I have also tried routines such as MultiButton, flexible_button, and ButtonDrive_master. These routines are fully functional, with standardized interfaces and wide applicability, but it is not easy to use them. Either this parameter does not meet the requirements or that setting is incorrect, resulting in compilation failure. Today, I suddenly realized that I used the simplest polling button plus DELAY delay to successfully achieve my goal. Although this is inefficient, it is not a problem for my project. The code is as follows: while(1){ keys = 0; while(gd_eval_key_state_get(KEY_WAKEUP)) { keys++; delay_1ms(100); } if(keys > 0) //A key is pressed key_processing();
This post is from GD32 MCU
 
 
 

61

Posts

1

Resources
10
 
When a button is pressed, the entire MCU is occupied by delay... what a waste.
This post is from GD32 MCU

Comments

It is a waste of MCU, but in my project, the MCU is not used and it is an empty loop here, so I don't care.  Details Published on 2019-5-7 17:40
 
Personal signature

intersil Techwell应用工程师

 
 

931

Posts

3

Resources
11
 
Li Baiyi published on 2019-5-7 13:53 When the button is pressed, the MCU is fully occupied by delay..., what a waste.
It is a waste of MCU, but in my project, the MCU is not used here and it is an empty loop, so I don't care.
This post is from GD32 MCU
 
 
 

1

Posts

0

Resources
12
 
Li Baiyi published on 2019-5-7 13:53 When the button is pressed, the MCU is fully occupied by delay... What a waste.
The master sees through it
This post is from GD32 MCU
 
 
 

124

Posts

1

Resources
13
 
RCSN posted on 2019-5-6 14:02 https://github.com/0x1abin/MultiButton Check it out
Thanks, the information is good~
This post is from GD32 MCU
 
 
 

282

Posts

2

Resources
14
 
In fact, you can use a timer to do it. In the last GD competition, there were many buttons needed, and I also used long and short presses to distinguish different functions. My implementation method was to use a timer. The device will detect the button status every 10ms or 1ms, and then use a static variable counter to accumulate. I used 10ms at the time. The short press time was less than 1s, and the long press time was greater than 1s. When the button is released, check the value of the counter. If the counter is greater than 100, it is a long press, and if it is less than 100, it is a short press. I think this method is still better. Of course, you can also add a 40ms filter, which can also be done in the timer.
This post is from GD32 MCU

Comments

Thank you for your suggestion! The forum friend on the 3rd floor also suggested the same thing. I was also going to try it at first, but I couldn't find a suitable reference code. Besides, although the polling method is inefficient, it can meet the needs of the project, so I will use it temporarily. After the main part of the project is completed, if there is still time, I will try the timer method.  Details Published on 2019-5-11 13:14
 
 
 

931

Posts

3

Resources
15
 
hehung posted on 2019-5-11 11:50 In fact, you can use a timer to do it. In the last GD competition, there were many buttons needed, and I also used long and short presses to distinguish different functions, ...
Thank you for your suggestion! The forum friend on the 3rd floor also suggested this. I was also going to try it at first, but I didn’t find a suitable reference code. Besides, although the polling method is inefficient, it can meet the needs of the project, so I used it temporarily. After the main part of the project is completed, if there is still time, I will try the timer method and optimize the code.
This post is from GD32 MCU

Comments

I wrote a program that determines the long and short presses of the timer. You can take a look at it: https://en.eeworld.com/bbs/thread-1076729-1-1.html  Details Published on 2019-5-12 00:15
 
 
 

869

Posts

0

Resources
16
 
When the button is pressed, the MCU is completely occupied by delay... What a waste
This post is from GD32 MCU

Comments

What is waste? Isn't it wasteful to have an empty loop in the main loop?  Details Published on 2019-5-11 18:55
 
 
 

931

Posts

3

Resources
17
 
zhuyebb posted on 2019-5-11 15:12 When a button is pressed, the MCU is completely occupied by delays... What a waste
What is waste? Isn't it a waste to have an empty loop in the main loop?
This post is from GD32 MCU
 
 
 

282

Posts

2

Resources
18
 
hujj posted on 2019-5-11 13:14 Thank you for your suggestion! The forum friend on the 3rd floor also suggested this. I was also going to try it, but I didn't find a suitable reference code. Besides, the polling method is...
I wrote a program that determines the long and short presses of the timer. You can take a look at: https://en.eeworld.com/bbs/thread-1076729-1-1.html
This post is from GD32 MCU

Comments

Thanks for sharing! I will try your code.  Details Published on 2019-5-12 09:30
 
 
 

931

Posts

3

Resources
19
 
hehung posted on 2019-5-12 00:15 I wrote a program that determines the long and short presses of a timer. You can take a look at: https://en.eeworld.com/bbs/thread-1076729-1-1.h ...
Thank you for sharing! I am going to borrow your code to try it out.
This post is from GD32 MCU
 
 
 

195

Posts

0

Resources
20
 
Newbies in electronic design, come and learn and see how the masters think.
This post is from GD32 MCU
 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews

Room 1530, Zhongguancun MOOC Times Building, Block B, 18 Zhongguancun Street, Haidian District, Beijing 100190, China Tel:(010)82350740 Postcode:100190

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list