[Contents] [Main Page]

COUNTERS/TIMERS

There are two independent 16-bit-counter/timers (called Timer 1 and Timer 2) in the R6522. Each timer is controlled by writing bits into the Auxiliary Control Register (ACR) to select this mode of operation (Figure 12).

REG 11 -- AUXILIARY CONTROL REGISTER


Figure 12. Auxiliary Control Register (ACR)

Timer 1 Operation

Interval Timer T1 consists of two 8-bit latches (Figure 13) and a 16-bit counter (Figure 14). The latches store data which is to be loaded into the counter. After loading, the counter decrements at clock rate. Upon reaching zero, an interrupt flag is set, and goes low if the T1 interrupt is enabled. Timer 1 then disables any further interrupts or automatically transfers the contents of the latches into the counter and continues to decrement. In addition, the timer may be programmed to invert the output signal on a peripheral pin (PB7) each time it "times-out". Each of these modes is discussed separately below.

Note that the processor does not write directly into the low-order counter (T1C-L). Instead, this half of the counter is loaded automatically from the low order latch (T1L-L) when the processor writes into the high order counter (T1C-H). In fact, it may not be necessary to write to the low order counter in some applications since the timing operation is triggered by writing to the high order latch.


REG 6 ÄÄ T1 LOW-ORDER LATCH REG 7 ÄÄ T1 HIGH-ORDER LATCH WRITE - 8 BITS LOADED INTO T1 WRITE - 8 BITS LOADED INTO T1 HIGH- LOW-ORDER LATCHES. THIS ORDER LATCHES. UNLIKE REG 4 OPERATION IS NO DIFFERENT OPERATION NO LATCH TO THAN A WRITE INTO REG 4 COUNTER TRANSFERS TAKE PLACE READ - 8 BITS FROM T1 LOW ORDER- READ - 8 BITS FROM T1 HIGH-ORDER LATCHES TRANSFERRED TO MPU. LATCHES TRANSFERRED TO MPU UNLIKE REG 4 OPERATION, THIS DOES NOT CAUSE RESET OF T1 INTERRUPT FLAG

Figure 13. Timer 1 (T1) Latch Registers



REG 4 ÄÄ T1 LOW-ORDER COUNTER REG 5 ÄÄ T1 HIGH-ORDER COUNTER WRITE - 8 BITS LOADED INTO T1 WRITE - 8 BITS LOADED INTO T1 LOW-ORDER LATCHES. LATCH HIGH-ORDER LATCHES. ALSO CONTENTS ARE TRANSFERRED AT THIS TIME BOTH HIGH- AND INTO LOW-ORDER COUNTER AT LOW-ORDER LATCHES TRANSFERRED THE TIME THE HIGH-ORDER INTO T1 COUNTER. T1 INTERRUPT COUNTER IS LOADED (REG 5) FLAG ALSO IS RESET READ - 8 BITS FROM T1 LOW-ORDER READ - 8 BITS FROM T1 HIGH-ORDER COUNTER TRANSFERRED TO MPU. COUNTER TRANSFERRED TO MPU IN ADDITION T1 INTERRUPT FLAG IS RESET (BIT 6 IN INTERRUPT FLAG REGISTER)

Figure 14. Timer 1 (T1) Counter Registers


Timer 1 One-Shot Mode

The Timer 1 one-shot mode generates a single interrupt for each timer load operation. As with any interval timer, the delay between the "write T1C-H" operation and generation of the processor interrupt is a direct function of the data loaded into the timing counter. In addition to generating a single interrupt, Timer 1 can be programmed to produce a single negative pulse on the PB7 peripheral pin. With the output enabled (ACR7=1) a "write T1C-H" operation will cause PB7 to go low. PB7 will return high when Timer 1 times out. The result is a single programmable width pulse.

T1 interrupt flag will be set, the pin will go low (interrupt enabled), and the signal on PB7 will go high. At this time the counter will continue to decrement at system clock rate. This allows the system processor to read the contents of the counter to determine the time since interrupt. However, the T1 interrupt flag cannot be set again unless it has been cleared as described in this specification.

Timing for the R6522 interval timer one-shot mode is shown in Figure 15.


Figure 15. Timer 1 One-Shot Mode Timing

In the one-shot mode, writing into the T1L-H has no effect on the operation of Timer 1. However, it will be necessary to assure that the low order latch contains the proper data before initiating the count-down with a "write T1C-H" operation. When the processor writes into the high order counter (T1C-H), the T1 interrupt flag will be cleared, the contents of the low order latch will be transferred into the low order counter, and the timer will begin to decrement at system clock rate. If the PB7 output is enabled, this signal will go low on the following the write operation. When the counter reaches zero, the T1 interrupt flag will be set, the pin will go low (interrupt enabled), and the signal on PB7 will go high. At this time the counter will continue to decrement at system clock rate. This allows the system processor to read the contents of the counter to determine the time since interrupt. However, the T1 interrupt flag cannot be set again unless it has been cleared as described in this specification.

Timer 1 Free-Run Mode

The most important advantage associated with the latches in T1 is the ability to produce a continuous series of evenly spaced interrupts and the ability to produce a square wave on PB7 whose frequency is not affected by variations in the processor interrupt response time. This is accomplished in the "free-running" mode.

In the free-running mode, the interrupt flag is set and the signal on PB7 is inverted each time the counter reaches zero. However, instead of continuing to decrement from zero after a time-out, the timer automatically transfers the contents of the latch into the counter (16 bits) and continues to decrement from there. The interrupt flag can be cleared by writing T1C-H, by reading T1C-L or by writing directly into the flag as described later. However, it is not necessary to rewrite the timer to enable setting the interrupt flag on the next time-out.

All interval timers in the R6522 are "re-triggerable". Rewriting the counter will always re-initialize the time-out period. In fact, the time-out can be prevented completely if the processor continues to rewrite the timer before it reaches zero. Timer 1 will operate in this manner if the processor writes into the high order counter (T1C-H). However, by loading the latches only, the processor can access the timer during each down-counting operation without affecting the time-out in process. Instead, the data loaded into the latches will determine the length of the next time-out period. This capability is particularly valuable in the free-running mode with the output enabled. In this mode, the signal on PB7 is inverted and the interrupt flag is set with each time-out. By responding to the interrupts with new data for the latches, the processor can determine the period of the next half cycle during each half cycle of the output signal on PB7. In this manner, very complex waveforms can be generated.

A precaution to take in the use of PB7 as the timer output concerns the Data Direction Register contents for PB7. Both DDRB bit 7 and ACR bit 7 must be 1 for PB7 to function as the timer output. If one is 1 and the other is 0, then PB7 functions as a normal output pin, controlled by ORB bit 7.


Figure 16. Timer 1 Free-Run Mode Timing

Timer 2 Operation

Timer 2 operates as an interval timer (in the "one-shot" mode only), or as a counter for counting negative pulses on the PB6 peripheral pin. A single control bit in the Auxiliary Control Register selects between these two modes. This timer is comprised of a "write-only" low-order latch (T2L-L), a "read-only" low-order counter (T2C-L) and a read/write high order counter (T2C-H). The counter registers act as a 16-bit counter which decrements at rate. Figure 17 illustrates the T2 Latch/Counter Registers.



REG 8 Ä T2 LOW-ORDER LATCH/COUNTER REG 9 Ä T2 HIGH-ORDER COUNTER WRITE - 8 BITS LOADED INTO T2 WRITE - 8 BITS LOADED INTO T2 LOW-ORDER LATCH HIGH-ORDER COUNTER. ALSO, LOW-ORDER LATCH TRANSFERRED READ - 8 BITS FROM T2 LOW-ORDER TO LOW-ORDER COUNTER. IN COUNTER TRANSFERRED TO MPU. ADDITION T2 INTERRUPT FLAG T2 INTERRUPT FLAG IS RESET IS RESET READ - 8 BITS FROM T2 HIGH-ORDER COUNTER TRANSFERRED TO MPU
Figure 17. Timer 2 (T2) Latch/Counter Registers

Timer 2 One-Shot Mode

As an interval timer, T2 operates in the "one-shot" mode similar to Timer 1. In this mode, T2 provides a single interrupt for each "write T2C-H" operation. After timing out, the counter will continue to decrement. However, setting of the interrupt flag is disabled after initial time-out so that it will not be set by the counter decrementing again through zero. The processor must rewrite T2C-H to enable setting of the interrupt flag. The interrupt flag is cleared by reading T2C-L or by writing T2C-H. Timing for this operation is shown in Figure 18.


Figure 18. Timer 2 One-Shot Mode Timing

Timer 2 Pulse Counting Mode

In the pulse counting mode, T2 counts a predetermined number of negative-going pulses on PB6. This is accomplished by first loading a number into Timer 2. Writing into T2C-H clears the interrupt flag and allows the counter to decrement each time a pulse is applied to PB6. The interrupt flag is set when T2 counts down past zero. The counter will then continue to decrement with each pulse on PB6. However, it is necessary to rewrite T2C-H to allow the interrupt flag to set on a subsequent time-out. Timing for this mode is shown in Figure 19. The pulse must be low on the leading edge of .


Figure 19. Timer 2 Pulse Counting Mode


[Contents] [Main Page]