[Return to Main Page]

One-Shot 6522 Timer Examples from 6502 Applications
[Up to Source Code Repository]

Generating Delays with the 6522's Timers in One-Shot Mode

The reader should study the details of the timers in the manufacturer's data sheets before using them. Timer 2 is simpler than Timer 1. Both timers are not identical, and it is important to understand their specific characteristics before using them. Since a complete study of the timer operating modes is not necessary for implementing them in a typical application, we will show here two typical examples of the generation of delays, using respectively Timer 2 and Timer 1.

Generating a One-Shot Delay with Timer 2

The program appears below:

    ONESHOT2  LDA   #$00
              STA   ACR      ;Select Mode
              STA   T2LL     ;Low-Latch=0
              LDA   #$01     ;Delay Duration
              STA   T2CH     ;High Part=01hex.  Start
              LDA   #$20     ;Mask
    LOOP      BIT   IFR      ;Time out?
              BEQ   LOOP
              LDA   T2CL     ;Clear Timer 2 Interrupt

Bits 6 and 7 of the ACR must be set to zero to specify the one-shot mode (PB7 not used with T2). Since we assume here that none of the other resources such as the shift register are being used, we simply load all zeroes into the ACR register:

    ONESHOT2  LDA   #$00
              STA   ACR

Timer 2, like Timer 1, contains a 16-bit OR so that the two halves of the register must be loaded separately. We will first load the low half, then the high half:

              STA   T2LL
              LDA   #$01
              STA   T2CH

Loading the value $01 into T2CH also results in clearing any interrupt flag and starting the counter automatically. Bit 5 of the IFR is the one indicating that Timer 2 has timed out. Bit 5 of the IF therefore must be tested for the value "1". This is accomplished by the next three instructions:

              LDA   #$20     ;Bit 5 = 1
    LOOP      BIT   IFR
              BEQ   LOOP

The value 20 hexadecimal is equal to "00100000". It is used to test whether bit 5 is indeed a "1". The BIT instruction performs a logical AND, without modifying the contents of the accumulator. As long as bit 5 remains "0", the program loops, waiting for the Timer 2 interrupt. Whenever Timer 2 generates the interrupt, it is detected, and the program exits the loop.

Finally, the program must explicitly clear the Timer 2 interrupt before branching to another task. This could be accomplished by reloading a new value into the counter register. However, since this prgoram should be useful in any environment, we make no asumption as to what will be done after this program terminates. The interrupt falg will be cleared either by writing into T2CH or by reading T2CL. Since we do not want to start the counter running again, we will not write in T2CH, but instead read T2CL, simply to clear the interrupt:

              LDA T2CL

Generating a One-Shot Delay with Timer 1

We will use Timer 1 here in a manner essentially analogous to Timer 2 above. However, Timer 1 is equipped with a true 16-bit latch register, unlike Timer 2. The program appears below:

    ONESHOT1  LDA   #$00
              STA   ACR      ;1-Shot Mode: No PB7 Pulses
              STA   T1LL     ;Low-Latch
              LDA   #$01     ;Delay
              STA   T1CH     ;Loads also T1CL and Starts
              LDA   #$20
    LOOP      BIT   IFR      ;Time Out?
              BEQ   LOOP
              LDA   T1LL     ;Clear Interrupt Flag

The program is essentially analogous to the one above, and should be self explanatory. The only difference is that the low latch is loaded first, then the program writes into T1CH, the high part of the counter proper. This instruction also results in transferring the contents of T1LL into T1CL and starts the counter. The rest of the program is identical.

Generating a Pulse

The above programs will generate a delay for a program. If an actual pulse must be generated, then the proper output pin must be specified. For Timer 1, the PB7 pin will be used to provide the output pulse PB7 will be an output if either DDRB7 or ACR7 equals "1".

Timer 2 does not send a direct pulse on a pin for output. The pulse must be generated by adding instruction which explicitly turn on and off one of the bits of the port. However, Timer 2 may count pulses easily in its pulse-counting mode. Pin PB6 is then used for this purpose. This underlines again the practical differences between these timers. In any practical application, the reader is encouraged to review the manufacturer's data sheets to take best advantage of them.

Last page update: October 17, 2000.