Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Device control, buttons and MCU?

Status
Not open for further replies.

Javert

Member level 2
Joined
Sep 14, 2019
Messages
44
Helped
0
Reputation
0
Reaction score
1
Trophy points
8
Activity points
551
I have such a philosophical programming question.
I have a project in which the STM32 controls a pair of air valves according to a prepared program or according to how the operator presses the controls on the panel (rather for condition and functionality testing, not normal operation).

The first version was simple when the user pressed a button so the edge triggered an extit and a flag was set in the interrupt handler (button pressed).
In the main program (infinite loop) the flag was checked at each pass and if it was set some command was executed (for example the valve was opened for 100ms)

Now two things have happened

1. I had to add the emWIN library to the project and with it the RTOS, (I chose FreeRTOS)
Yes, so far I have only added our infinite loop to a single system task

2. For one button I need to
- if pressed briefly to function as before
- if pressed for a long time to turn the selected valve on permanently


Now I'm thinking about how to do it and I can't decide which solution to choose.
Which is the best in the future


My ideas

- Add a loop to the interrupt handler, which will be executed as long as the button is pressed, and then return the short or long press flag, depending on the number of passes.
- In the interrupt handler, at the rising edge, note the time (How best to mark the time?) or start the timer. In the interrupt handler, at the falling edge, read the times or stop the counter and set the short or long press flag depending on the time
- somehow use FreeRTOS, whether you use software OStimer or front operation of control buttons to move to a separate task. I don't know I have no experience with that


What is the solution that will be the most suitable for the future? For example, when the complexity of control or the complexity of the program increases again-


I really don't even know how long to choose the time to distinguish between short and long presses? Just 1 second or 1.5, 2, 3 ..? when will it still be interchangeable and when will it bother the operator?


I would welcome both my own obedience and a link to a sample example with the solution of operating the buttons, for example in FreeRTOS
 

Hi,
Add a loop to the interrupt handler, which will be executed as long as the button is pressed,
--> absolute no-go. An ISR should be as short as possible. No delay, no busy wait.
******

I don´t know your RTOS configuration.
We usually run the main task with strict timing: let´s say every 10ms.
Then there is no need to use the change_on_input_state_ISR at all.
Just read the pushbutton state in the (timed) main task.
Then increment a counter when pressed. 1 --> (10ms later) 2 --> (10ms later) 3 --> (10ms later) 4 --> (10ms later) 5 ...
Clear the counter when not pressed.
(100 represents 1 second. )
So if counter_value = long_time_pressed_value then do .... whatever you want.

Klaus
 

I really don't even know how long to choose the time to distinguish between short and long presses?

Flowchart is your best friend regardless on how many/which layers of the program you are; if you assign a state for each stage of your program this will guide you on the code, for instance you can define a state for key pressed, another state for key released, and account the elapsed time, which will determine what action should be performed, in other words, what state to be selected.
 

When you are dealing with mechanical switches with contacts that exhibit bounce
you generally have to debounce them, either electrically or in code. I think most people
use ~ 50 - 100 mS to consider a stable value. Although I worked once with a switch that was
just shy of 400 mS, very poor quality switch. Then you also have to consider do you
want button push to be valid only after it has been detected then released ? There are
compromises if that is used, but it insures no false states.

Some processors have in their internals Hardware debouncers like -

1592593451152.png


As recommended by Andre there are utilities on web for drawing, here is one for state
machines -



Regards, Dana.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top