Sleep

The most important low-power feature that is implemented in the advanced keypad application is putting the MCU to sleep. tinyAVR and megaAVR devices have three available sleep modes. The sleep modes each turn off the clock for different parts of the microcontroller to save power.

When designing an application that uses a sleep mode, one must consider which peripherals and clocks that need to be awake while the CPU sleeps. If, for example, the Real-Time Counter (RTC) should keep track of time while the CPU sleeps, one must select a sleep mode accordingly. Also, the sleep mode must be selected based on how the CPU should wake from sleep. Some interrupt types are unable to wake the device in certain sleep modes. Information regarding peripheral clocks and interrupts in different sleep modes can be found in the Sleep Controller section in the device data sheet.

In the advanced keypad application, no peripheral is needed to be kept running between button presses. Therefore, Power-Down Sleep mode is suitable. Looking at wake-up sources for this sleep mode, one can see that a pin change interrupt will be able to wake the CPU. This is also suitable for the application since a pin change (that is, a button press), is what the application is waiting for. It is important to note that some pin change interrupt types rely on a clock signal to trigger. Since the relevant clock is turned off in Power-Down Sleep mode, an asynchronous (independent of a clock signal) trigger must be chosen for the pin change interrupt. In the application, the BOTHEDGES trigger (as described in Section Interrupt Operation) is chosen.

Power-Down Sleep mode initialization is shown in the following code snippet:

	/* Set Power Down Sleep Mode and enable sleep */
	SLPCTRL.CTRLA = SLPCTRL_SMODE_PDOWN_gc | SLPCTRL_SEN_bm;