4.2.2 Interrupt Operation
The basic keypad application makes the CPU continuously poll the column pins to see if a button has been pressed. Polling buttons with the CPU is inefficient use of both power and CPU resources.
To solve this problem, interrupts can be used. The tinyAVR® and megaAVR® devices feature pin sense interrupts for all of their GPIO pins. With pin sense interrupts enabled, the PORT peripheral will look over the pins and automatically notify the CPU if a change has been detected. This frees the CPU to perform different tasks while waiting for a key press, or to go to sleep to save power.
- FALLING: Triggers when the logic input level of the pin has dropped from high to low
- RISING: Triggers when the logic input level of the pin has risen from low to high
- BOTHEDGES: Triggers both when the logic input level of the pin has dropped from high to low or risen from low to high
- LEVEL: Triggers whenever the pin is detected to be low
When combining the pin sense interrupts with a sleep mode, further considerations must be made. Not all pin sense interrupt triggers are compatible with all sleep modes. In the low-power keypad application, the lowest possible power consumption is desired and, therefore, a trigger that can wake the device from Power-Down Sleep mode must be used. The BOTHEDGES and the LEVEL triggers are able to wake the device in the deepest sleep mode. In the application, the BOTHEDGES trigger has been selected.
Configuration of the BOTHEDGES interrupt trigger for the columns of the keypad is shown in the following code snippet:
/* Enable BOTHEDGES interrupts for columns */ PORTC.PIN0CTRL = PORT_ISC_BOTHEDGES_gc; PORTC.PIN1CTRL = PORT_ISC_BOTHEDGES_gc; PORTC.PIN2CTRL = PORT_ISC_BOTHEDGES_gc; PORTC.PIN3CTRL = PORT_ISC_BOTHEDGES_gc;