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