4.1 Basic Operation
This section presents a basic keypad application where a bare minimum of peripherals and features have been used. It is intended to show the basics of interfacing a keypad with an AVR® microcontroller.
The figure below shows the application main function. It initiates the microcontroller to the default main clock setting and configures the GPIO pins connected to the LED and to the keypad. The keypad pins are initiated with the rows as outputs with a low-output value and the columns as inputs with the internal pull ups enabled.
As shown in the figure, the device polls the column pins to see whether any of them have been pulled down because of a connection to the row. If a connection is detected, the device busy-waits for 10 ms before checking the column pins again. If one of the column pins are still pulled to the row voltage, the key press is considered valid. This sequence is a simple implementation of a button debouncing technique.
When a valid key press has been detected, the functions scan_keys()
and
check_passcode()
are run. The two functions are discussed in the
following sections.
When the key press has been properly handled by the two functions, the device polls for the button to be released. This is done to prevent one press to be registered multiple times. When the button is released, the device returns to polling for a new key press.