Advanced Keypad Implementation

The main application flow of the advanced keypad application is shown in the figure below. It shows that the device is set to sleep when waiting for a keypress. A pin change interrupt wakes up the CPU and lets it scan the button that was pressed.

Figure 1. Main Application Flow

After wake up from sleep, the button press is debounced. This is performed by running a loop ten times, in which the button is checked to be pressed as shown in the chart below. If the button is found to be pressed all ten times, the button press is considered valid. If the button is not read as pressed while iterating through the loop, the validation flag is cleared and the CPU exits the loop.

Figure 2. btn_debounce()

The debounce function uses a 2-ms delay between the iterations of the loop. This delay is implemented using a timer delay as shown in the figure below. The Periodic Interrupt Timer (PIT) is programmed to trigger an interrupt once every millisecond. When the interrupt is triggered, a counter is incremented, and when this counter reaches the desired number of microseconds, the CPU returns from the function.

Figure 3. lp_delay_ms()

The scan_keys() and check_passcode() functions are identical to those of the basic keypad example, except that they use the low-power timer delay function when timing the LED indicators.