This document discusses timers on the 8051 microcontroller. It covers:
1. The 8051 has two 16-bit timers, T0 and T1, that can operate in different modes set by the TMOD register to function as timers or counters.
2. The timers use two 8-bit registers each, TL and TH, to store the 16-bit timer value. They are clocked by the system clock divided by 12.
3. Timer mode 1 is a 16-bit timer where the TF flag is set when the timer rolls over from 0xFFFF to 0x0000, which can trigger an interrupt. Timers can generate waveforms and measure time intervals.
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
8051 Timers Class - Modes, Counting, and Time Measurement
1. Pari vallal Kannan
Center for Integrated Circuits and Systems
University of Texas at Dallas
8051 Timers
Class 7
EE4380 Fall 2002
2. 24-Sep-02 2
Introduction
l Timers
– Timing devices - Generate specific time delay
– Event counter - Count the occurrence of an
(external) event
l 8051 has Two 16-bit timers T0 and T1
l 8051 Timers can operate in many modes
– Timer mode is controlled by TMOD register
– Timers are controlled by TCON register (upper 4
bits)
l Clock source for the timer is sys_clk/12
3. 24-Sep-02 3
Timer Registers
l 8051 timers are made up of two 8-bit registers
each
l Timer 0 Registers
– TL0 and TH0
– Accessed like any other register
l mov TL0, #55H, mov R1, TH0
l Timer 1 Registers
– TL1 and TH1
4. 24-Sep-02 4
8051 Timer : TMOD Register
l Gate - Gating Control (how to start-stop timer)
– 0 à software gating (TRx bit in TCON register)
– 1 à hardware gating (INTx pin)
l C/T - Counter/Timer Operation
– 0 à Timer operation (clock is sysclk/12)
– 1 à Counter operation (clock is T0 or T1 pin)
l M1:M0 - Mode control
– 00 à Mode 0 (13bit timer)
– 01 à Mode 1 (16 bit timer)
– 10 à Mode 2 (8 bit timer, with auto-reload)
– 11 à Mode 3 (split timer)
M0M1C/T0Gate0M0M1C/T1Gate1
5. 24-Sep-02 5
Timer: Mode –1
l 16 bit timer
– Load the timer with a number and set TR, to start
counting
– When the counter rolls over to 0x0000, it sets the
TF flag and raises the TF interrupt if enabled
XTAL
TR
TH TL TF
TF=1 if
TH:TL goes from
oxFFFF to 0x0000
oscillator divide by 12
6. 24-Sep-02 6
Timer – Mode 1
l Algorithm
1. Load TMOD register to set mode
2. Load TLx and THx with the initial count values
3. Start timer (setb TRx)
4. Keep monitoring TFx flag (jnb TFx, target)
5. Stop timer (clrb TRx) and clear TFx flag
6. Go back to step 2 to load again
l Time spent
– Telapsed = (65536 - intial_value)*cycle_time
l Instead of polling for TFx flag, an ISR could be used
7. 24-Sep-02 7
Timer : Mode –1 Example
l Generate a
50% duty
cycle
square
wave on
P1.5, with
Timer0
mov TMOD, #01 ;Timer 0, mode 1
Here: mov TL0, #0F2H
mov TH0, #0FFH ;Initial Value = FFF2H
cpl P1.5
acall delay
sjmp Here
Delay: setb TR0 ;start Timer0
Again: jnb TF0, Again ;poll for TF0 (timer overflow)
clr TR0 ;stop timer
clr TF0 ;clear TF0 flag
RET
8. 24-Sep-02 8
Timers and Interrupts
l On timer overflow
interrupt TF1 or
TF0 is generated
l Enable the interrupt
and then start the
timer
l ISR automatically
clears the TFx flag
Program to generate a square wave on
Pin1.2
org 0x0000
ljmp MAIN
org 0x000B ;ISR for T0
cpl P1.2 ;toggle P1.2
mov TL0, #0x55 ;reload T0
mov TH0, #0xAA
reti
org 0x0030
main: mov TMOD, #0000 0001B ;T0, Mode1
mov TL0, #0x55 ;load T0
mov TH0, #0xAA
mov IE, #0x82 ;enable T0 interrupt
setb TR0 ;start T0
here: sjmp here ;wait here until interrupted
9. 24-Sep-02 9
Timer for Time Measurement
l Timer are used to measure elapsed time
– Useful for scheduling routine tasks
– Similar to “cron” functionality
– Not as accurate as an RTC, but cheap !
l Timer’s clock is 1/12 of the 8051 clock
– Time period for one “count” is = 1.085us
– Time spent for a count sequence to “roll-over” is
l Number of counts x 1.085us
l Ex: Timer loaded with 0xFFF2
– Number of counts to rollover to 0x0000 is
0xFFFF-0xFFF2 +1 = 14
– Time elapsed = 14 x 1.085us
Timer0 sequence
FFF2 TF=0
FFF3 TF=0
FFF4 TF=0
….
….
FFFE TF=0
FFFF TF=1
10. 24-Sep-02 10
Time measurement (contd.)
l How to calculate the initial load values for a given time
delay requirement T ?
– Divide T by 1.085us to get n (assumed 11.0592MHz crystal)
– Find m = 65536 – n
– Convert m to hex, m = 0xUUVV
– Load TH ß 0xUU and TL ß 0xVV
l For larger delays ?
– Repeat inside a loop
– Introduce a number of additional instructions (nop), before
enabling the timer again
– Go for an RTC
11. 24-Sep-02 11
Timer : Other modes
l Mode 0
– Exactly like Mode1, but it is a 13bit timer
– Count sequence is from 0x0000 to 0x1FFF
l Mode 2
– 8 bit timer, with auto reload
– Load the count value in TH and enable the timer
– 8051 loads TL with TH (TL ß TH)
– When TL rolls-over to 0x00, timer raises TF flag (and interrupt)
– After TF flag is cleared by ISR / code, TL is automatically
reloaded with TH again and the cycle repeats
– Used in serial communication as baud rate generator
12. 24-Sep-02 12
Timers as Counters
l Counters count how many times a particular event has
occurred
– How many 1’s in a bit stream ?
– How many widgets passed the sensor in an assembly line ?
– How many dogs walked past my doggie door ?
l Counters increment their count when they receive a
signal (count pulse)
l 8051 timers can serve as counters
– C/T bit in TMOD reg has to be 1 for counter operation
– Two external pins on 8051 to give the count pulses
l P3.4 (T0, pin 14) : external count pulse for Timer0
l P3.5 (T1, pin 15) : external count pulse for Timer1
13. 24-Sep-02 13
Counter Example
l Count the pulses on pin T1 (P3.5) and display
the counter value on P2. Counter is in mode 2
START: mov TMOD, #01100000B ;counter 1, mode 2, C/T=1
mov TH1, #0 ;count from 0x00 to 0xFF
setb P3.5 ;configure P3.5 as input
AGAIN: setb TR1 ;enable counter
BACK: mov A, TL1 ;read TL1 value
mov P2, A ;display it on P2
jnb TF1, back ;poll for TF1, could use INT1 also
clr TR1 ;stop counter
clr TF1 ;clear TF1 flag
sjmp AGAIN ;while(1)
14. 24-Sep-02 14
Timers : External Gate
l External gate provides the facility of controlling the
timer with an external device
– Push buttons maybe used to enable/disable timer
– Snooze button in an 8051 based clock !
l Set GATE=1 in TMOD, then the timer can be controlled
from an external pin
– Pin P3.2 (INT0) for Timer0
– Pin P3.3 (INT1) for Timer1
l With GATE=1, Timer is enabled iff
– TRx is set by software (setb TR0)
– AND, INT0 (Pin P3.2) has to be pulled HIGH by hardware