Professional Documents
Culture Documents
8051 has two 16-bit counter Timer-0 (T0) and Timer-1 (T1). For read write operation
it is accessed as two 8-bit registers TL0/TL1 (lower byte of the timer) and TH0/TH1
(upper byte of the counter. It may be used as a
• Timer : an interval timer
o measure time between two events or generate a delay
o Count the internal clock
• Counter : as an event counter
o Count number of external events
Counts external clock in T0 (P3.4) or T1 (P3.5) that are derived from the
events.
Timers in 8051
• 8051 has two programmable 16-bit timers: T1 and T0.
o 8052 has three 16-bit timers: T2, T1 and T0.
• The upper byte of T1 is called TH1 and Lower byte is called TL1.
• Similarly, T0 consists of TH0 and TL0.
• In general the upper byte and lower byte of timers are denoted as THx and
TLx respectively Where x=0 or 1.
• A 16-bit timer is always accessed as two 8-bit special function register.
o Write or read to THx or TLx.
• Operates in four different mode: Mode0, Mode-1, Mode-2 and Mode-3,
• The timers can be programmed independently in any mode and may operate
as a timer or a counter.
Special Function Register, Timers
• Timer0 (T0)
o TL0 ➔ Address 8AH
o TH0 ➔ Address 8CH
• Timer 1 (T1)
o TL1 ➔ Address 8BH
o TH1 ➔ Address 8DH
Other Special Function Registers associated
Other special function registers associated with timers are
1. Timer Mode register (TMOD)
2. Timer Control register (TCON)
Pin associated with Timers
• P 3.4 (pin 14) ➔ T0, External clock input to event counter, Timer 0
• P 3.5 (pin 15) ➔ T1, External clock input to event counter, Timer 1
• P 3.2 (pin 12) ➔ ̅̅̅̅̅̅̅
𝐼𝑁𝑇0, Control (RUN/STOP) Timer0 when GATE=1
• P 3.3 (pin 13) ➔ ̅̅̅̅̅̅̅
𝐼𝑁𝑇1, Control (RUN/STOP) Timer1 when GATE=1
GATE1 ̅̅̅̅
C/T1 M1 M0 GATE0 ̅̅̅̅
C/T0 M1 M0
TIMER1 TIMER0
0 1 MODE-1
1 0 MODE-2
1 1 MODE-3
To use as timer:
1. Configure interrupt and mode of operation.
2. The timer flag bit is cleared TFx=0.
3. Load THx and TLx with initial count/preset value.
4. Timer RUN bit is set i.e.;TRx=1 to run the timer.
5. If interrupt is disabled, monitor TFx.
6. RUN the service routine when TFx=1.
7. If timer interrupt has been enabled, service routine will run on interrupt
service.
8. In the service routine:
a. Clear TR0 to stop timer
b. To the task if any
c. Clear Timer Flag, TFx
d. Set timer run bit, TRx
e. Go to step 5 to repeat.
Operations as Counter
• Counts the External Pulses at pin Tx (T1 or T0).
o The pulses represent events. For example
o Counts the number of external events occurred
▪ No of people entering a hall
▪ No of pills filled in to a bottle
▪ Counts number of rotations => Speedo meter etc.
• External events are converted to electrical pulses and fed as external
clock through a pin.
o P3.4 (Pin14) ➔ T0 (external clock input for Counter0)
o P3.5 (Pin15) ➔ T1 (external clock input for Counter1)
• ISR can read the count value in a predetermined interval, that can be used
by software.
Modes of Operations
Timers/Counters operates in 4 different modes. The mode is selected by
programming TMOD register.
Timers are programmed independently to operate in any mode.
MODE0
• Both timers can operate in this mode as a 13-bit timer or counter.
• MODE0 is not generally used in 8051. Instead Mode1 is preferred that has
similar operations with a higher maximum count. MODE0 is provided for
backward compatibility with previous Micro-controller 8048.
o That means software written for timers in 8048 will also run on 8051.
• Maximum counting is 213=8192 i.e. FF20H (counts from 0000H to FF1FH).
• Count is maximum when Pre-set Value (Initial Count) loaded is 0000H.
• C/Tx
̅̅̅ bit is used to select the operation as counter or Timer.
• 5 LSB-bits of TLx and all 8 bits of THx is used as 13-bit counter.
C13 C12 C11 C10 C9 C7 C6 C5 X X X C4 C3 C2 C1 C0
THx TLx
• C0, C1…… C13 are counting bits of timer. THx is incremented when TLx
is incremented to 00H.
• Timer overflows when THx goes from FFH to 00H and TFx is set (TFx=1).
• Assuming GATE=0, timer starts running when TRx=1.
• TFx is monitored to detect end of counting if interrupt has not been
enabled.
• Once the timer overflows, the Pre-set Value has to be loaded again to
timer.
• The operation is easily understood from the diagram given below.
Timer Run
Fig: Operation of timer T0 in Mode1. Operation of Timer1 is
similar. Only the flags and signal input pins are different.
• TR0 is the timer run bit. When GATE = 0, the bit controls the operation (RUN
or STOP) of timer.
o TR0=1 runs the timer
o TR0=0 stops the counter as control will be 0 in this case.
• GATE flag is available in TMOD Register.
o GATE=0: Only TR0 flag control the timer as control signal connected to
buffer will be 1.
o GATE=1: Both ̅̅̅̅̅̅̅
𝐼𝑁𝑇0 and TR0 together control the timer.
▪ Timer runs only if both ̅̅̅̅̅̅̅
𝐼𝑁𝑇0 =1 and TR0=1. When any one is 0,
control input to MUX goes low and the clock is inhibited. So,
timer stops.
• ̅̅̅̅̅̅̅
𝐼𝑁𝑇0 is the external signal to RUN or STOP the counter. This is the hardware
control of timer.
• T0 is the external clock input for counter mode.
Mode-2: Auto Reload Mode
• The lower byte of timers TL1 and TL0 operates as 8-bit timer.
• TH1 and TH0 holds the Pre-set Value for TL1 & TL0.
• Maximum count is 28 =256 (100H=FF H +1).
• Pre-set Value is loaded to TH0 or TH1 during Initialisation process.
• The Pre-Set value in TH0 /TH1 is loaded to TL0/TL1 by hardware
automatically.
• When count rolls from FFH to 00H.
o TF0 or TF1 flag is set that generate interrupt if enabled.
o Pre-set Value in TH0 or TH1 is reloaded to TL0 or TL1 by hardware.
o Counting process restarts automatically.
• Timer1 overflow (in any mode) is used to generate Baud rate for serial
communication. But the Mode-2 is most preferable as it avoids the reloading
the Pre-set Value and restart the timer. So easy to generate Baud rate.
• The operation is easily understood from the diagram given below
Additional study
Determination of Pre-set Count
The Pre-set count can be determined as follows. Please note that the maximum
count for:
8-bit timer 28=256 (FF+1=100H)
13-bit timer 213=8192 (1FFF+1=2000H)
16-bit timer 216=65536 (FFFF+1=10000H)
The number of times the timer counts is known as Count.
The number loaded to timer during initialisation is known as Pre-set Count Value.
So, 𝐶𝑜𝑢𝑛𝑡 = 𝑀𝑎𝑥𝑖𝑚𝑢𝑚 𝐶𝑜𝑢𝑛𝑡 – 𝑃𝑟𝑒𝑠𝑒𝑡 𝑉𝑎𝑙𝑢𝑒
1 1 12
Also, Pulse width = 𝐼𝑛𝑝𝑢𝑡 𝑐𝑙𝑜𝑐𝑘 𝑓𝑟𝑒𝑞𝑢𝑒𝑛𝑐𝑦 = 𝑓 =𝑓
( 𝑜𝑠𝑐⁄12) 𝑜𝑠𝑐
12 × 𝐷𝑒𝑙𝑎𝑦
➔ 𝑃𝑟𝑒𝑠𝑒𝑡 𝑉𝑎𝑙𝑢𝑒 = 𝑀𝑎𝑥𝑖𝑚𝑢𝑚 𝐶𝑜𝑢𝑛𝑡 −
𝑓𝑜𝑠𝑐
Programming Timers
Steps
1. Configuring timer/counter using TMOD.
a. Select as counter or timer using 𝐶⁄ ̅ .
𝑇
b. Set Mode of operation with M0 and M1.
c. GATE=0, normally.
2. Load initial count as pre-set value
a. For Mode0, Mode1 and Mode3
i. Timer0
MOV TH0, #MSB_Value
MOV TL0, #LSB_Value
ii. Timer1
MOV TH1, #MSB_Value
MOV TL1, #LSB_Value
b. For Mode2 (Auto-reload Mode)
MOV TH0, #MSB_Value
MOV TH1, #MSB_Value
3. Initialise interrupt system (Enable or Disable as desired)
a. SETB EA ; Enable Interrupt system
b. SETB ET0 ; Enable Timer0 interrupt
c. SETB ET1 ; Enable Timer1 interrupt
d. CLR ET0 ; Disable Timer0 interrupt
e. SETB ET1 ; Disable Timer1 interrupt
4. Start timer/counter through TR0, TR1
a. SETB TR0 , RUN Timer0
b. SETB TR1 ; RUN Timer1
5. if interrupt is disabled poll Timer Flag TFx
a. JNB TF0, $ ; Repeatedly execute till TF0=1
b. Wait: JNB TF0, Wait ; Repeatedly execute till TF0=1
c. JNB TF1, $ ; Repeatedly execute till TF1=1
6. When timer overflows, following task is performed
a. If interrupt is disabled (Polling)
i. Stop Timer
1. CLR TR0
2. CLR TR1
b. Load Pre-set value to Timer (Except in Mode2)
c. Clear interrupt flag (TF0 orTF1) (Except Mode2)
i. CLR TF0
ii. CLR TF1
Example:
Write the necessary instructions that will load timers with Pre-set Value as
follows.
Timer 0 = 2BCDH and ad Timer 1 = 103FH
Solution:
• Not possible to write 16-bit data directly.
• The Timers is loaded by loading to THx & TLx separately.
Instructions that load the desired Pre-set Value
MOV TL0, #CDH
MOV TH0, #2BH
MOV TL1, #3FH
MOV TH1, #10H