12
AVR1306
5 Using two 16-bit Timer/Counters as one 32-bit Timer/Counter
It is possible to use the event system to cascade two 16-bit Timer/Counter modules
into one 32-bit Timer/Counter with 32-bit input capture functionality.
5.1 Cascading two 16-bit Timer/Counter Modules
All Timer/Counter modules have the option of being clocked by one of the event
channels. To cascade two 16-bit TC modules and thus get a 32-bit TC, the overflow
event from the low word TC must be routed through the event system and used as
the clock for the high word TC. Figure 5-1 shows how this is done using TCC0 as low
word and TCC1 as high word, through event channel 0. Event channel 0 is configured
through the EVSYS.CH0MUX register to use the TCC0 overflow event as event
source. The CLKSEL bits in TCC1.CTRLA are configured to use event channel 0 as
clock source. The result is that TCC1 will increase by one each time TCC0 overflows.
In effect, this creates a 32-bit TC. Note that any two TC modules can be cascaded in
this way.
Figure 5-1.
Cascading two 16-bit Timer/Counter modules.
TCC1H
TCC1L
TCC0H
TCC0L
High word
Overflow
OVF
EVMUX0
Low word
OVF
Clock
signal for
high word
T/C
5.2 Accessing the 32-bit Value
When reading the cascaded TC, the 32-bit value must be manually assembled from
the two 16-bit parts. However, since there is no temporary register for the 32-bit
counter value, the high and low words will not be read in the same clock cycle. The
low word TC might overflow just after it has been read. This would lead to an incorrect
value being read, since the overflow of the low word would lead the high word to be
increased by 1. One way to avoid this is to stop the low word TC while reading the 32-
bit count value. If stopping the TC is not acceptable, the best solution is to use a 32-
bit input capture.
The same problems can occur when a new value is written to the cascaded TC. The
safest way to write a new 32-bit value is to stop the TC, write the value and start the
TC again.
5.3 Using 32-bit Input Capture
A 32-bit Timer/Counter can be set up to perform 32-bit input capture. First, a 32-bit
Timer/Counter must be set up as described in section 5.1. Both TCs are configured
8045A-AVR-02/08