EE 308  Spring 1998

Time Units for Timer Subsystem

With 8 MHz E clock, basic time unit T is 0.125 μs. Prescaler changes time unit for timer module (input capture, output compare, timer overflow) to 0.125μs × prescaler. For example, PR2:0 = 011 => prescaler = 8, so TCNT is incremented every 1 μs. When reading input capture, elapsed time = ΔTCx × prescaler × T.

For example, PR2:0 = 011 and TC2 - TC1 = 12,500, then

elapsed time = 12, 500 × 8 × 0.125μs = 12.5 ms Want OC to do something after time X seconds, what to add to TCx?

\frac{X}{T × prescaler}

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

\frac{500μs}{T × 8} = \frac{500μs}{1μs} = 500

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:

\frac{2ms}{T × 2} = \frac{2000μs}{0.25μs} = 8000

For 80% duty cycle, add 0.80 × 8000 = 6400 to TC1.