4 RTC Periodic Interrupt

This code example shows how to use the PIT timing function of the RTC. The on-board LED will be toggled each second when the periodic interrupt occurs.

The source clock configuration for this particular example is the same as for the RTC overflow interrupt example. Enable the periodic interrupt by setting the PI bit in the RTC.PITINTCTRL register.

Figure 4-1. RTC.PITINTCTRL – Set the PI Bit
RTC.PITINTCTRL = RTC_PI_bm;

The PIT period is set in the RTC.PITCTRLA register. Enable the PIT by setting the PITEN bit in RTC.PITCTRLA.

Figure 4-2. RTC.PITCTRLA – Set the PITEN Bit
RTC.PITCTRLA = RTC_PERIOD_CYC32768_gc | RTC_PITEN_bm;

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: