Professional Documents
Culture Documents
Timers /Counters
Timer/Counter 0
Timer/Counter 1
2. An event counter.
External input from input pin to count the
number of events on registers.
These clock pulses represent the number of
people passing through an entrance, or the
number of wheel rotations, or any other event
that can be converted to pulses.
TimersandCounters
Both Timer 0 and Timer 1 are 16 bits wide
Since 8051 has an 8-bit architecture, each 16-bits
timer is accessed as two separate registers of low
byte and high byte.
Accessed as low byte and high byte
The low byte register is called TL0/TL1 And
The high byte register is called TH0/TH1
Accessed like any other register
Contd.
Contd.
8051
12
Timer
P1
to
LCD
TH0
Set
Timer 0
TL0
5
Timer SFRs
As mentioned before, the 8051 has two
timers which each function essentially the same
way. One timer is TIMER0 and the other is
TIMER1.
The two timers share two SFRs (TMOD
and TCON) which control the timers, and each
timer also has two SFRs dedicated solely to itself
(TH0/TL0 and TH1/TL1).
Weve given SFRs names to make it easier to refer to
them, but in reality an SFR has a numeric address.
It is often useful to know the numeric address that
corresponds to an SFR name.
6
Contd.
The SFRs relating to timers are:
Timer SFRs
When you enter the name of an SFR into
an assembler, it internally converts it to a number.
For example, the command:
MOV TH0,#25h
moves the value 25h into the TH0 SFR.
However, since TH0 is the same as SFR address
8Ch this command is equivalent to:
MOV 8Ch,#25h
8
Contd.
10
Gate
Every timer has a mean of starting and stopping.
GATE=0
Internal control
The start and stop of the timer are controlled
by way of software.
Set/clear the TR for start/stop timer.
SETB TR0
CLR
TR0
GATE=1
External control
The hardware way of starting and stopping the
timer by software and an external source.
Timer/counter is enabled only while the INT pin
is high and the TR control pin is set (TR).
Contd.
12
Contd.
15
16
Contd.
18
Contd.
19
Contd.
20
21
22
Example:
1. Indicate which mode and which timer are
selected for the following.
(a) MOV TMOD, #01H
Solution: We convert the value from hex to binary.
We have:
(a) TMOD = 00000001, mode 1 of timer 0 is
selected.
23
Exercise
1. Indicate which mode and which timer are
selected for each of the following.
(a) MOV TMOD, #20H
(b) MOV TMOD, #12H
24
25
Contd.
26
Tcon contd
TR (Timer run control bit)
TR0 for Timer/counter 0; TR1 for Timer/counter 1.
TR is set by programmer to turn timer/counter on/off.
TR=0 : off (stop)
TR=1 : on (start)
TF (timer flag, control flag)
TF0 for timer/counter 0; TF1 for
timer/counter 1.
TF is like a carry. Originally, TF=0.
When TH-TL roll over to 0000 from
FFFFH, the TF is set to 1.
TF=0 : not reach
TF=1: reach
Contd.
As you may notice, weve only defined 4 of
the 8 bits.
Thats because the other 4 bits of the
SFR dont have anything to do with timers--they
have to do with Interrupts .
A new piece of information in this chart is
the column "bit address." This is because this SFR
is "bit-addressable."
28
Contd.
What does this mean? It means if you want to set the bit
TF1--which is the highest bit of TCON--you could execute
the command:
MOV TCON, #80h
or, since the SFR is bit-addressable, you could just execute
the command:
SETB TF1
This has the benefit of setting the high bit of TCON
without changing the value of any of the other bits of the
SFR.
Usually when you start or stop a timer you dont want to
modify the other values in TCON, so you take advantage
of the fact that the SFR is bit-addressable.
29
Contd.
30
Initializing a Timer
As youll recall, we first must decide what mode
we want the timer to be in.
In this case we want a 16-bit timer that runs
continuously; that is to say, it is not dependent on
any external pins.
We must first initialize the TMOD SFR.
Since we are working with timer 0 we will be
using the lowest 4 bits of TMOD. The first two
bits, GATE0 and C/T0 are both 0 since we want the
timer to be independent of the external pins.
31
Initializing a Timer
16-bit mode is timer mode 1 so we must clear
T0M1 and set T0M0.
Effectively, the only bit we want to turn on is bit
0 of TMOD.
Thus to initialize the timer we execute the
instruction:
MOV TMOD,#01h
Timer 0 is now in 16-bit timer mode.
32
Contd.
34
8051 Interrupts
An interrupt is an external or internal event
that disturbs the microcontroller to inform it
that a device needs its service.
Interrupt Sources
Original 8051 has 6 sources of
interrupts
Reset
Timer 0 overflow
Timer 1 overflow
External Interrupt 0
External Interrupt 1
Serial Port events (buffer full, buffer
empty, etc)
EA : Global
enable/disable.
--: Undefined.
ET2 : Enable Timer 2
interrupt.
ES : Enable Serial port
interrupt.
ET1 : Enable Timer 1
interrupt.
EX1 :Enable External 1
By bit operation
EA
ET0
ET1
EX0
EX1
ES
SETB
SETB
SETB
SETB
SETB
IE.7
IE.1
IE.3
IE.0
IE.2
SETB
IE.4
;Enable
;Enable
;Enable
;Enable
;Enable
;Enable
All
Timer0 ovrf
Timer1 ovrf
INT0
INT1
Serial port
By Mov instruction
Recommended in the first of program
MOV IE, #10010110B
Interrupt Priorities
What if two interrupt sources interrupt at
the same time?
The interrupt with the highest PRIORITY
gets serviced first.
All interrupts have a power on default
priority order.
1. External interrupt 0 (INT0)
2. Timer interrupt0 (TF0)
3. External interrupt 1 (INT1)
4. Timer interrupt1 (TF1)
5. Serial communication (RI+TI)
---
PT2
PS
PT1
PX1
PT0
IP.7: reserved
IP.6: reserved
IP.5: Timer 2 interrupt priority bit (8052 only)
IP.4: Serial port interrupt priority bit
IP.3: Timer 1 interrupt priority bit
IP.2: External interrupt 1 priority bit
IP.1: Timer 0 interrupt priority bit
IP.0: External interrupt 0 priority bit
PX0
---
PS
MOV IP , #00000100B
priority order
1.
2.
3.
4.
5.
PT2
PT1
PX1
PT0
Int1
Int0
Timer0
Timer1
Serial
Int1
Timer1
Int0
Timer0
Serial
PX0