5 RTC PIT Wake from Sleep

This code example shows how to use the PIT timing function of the RTC to wake up the CPU from sleep. The on-board LED will be toggled each second when the periodic interrupt occurs, meaning it will be toggled when the CPU wakes up from sleep.

The sleep mode is configured in the SLPCTRL.CTRLA register. The sleep feature is enabled by setting the SEN bit in SLPCTRL.CTRLA.

Figure 5-1. SLPCTRL.CTRLA – Set the Sleep Mode, SEN Bit
SLPCTRL.CTRLA |= SLPCTRL_SMODE_PDOWN_gc;
SLPCTRL.CTRLA |= SLPCTRL_SEN_bm;

The CPU can be put to sleep by calling the following function:

sleep_cpu();

The PIT interrupt will wake the CPU from sleep. For the interrupt to occur, the global interrupts must be enabled:

sei();

The Interrupt Service Routine (ISR) for the RTC PIT will toggle an LED in the example below:

ISR(RTC_PIT_vect)
{
    RTC.PITINTFLAGS = RTC_PI_bm;
    LED0_toggle();
}
Note: Clear the PI bit from the RTC.PITINTFLAGS register by writing a ‘1’ to it inside the ISR function.
Tip: The full code example is available in the Appendix section.

An MCC generated code example for AVR128DA48, with the same functionality as the one described in this section, can be found here: