Hello!
Indeed, using a loop is the worst possible method. The reason explained above
is that it freezes the program. But there is another reason:
If an edge is detected, it means that the button has been pressed. Whatever the
duration. If you use a delay loop as above, and if you get a 100 ms pulse from a
button:
- You detect that the button has been pressed
- You set a delay
- The button is released before the end of the delay
- The delay expires, and since the button is not pressed anymore,
you conclude that the button was not pressed. But it was pressed.
What I usually do:
- Detect an edge at a given button;
- At that point, there are 3 actions:
* Fire a timer
* Disable interrupts for that given button
* Process the actions that should be caused by the button
- When the timer expires, get a timer interruption, and in the timer interruption
routine, re-enable the interruptions for the button you just disabled.
With this method
- You will never miss one button except if you type faster than the timer
- You will never have a rebound except if your timer is really too short.
- As you don't use a wait loop, you can use the processor for real work.
Dora.
Dora