3 Theory

For a timing-critical application, there is no way to avoid the requirement for an external crystal to make the crystal oscillator circuit operational, but it is possible to do something about its power consumption. Instead of allowing the crystal oscillator to run continuously, it is possible to just enable the crystal oscillator on an occasional basis and use it to precisely measure the frequency of the internal ULP oscillator. Then the crystal oscillator can be disabled while the ULP oscillator continues to run. As long as the ULP oscillator is reasonably stable until the next measurement, it is possible to keep accurate time using it because its frequency is precisely known. Fortunately, AVR devices provide additional internal modules that make it feasible to implement this approach.

On the ATtiny416 device used in this example, there are two key modules that make this feasible; the Real-Time Counter (RTC) and the 16-bit Timer/Counter Type B (TCB). With appropriate setup, the RTC can be clocked by the internal ULP oscillator to generate a periodic interrupt/event every n ULP oscillator cycles, where n is configurable. The TCB can count the number of crystal oscillator cycles between successive interrupts/events from the RTC, thereby providing a precise measurement of the internal ULP oscillator timing.

Once the precise measurement of the internal ULP oscillator is available, it can be used to update a time counter every time a ULP oscillator interrupt is received, instead of updating the time counter with a nominal value for the ULP oscillator.