TIMER2 in 8032 and compatibles

 

Some microprocessors have an additional timer on board : TIMER2.
This section describes the 8032 compatible TIMER2 and is not compatible with th TIMER2 found in the 80C535 and others.
TIMER2 is a 16-bit timer/counter which can operate as either an event timer or an event counter. TIMER2 has three main operating modes : capture, auto-reload(up or down counting) , and baud rate generator.

Capture mode
In the capture mode there are two options :

· 16-bit timer/counter which upon overflowing sets bit TF2, the TIMER2 overflow bit. This bit can be used to generate an interrupt.

Counter mode :
CONFIG TIMER2 = COUNTER, GATE = INTERNAL, MODE = 1

Timer mode:
CONFIG TIMER2=TIMER, GATE= INTERNAL,MODE =1

· As above but with the added future that a 1 to 0 transition on at external input T2EX causes the current values in the TIMER2 registers TL2 and TH2 to be captured into the capture registers RCAP2L and RCAP2H.

Counter mode:
CONFIG TIMER2 = COUNTER, GATE = EXTERNAL, MODE = 1

Timer mode:
CONFIG TIMER2=TIMER,GATE=EXTERNAL,MODE=1

In addition the transition at T2EX causes bit EXF2 in T2CON to be set and EXF2 like TF2 can generate an interrupt.

The TIMER2 interrupt routine can interrogate TF2 and EXF2 to determine which event caused the interrupt.
(there is no reload value in this mode. Even when a capture event occurs from T2EX the counter keeps on counting T2EX pin transitions or osc/12 pulses)

Auto reload mode
In the 16-bit auto reload mode, TIMER2 can be configured as a timer or counter which can be programmed to count up or down. The counting direction is determined by bit DCEN.

TIMER2 will default to counting up to &HFFFF and sets the TF2 overflow flag bit upon overflow. This causes the TIMER2 registers to be reloaded with the 16-bit value in RCAP2L and RCAP2H.
The values in RCAP2L and RCAP2H are preset by software means.

Counter mode:
CONFIG TIMER2=COUNTER,GATE=INTERNAL,MODE=0

Timer mode:
CONFIG TIMER2=COUNTER,GATE=INTERNAL,MODE=0

If EXEN2=1 then a 16-bit reload can be triggered either by an overflow or by a 1 to 0 transition at input T2EX. This transition also sets the EXF2 bit. The TIMER2 interrupt, if enabled, can be generated when either TF2 or EXF2 are 1.

Counter mode:
CONFIG TIMER2=COUNTER,GATE=EXTERNAL,MODE=0

Timer mode:
CONFIG TIMER2=TIMER,GATE=EXTERNAL,MODE=0
TIMER2 can also count up or down. This mode allows pin T2EX to control the direction of count. When a logic 1 is applied at pin T2EX TIMER2 will count up. TIMER2 will overflow at &HFFFF and sets the TF2 flag, which can then generate an interrupt, if the interrupt is enabled. This timer overflow also causes the 16-bit value in RCAP2L en RCAP2H to be reloaded in to the timer registers TL2 and TH2.

Counter mode:
CONFIG TIMER2=COUNTER,GATE=INTERNAL/EXTERNAL,MODE=0,DIRECTION=UP

Timer mode:
CONFIG TIMER2=COUNTER,GATE=INTERNAL/EXTERNAL,MODE=0,DIRECTION=UP


A logic 0 applied at pin T2EX causes TIMER2 to count down. The timer will under flow when TL2 and TH2 become equal to the value stored in RCAP2L and RCAP2H. TIMER2 under flows sets the TF2 flag and causes &HFFFF to be reloaded into the timer registers TL2 and TH2.

Counter mode:
CONFIG TIMER2=COUNTER,GATE=INTERNAL/EXTERNAL,MODE=0,DIRECTION=DOWN

Timer mode:
CONFIG TIMER2=COUNTER,GATE=INTERNAL/EXTERNAL,MODE=0,DIRECTION=DOWN


The external flag TF2 toggles when TIMER2 under flows or overflows.
The EXF2 flag does not generate an interrupt in counter UP/DOWN mode.

Baud rate generator
This mode can be used to generate a baud rate for the serial port. TIMER1 can be used for an other task this way.
CONFIG TIMER2=TIMER,GATE=INTERNAL,MODE=2


Receive only
This mode can be used to generate the baud rate for the receiver only.

TIMER1 can be used for the transmission with an other baud rate.
CONFIG TIMER2=TIMER,GATE=INTERNAL,MODE=3

Note that TIMER1 must be setup from assembler this way.


Transmit only
This mode can be used to generate the baud rate for transmitter only.
TIMER1 can be used for the reception with an other baud rate.
CONFIG TIMER2=TIMER,GATE=INTERNAL,MODE=4

Note that TIMER1 must be setup from assembler this way.

Clock output
Some 8052 deviants have the ability to generate a 50% duty cycle clock on P1.0.

CONFIG TIMER2=TIMER,MODE=5

The output frequency = (fOSC / 4) / (65536-CAPTURE)

Use CAPTURE = value to set the capture register.

How to determine what caused the interrupt
You can test the bit T2CON.7 to see if an overflow caused the interrupt.
You can test bit T2CON.6 whether either a reload or capture is caused by a negative transition on T2EX.

Timer2_ISR:
If T2CON.7 = 1 Then
Print "Timer overflowed"
Else
If T2CON.6 = 1 Then

Print "External transition"
End if
End If
Return