Time Units for Timer Subsystem

With 8 MHz E clock, basic time unit T is 0.125 $\mu$s.

Prescaler changes time unit for timer module (input capture, output compare, timer overflow) to $0.125 \mu{\rm {s}}$$\times$ prescaler.

For example, PR2:0 = 011 => prescaler = 8, so TCNT is incremented every 1 $\mu$s.

When reading input capture, elapsed time = $\Delta {\tt {TCx}}$$\times$prescaler $\times$ T.

For example, PR2:0 = 011 and TC2 - TC1 = 12,500, then
elapsed time =
$12,500 \times 8 \times 0.125 \mu{\rm {s}} = 12.5$ ms

Want OC to do something after time X seconds, what to add to TCx?


\begin{displaymath}\frac{\tt X}{{\tt T} \times {\rm prescaler}}\end{displaymath}

For example, to create 1 kHz square wave, half-period is 500 $\mu$s. If prescaler is 8, then add:


\begin{displaymath}\frac{500 \mu{\rm {s}}}{{\tt T} \times 8} =
\frac{500 \mu{\rm {s}}}{1 \mu{\rm {s}}} = 500\end{displaymath}

and TCx = TCx + 500

For example, to have pulse width modulation on OC1 with 500 Hz frequency and 80% duty cycle, with prescaler = 2:

500 Hz => 2 ms, so add to TC7:


\begin{displaymath}\frac{2 {\rm {ms}}}{{\tt T} \times 2} =
\frac{2000 \mu{\rm {s}}}{0.25 \mu{\rm {s}}} = 8000\end{displaymath}

For 80% duty cycle, add $0.80 \times 8000 = 6400$ to TC1.