Timer Delays

Delaying the application flow by counting clock cycles with the CPU similar to the basic keypad example is inefficient. Instead of holding the CPU to something trivial as counting clock cycles, it can go to sleep to save power or do other useful work.

The ATtiny1627 microcontroller used in this application example features a Real-Time Counter (RTC) which can keep track of delays independent of the CPU. It can run on the internal 32.768 kHz RC oscillator, which means it will run in any sleep mode. The RTC features a Periodic Interrupt Timer (PIT) which can interrupt the CPU from any sleep mode after a set number of RTC clock cycles.

PIT initialization is shown in the code snippet below:

/* Set interrupt to fire every 32 RTC clock cycles and enable PIT */
RTC.PITCTRLA = RTC_PERIOD_CYC32_gc | RTC_PITEN_bm;
/* Enable PIT interrupts */
RTC.PITINTCTRL = RTC_PI_bm;