Setting Up the Timers

This section shows concrete examples for how to set up the different timers. The data sheet and the application notes listed in the final section of this document should be read in addition. Especially when transforming the settings to other parts than the ATmega328PB.

Using interrupts is the most common way to react on timer events. The examples described in the following use interrupts.

Independent of the different features of these timers, they all have two things in common. The timer has to be started by selecting the clock source and, if interrupts are used, they have to be enabled.

Shared Registers

If the same registers are used in the interrupt service routines as in the main code, these registers have to be saved at the beginning of the ISR and restored at the end of the ISR. If not all the 32 registers are needed in the application, the save and restore operations can be avoided by using separate registers in the main code and the ISR.

It is also very important to remember to store the Status Register (SREG), as this is not automatically done by the interrupt handler.

Note: The C compiler handles this automatically. If assembly language is used, it has to be done manually by using push and pop instructions.