Application Note: Push Buttons & Path Switcher
Introduction
This application note presents programming techniques for implementing and debouncing from 1-4 push
buttons. This implementation also demonstrates the use of a path switcher (combined with a simple real-time
clock1) to reduce the execution time of each interrupt. The program relies upon the SX's internal interrupt
feature to allow background operation of the clock, buttons, and path_switcher as virtual peripherals.
How the circuit and program work
This firmware module requires no external circuitry, other the push buttons, their pull-up resistors, and an
(optional2) oscillator crystal, making it quite straight forward. The real-time clock peripheral is described
elsewhere (see note 1 below), and will not be discussed here other than that it passes control to the path switcher
virtual peripheral once per millisecond.
The path_switch routine simply looks at the lower 2 bits of the real time clocks msec count and jumps to
the corresponding push button vector. This allows for only one push button sequence to be run per interrupt and
reduces the overall execution time of the interrupt sequence. This feature of path_switch may be used to select
from amongst any number of code segments (including other than just push button modules) which do not
require execution during each interrupt cycle. For such purposes, it does not even need to be combined with the
real time clock3, which is used here to simplify push button debounce time processing.
The push buttons are wired directly from port B, pins 0-3* to ground, with a 100K pull-up resistor4 also
connected to each port pin, but wired to Vdd.
Within a few5 milliseconds of any pushbutton press, the corresponding pbx (where x=0-3) push button
code sequence will register the press. First, the program checks whether it is a new press by looking at the
corresponding pbx_down flag. If its not a new press, it is ignored. If it is new, the program makes sure the
pbx_down flag is cleared and then begins incrementing the corresponding debouncex counter variable upon
subsequent passes through the interrupt until it detects that the switch contacts have been sufficiently
debounced6, in which case the pbx_pressed flag is set along with the pbx_down flag.
It this code example, is the main programs responsibility, performed by the button_check main loop code
sequence, to scan the pbx_pressed flags to watch for a button press, and to make sure theyre reset (cleared)
once the appropriate button action has been taken.
If the button actions are short, they may be placed directly in-line in the interrupt code segment for the
corresponding button. This has the attractive benefit of avoiding the need for any main loop handling of the
buttons whatsoever, but also carries the disadvantage of increasing the overall length of the interrupt routine
(which is somewhat compensated for by the path_switcher code module).
1
Described in more detail a separate application note: Virtual Peripheral Real Time Clock
2
If a lot of accuracy is needed on the clock, the SXs internal oscillator may be used by adjusting the msec tick count value to the
appropriate count, as described in the above application note.
3
If it is not to be combined with the real time clock module, some type of counter must still be maintained to control switching.
*
From 1-4 push buttons may be used in this implementation. The user then sets the num_buttons parameter variable accordingly.
4
The value of the pull-up is not very crucial, since the push button port pins are always set as inputs (i.e. high impedance), and the
duration of presses is usually insignificant in terms of power consumption. Lower or higher resistor values can therefore be used.
5
This value depends on how many push buttons are being used in total. It will range from 0-3 msec, depending.
6
The amount of time any given switch or button takes to debounce is not obvious by any means, and varies with switch type and
press speed and pressure, etc. A good rule of thumb to assure catching rapid sequential presses while avoiding false double triggering
is about 10-20 msec for an average click type push button (and most other switches). This value can always be experimented with.