Page 189 - Embedded Microprocessor Systems Real World Design
P. 189
Timer ISR code:
If switch closed and if switch pressed flag not set, increment debounce
counter.
If debounce counter = maximum value, set switch closed flag.
If switch not closed, clear debounce counter and clear switch closed flag.
For this to work, the timer ISR must be based on a regular, repeating timer event.
The non-ISR code ignores the state of the switch; it just looks at the state of the
switchclosed flag, which is a memory location (OF even a bit in a memory loca-
tion). The way this code works is that when the switch contacts are bouncing (inter-
mittently making and breaking), the debounce counter will increment a few counts,
then get reset, then increment a few counts, and so on. Once the contacts have
settled, the counter will increment to its maximum value and the switchclosed flag
is set.
The maximum value of the debounce counter depends on the interrupt rate
and the required debounce time. If the timer ISR occurs every millisecond, then a
maximum count value of 30 would produce a 30ms debounce time.
If you are only debouncing one switch, you can use the counter itself as an
open/closed flag. If the counter is at maximum value, the switch is closed; other-
wise, it is open. The ISR code must stop incrementing the counter when the
maximum count is reached, of course.
If you need to debounce both switch opening and closing (such as when
debouncing a door interlock), you can have two debounce counters: One counter
debounces opening, and one debounces closing. When the switch indicates open,
the close counter is reset and vice versa. You only change the state of the
open/closed flag when the appropriate counter reaches maximum value.
Although this is a lengthy explanation of a simple situation, it illustrates how
interrupts allow an embedded system to keep up with real-time requirements while
doing things at human-compatible speeds. Similar techniques can be used for
things like:
Blinking status LEDs at a rate people can see them.
0 Refreshing a display on a regular basis.
Blinking a character on the same display for emphasis.
Implementing long delays, such as turning off the illuminated keypad on a cell
phone after 5 seconds.
Generating audio alerts.
Filtering noisy signals such as an optical sensor exposed to ambient light.
The software, hardware, and interrupt structure are the primary components
of the embedded system. Chapter 6 explains adding hardware and software to
simple the debug process.
170 Embedded Mamopocessm Systems