Maker Pro
Maker Pro

AVR: detecting various button presses

P

Pawel Paron

Jan 1, 1970
0
I'm looking for example procedures (preferably in assembler), or any ideas
how to do the following with tiny15:

- detect single short button press
- detect fast double-click
- detect single long press

All with the same single button, and without consuming all processor time
inside idle counter loops. I'd like to use timer0 for regular interrupts to
poll the button register, and timer1 for PWM (so no extra timer is available
for time counting).

Thanks in advance for any advices

Pawel
 
D

Dave VanHorn

Jan 1, 1970
0
Pawel Paron said:
I'm looking for example procedures (preferably in assembler), or any ideas
how to do the following with tiny15:

- detect single short button press

Poll reasonably frequently
- detect fast double-click

Having detected the first one, and knowing that it's no longer pressed,
start a timer byte that will still be non-zero if the button is pressed
again in a short while. Decrement this timer byte to zero with something
like the T0overflow ISR set to 10-100mS per
- detect single long press

Having detected button down, start incrementing a "held button" byte, each
time you poll.
If it gets high enough, you're there. If you detect "button not pressed",
then zero the counter.

All with the same single button, and without consuming all processor time
inside idle counter loops. I'd like to use timer0 for regular interrupts to
poll the button register, and timer1 for PWM (so no extra timer is available
for time counting).

You got ram dont you? :)
 
P

petrus bitbyter

Jan 1, 1970
0
Pawel Paron said:
I'm looking for example procedures (preferably in assembler), or any ideas
how to do the following with tiny15:

- detect single short button press
- detect fast double-click
- detect single long press

All with the same single button, and without consuming all processor time
inside idle counter loops. I'd like to use timer0 for regular interrupts to
poll the button register, and timer1 for PWM (so no extra timer is available
for time counting).

Thanks in advance for any advices

Pawel

Pawel,

I don't expect you to find "ready for use" code and if you find something
that looks like what you need, you may be trapped. It's often harder to
modify excisting code then to write your own, especially if you do not fully
understand that other code. Besides, the code itself will be short and
simple. The problems you need to face are both timing and interrupt
processing.

Let's treat timing.
You have to decide whether your processor has to listen to the switch all
the time or not but that has to do with its other tasks.
You have to decide the bounds of your swiching times. How long should a
single short pulse last, how long the time of the first of two pulses, the
time of the pause between the first and the second etc. Once you defined
this times you can decide the setting of your timer. Assume the shortest
pulse you want to recognise is 100ms, your timer should be set to, let's
say, 50ms. So you get an interrupt every 50ms. To complete the example, a
short pulse may lasts 100-500ms, a long one 600ms-2s and the pause between
two shorts 200-500ms.

Interrupt handling.
An interrupt routine should be as short as possible. In this particular
case, basically all it has to do is reading the input pin and report its
value to the main program. Treating the counting and the status can be done
in the main program loop as long as this loop lasts no longer then 50ms.
Otherwise you can succesfully do the counting in the interrupt routine. So
you reserve a (RAM)byte for the ones and another for the zeros and increment
the appropriate byte by the interrupt routine. Keep in mind to switch of the
interrupt temperory when you read and reset this bytes in the main program
loop.

In the main program you can keep track of the things happened using some
other RAM places. For instance, a short pulse has occured and you're
counting the time the switch has been released to decide whether or not a
second short pulse has to be detected.

petrus
 
P

Pawel Paron

Jan 1, 1970
0
Thanks for help. Actually I finished this part yesterday, in the way very
similar that you describe, using two registers as counters, increasing,
decreasing and clearing them apropriately within timer interrupt routine.
Wasn't that difficult like it seemed before.

Regards
Pawel
 
A

Andrew Lawrance

Jan 1, 1970
0
Pawel Paron said:
I'm looking for example procedures (preferably in assembler), or any ideas
how to do the following with tiny15:

- detect single short button press
- detect fast double-click
- detect single long press

All with the same single button, and without consuming all processor time
inside idle counter loops. I'd like to use timer0 for regular interrupts to
poll the button register, and timer1 for PWM (so no extra timer is available
for time counting).

Thanks in advance for any advices

Pawel

There are several AVR application notes dealing with keypads here is a
link to one of them http://www.eetkorea.com/ARTICLES/2000AUG/2000AUG30_MSD_AN.PDF.

Andrew
 
Top