View
240
Download
0
Category
Preview:
Citation preview
7/31/2019 Lecture12 Timers
1/20
CpE 213Digital Systems Design
Timers and Counters for 8051
Lectures 12
Tuesday, 3/4/2008
7/31/2019 Lecture12 Timers
2/20
Announcements
Homework #4 is due Today! Test #2 is on March 20, 2008. Same
format as Test #1.
Test # 2 topics: 8051 HardwareSummary, 8051 Instruction Set andAssembly Language.
Seminar Today
Simon Board Schematic is onblackboard.
7/31/2019 Lecture12 Timers
3/20
Todays Class
Overview/Objectives
Timers and Counters for 8051 (Chapter 9)
7/31/2019 Lecture12 Timers
4/20
Timers and Counters for the8051
7/31/2019 Lecture12 Timers
5/20
What is a Timer/Counter?
A counter is a device that generates binarynumbers in a specified count sequencewhen triggered by an incoming clock
pulse. Timers are counters that count pulses. If
the pulses are clock pulses, then the
timers count time.
7/31/2019 Lecture12 Timers
6/20
8051 Block Diagram
Interrupt Control
External Interrupts
CPU
OSC
Timer 1
Timer 0
Serial
PortI/O PortsBus
Control
Counter
Inputs
P0 P3P1
4k byte
ROM128 byte
RAM
P2
(Address/Data)
TXD RXD
7/31/2019 Lecture12 Timers
7/20
Timers/Counter on the 8051
The 8051 comes with two 16-bit timers/counters,both of which may be controlled, set, read, andconfigured individually.
The 8051 timers have three general functions: Keeping time and/or calculating the amount of time
between events (Timer Mode).
Counting the events themselves (Event CounterMode)
Generating baud rates for the serial port. Always count UP irrespective of function as a
timer or a counter.
7/31/2019 Lecture12 Timers
8/20
Difference between a Timer
and a Counter When the incoming clock frequency is known, we cangenerate a fixed period of time known to the designerby setting a preloaded value. This is called a timer.
It is also called an interval timer since it is measuring
the time of the interval between two events. When the incoming clock is irregular and we are only
interested in the number of occurrences of the pulse,this is called a counter. Since we are countingevents, is also known as an event counter.
In 8051, timer gets its clock from the oscillator (crystalthat connected to the CPU) frequency (1/12 of it).
Counter gets the clock from an external pin: P3.4 for timer 0, and
P3.5 for timer 1.
7/31/2019 Lecture12 Timers
9/20
Difference between a Timerand Counter (Cont.)
Timer mode (if the timer control bit is set totimer, the timer uses the system clock)
Increments by 1 every machine cycle.
A single machine cycle consists of 12 crystalpulses.
Event mode (if the timer control bit is set to
event, the timer/counter uses a port bit) Increments by 1 for each pulse on P3.4 for
Timer 0, and P3.5 for Timer 1, respectively.
7/31/2019 Lecture12 Timers
10/20
Applications ofTimers/Counters
Generating time delays. Measuring pulse duration or timing
intervals.
Counting pulses or events.
Generating baud rate clock for the internal8051 serial I/O port.
Generating interrupts.
C C
7/31/2019 Lecture12 Timers
11/20
Timer/Counter ClockingSource
C/T
On-chiposcillator
Crystal
0 = Up (interval timing)
1 = Down (event counting)
Timerclock
Clocking Source
T0 (P3.4) orT1 (P3.5) pin
7/31/2019 Lecture12 Timers
12/20
Group Exercise
Find the timers clock frequency andperiod for various 8051-based systemswith the following crystal frequencies:
12 MHz 16 MHz
7/31/2019 Lecture12 Timers
13/20
Group Exercise
Find the timers clock frequency andperiod for various 8051-based systemswith the following crystal frequencies:
12 MHz 1/12 x 12 MHz = 1 MHz => T = 1 us
16 MHz
1/12 x 16 MHz = 1.333 MHz => T = .75 us
Ti S i l F i
7/31/2019 Lecture12 Timers
14/20
Timer Special FunctionRegisters (SFRs)
As mentioned before, the 8051 has two timers thatessentially function the same way.
One timer is called TIMER0 and the other is TIMER1.
The two timers share two SFRs (TMOD and TCON) that
control the timers. Each timer also has two SFRs dedicated solely to itself
(TH0/TL0 and TH1/TL1).
It is common practice to use given SFR names to refer to
them, but in reality an SFR has a numeric address. When you enter the name of an SFR into an assembler,
it internally converts it to the specified SFR address.
7/31/2019 Lecture12 Timers
15/20
Location of Timer SFRs
SFR Name Description Address
TH0 Timer 0 High Byte 8CH
TL0 Timer 0 Low Byte 8AHTH1 Timer 1 High Byte 8DH
TL1 Timer 1 Low Byte 8BH
TCON Timer Control 88HTMOD Timer Mode 89H
Ti C t l R i t
7/31/2019 Lecture12 Timers
16/20
Timer Control Register(TCON)
TCON has the following functions: Turns Timer 0 and Timer 1 on or off.
Sets trigger type (edge or level) for interrupt 0
and interrupt 1. Sets flags when timer 0 or timer 1 overflow.
7/31/2019 Lecture12 Timers
17/20
Summary of TCON
Only the upper 4 bits are related to timers; the lower 4 bitshave nothing to do with timers - they have to do withinterrupts.
TCON is a bit-addressable register.
Bit SymbolBit
Address DescriptionTCON.7 TF1 8FH Timer 1 overflow flag. Set by hardware upon
overflow; cleared by software, or byhardware when processor vectors to interruptservice routine
TCON.6 TR1 8EH Timer 1 run-control bit. Set/cleared bysoftware to turn timer on/off
TCON.5 TF0 8DH Timer 0 overflow flagTCON.4 TR0 8CH Timer 0 run-control bitTCON.3 IE1 8BH External interrupt 1 edge flag. Set byhardware when a falling edge is detected on
INT1; cleared by software, or by hardwarewhen CPU vectors to interrupt service routine
TCON.2 IT1 8AH External interrupt 1 type flag. Set/cleared bysoftware for falling edge/low-level activatedexternal interrupt
TCON.1 IE0 89H External interrupt 0 edge flagTCON.0
IT0
88H
External interrupt 0 type flag
Ti M d C t l (TMOD)
7/31/2019 Lecture12 Timers
18/20
Timer Mode Control (TMOD)Register
TMOD controls timer 0 and timer 1 functions. The registers upper nibble controls Timer 1 and
the lower nibble controls timer 0. The four bits for each timer control a gate
function, selection of counter or timer, and timermode. The timer or counter function is selected by
control bits C/T; these two timers/counters have4 operating modes, selected by appropriate M0
and M1 bits in the TMOD register. TMOD is not a bit-addressable register. It has to
be manipulated as a byte.
Ti M d (TMOD)
7/31/2019 Lecture12 Timers
19/20
Timer Mode (TMOD)Register Summary
(MSB) (LSB)
GATE TC/ M1 M0 GATE TC/ M1 M0
GATE
TC/
If GATE = 1, timer x will run
only when TRx = 1 and INTx= 1. If GATE = 0, timer x will
run whenever TRx = 1.
Timer mode select. If
TC/ = 1, timer x runs in
counter mode taking its input
from Tx pin. If TC/ = 0 ,
timer x runs in timer modetaking its input from thesystem clock.
M1
0
01
1
1
M0
0
10
1
1
Operating Mode
13-bit timer mode (8048mode).
16-bit timer mode.8-bit auto-reload mode.
THx holds a value
which is to be reloaded
into TLx each time it
overflows.
(Timer 0) TL0 is an 8-bitTimer/Counter controlled
by the standard Timer 0
control bits. TH0 is an8-bit timer only controlled
by Timer 1 control bits.
(Timer 1) Timer/Counter
1 is stopped.
Timer 1 Timer 0
7/31/2019 Lecture12 Timers
20/20
Thursdays Class (March 6, 2008)
Review lecture notes. Read Chapter 9.
Recommended