Using RTC and Event System

In this example, the Periodic Interrupt Timer (PIT) functionality of the RTC is used together with the Event System. In this case, the system clock will not have to be switched to a slow oscillator. Refer to Get Source Code from Atmel | START for a link to the project.

Periodic events generated in the PIT can be output using the Event System. As seen above, there are many different periods available. In this example the lowest period PIT event output is used, as can be seen by the line EVSYS_ASYNCCH3 = EVSYS_ASYNCCH3_PIT_DIV64_gc;. With the division of the RTC clock frequency set to 64, this will generate a pulse on the selected output pin with a period of 1.95 ms.

Execute the protected write for enabling the 32.768 kHz crystal as early as possible. To better utilize time, it is recommended to perform other production tests while the crystal stabilizes. The below code shows how to setup the PIT as an event generator, and how to output the signal on a pin:

     //Deactivate change protection for register Start XOSC32K
    //here start-up time is set to the maximum 64K cycles (~2 seconds)
    _PROTECTED_WRITE(CLKCTRL_XOSC32KCTRLA , CLKCTRL_ENABLE_bm\
    | CLKCTRL_CSUT_64K_gc | CLKCTRL_RUNSTDBY_bm);

    // Use event system channel 3 as path for the PIT event
    EVSYS_ASYNCCH3 = EVSYS_ASYNCCH3_PIT_DIV64_gc;
    // Connect channel 3 to async user 8 (evout0)
    EVSYS_ASYNCUSER8 = EVSYS_ASYNCUSER8_ASYNCCH3_gc;
    // muxout evout0 to PA2
    PORTMUX_CTRLA = PORTMUX_EVOUT0_bm;

    // Enable PIT in RTC
    RTC_PITCTRLA = RTC_PITEN_bm;
    // Set crystal as clock source for RTC
    RTC_CLKSEL = RTC_CLKSEL_TOSC32K_gc;
    // Enable RTC
    RTC_CTRLA = RTC_RTCEN_bm;