Skip to main content

5. Delay Using Timers

5.1. Delay Calculation in Normal Mode

In Normal Mode, the timer always counts up to its maximum value and then overflows. To get a specific delay, you preload the TCNTn register with a starting value so it only has to count a specific number of steps before overflowing.

Here is the formula to find the required preload value:

For example, if we want to create a 1ms delay using Timer0 with 1/64 presacaler:

5.2. Delay Calculation in CTC Mode

In CTC (Clear Timer on Compare) Mode, the timer is much easier to use for delays because the hardware automatically resets the counter to zero when it reaches a target value stored in the OCRnx (Output Compare Register).

Here is the formula to find the value to put in OCRnx register:

For example, if we want to create a 1ms delay using Timer0 with 1/64 presacaler: