RTC - Real-Time Counter

The Real-Time Counter (RTC) is a peripheral that typically runs continuously, including in Low-Power sleep modes, to keep track of time. It offers two timing functions: The Real-Time Counter (RTC) and a Periodic Interrupt Timer (PIT). It can wake up the device from sleep modes and/or interrupt the device at regular intervals.
Figure 1. AVR® Dx - RTC Block Diagram

The RTC peripheral works in Idle and Standby sleep modes. A wide range of resolutions and time-out periods can be configured for it. With a 32.768 kHz clock source, the maximum resolution is 30.5 μs, and time-out periods can be up to two seconds. With a resolution of 1s, the maximum time-out period is more than 18 hours (65536 seconds).

The following code snippets show the initialization of RTC to generate interrupts with a 1-second periodicity (the interrupt code is not included):

AVR® Dx - RTC Initialization for 1s Periodic Interrupt

voidRTC_0_init(){while(RTC.STATUS >0){/* Wait for all register to be synchronized */}
    RTC.PER =0x3FF;/* Period: 0x3FF - 1024 RTC clock cycles */
    RTC.CLKSEL = RTC_CLKSEL_OSC1K_gc;/* 32kHz divided by 32 */
    RTC.INTCTRL =0<< RTC_CMP_bp   /* Compare Match Interrupt enable: disabled */|1<< RTC_OVF_bp;/* Overflow Interrupt enable: enabled */}

In addition to the RTC functionality, the RTC of the AVR Dx devices includes a Periodic Interrupt Timer (PIT) that can run even in Power-Down mode. If enabled, it can generate an interrupt request on every 2n clock period, where n can have any value from 2 to 15, selectable via the RTC.PITCTRLA register. This mode allows periodic interrupts/wake-up from sleep when the device is in Power-Down mode.

Note: For details about using the RTC and code examples, refer to TB3213 - Getting Started with RTC.