Timers
Timers
Timers
Timers/Counters
Reminder
For the 8052, 2 pins on port 1 also have additional functionality related to timers.
The 8051 has two registers that can be used either as timers or counters.
These are referred to as Timer0 and Timer1.
Usage
Event counting
Determine the number of occurrences of an event. An event is any external stimulus that provides a 1-to-0 transition on a pin of the 8051.
Incrementing
When used as timers, the registers are incremented once per machine cycle.
Each machine cycle is 12 clock cycles.
The count frequency = (system clock frequency) / 12
When used as counters, the registers will be incremented once on every 1-0 (negative edge) on the appropriate input pin.
T0 P3.4 T1 P3.5
The pins must be held high for one complete machine cycle and then low for one complete machine cycle.
Embedded Systems Design
The TMOD register (89H) contains two groups of 4 bits that set the operating mode for Timer 0 and Timer 1. MSB LSB
GATE C/T M1 M0 GATE C/T M1 M0 Timer 1 Bit 7 6 Name GATE C/T Description Gate bit. If set, timer 1 will only increment while INT1 is high. Counter/timer select bit 1 = event counter external timing signal 0 = interval timer internal timing signal Mode bit 1 Mode bit 0 Timer 0 Gate bit Timer 0 counter/timer select bit Timer 0 M1 bit Timer 0 M0 bit Timer 0
5 4 3 2 1 0
M1 M0 GATE C/T M1 M0
Using the M1 and M0 bits of the TMOD register, we can set the timers to operate in one of 4 possible modes:
M1 0 0 1 1 M0 0 1 0 1 Mode Description 0 1 2 3 13-bit timer mode 16-bit timer mode 8-bit auto reload mode Split timer mode:
Timer 0: TL0 is an 8-bit timer controlled by timer 0 mode bits; TH0 the same except controlled by timer 1 mode bits. Timer 1: Stopped.
The TCON register (88H) contains status and control bits for Timer0 and Timer1.
Bit addressable.
MSB TF1 TR1 TF0 TR0 IE1 IT1 IE0 LSB IT0
Description Timer 1 overflow flag Timer 1 run-control bit. Used to turn the timer on/off 0 overflow flag Timer Timer 0 run-control bit. External Interrupt 1 edge flag External Interrupt 1 type flag External Interrupt 0 edge flag External Interrupt 0 type flag
Mode 0/1
The upper 8 bits of the count are in TH The lower 5 bits are in the lower 5 bits of TL. The upper 3 bits of TL are not used
The TFx flag will be set when the counter switches from all 1s to all 0s.
The timer continues to count.
Dr. Bassel Soudan
9
Mode 2
10
Mode 3
Split timer mode. Timer 0 is split into two independent 8-bit timers.
When TL0 overflows, it sets the TF0 flag. When TH0 overflows, it sets the TF1 flag.
11
Clocking Sources
There are two possible clock sources for the timers controlled by the C/T bit of each timer in the TMOD register. If C/T = 0, continuous timer operation is selected and the timer is clocked from by the system clock divided by 12.
The timer is being used for interval timing. Timer overflow occurs after a certain number of cycles depending on the initial value stored in TLx/THx.
If C/T = 1, the timer is clocked from an external source (pin T0 or T1 on port 3).
The timer is being used for event counting. The number of events is stored as a 16-bit hexadecimal value in TLx/THx.
12
The simplest method for starting and stopping the timers is by setting/clearing the TRx bit in TCON.
TRx is cleared after a reset.
It has to be set by software to start the timer.
13
The other possibility is by using the GATE bit of TMOD and the external input INTx.
Setting GATE = 1 allows the timer to be controlled by INTx.
When INTx goes high, the counter is enabled and counts at a rate of system clock/12. When INTx goes low, the counter is disabled.
14
TMOD is the first register initialized since it sets the mode of operation.
MOV TMOD, #00010000B
This sets Timer 1 into mode 1 clocked from the on-chip oscillator.
It is possible to write an ISR that responds when the timer overflows. Then we can enable the appropriate interrupt bit in the IE register and let the microcontroller respond automatically. Or, we can write a wait loop and monitor the timer flag TFx.
WAIT: JNB TF1, WAIT
When the counter reaches FFFFH and turns to 0000H, the TF1 bit will be set and the program will break out of the loop.
Embedded Systems Design
16
When the timer overflows, we need to stop it and then reset the TFx bit so that we dont generate false overflows.
CLR CLR TR1 TF1
17