You are on page 1of 45

Chapter 6

PROGRAMMING THE
TIMERS
External
Start Stop Loa Reset
counting
Reload

d
of inputs
Internal clock Free
Programmabl Running
inputs
e
Pre- Pre-
scaling scaling
Timer-counter
Device

Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005
Lesson 1

Programmable Timer-Counter
Device

Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005
Outline
• Timer-Counter start, stop and reset
• Timer Pre-scaling
• Timer-Counter Loading and Reload
• Counting Time Interval Programming
• Timer-Counter Overflow Events

Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005
Exemplary Use
Finding a short time interval between two
events.
Finding period of a pulse at a port pin
Find time taken for a motor for 1 or more
revolutions
Reset timer T counts = 0, and mode set for
internal clock-inputs. On first event, start
T and second event stop T.
Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005
External gate
8051 T1 in Mode 1 pin INT1
Example and TR1
start/stop
16-bit counter when so
programme
d else by
TR1 only
start/stop.
Count Inputs from
internal clock or from
pin T1
Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005
External
8052 T2 Example CP/RL2 for
counter
reload if
16-bit counter EXEN2 bit
set
TR2 bit
set/reset
Count Inputs from start/stop
internal clock or from programme
pin C/T2 as d
programmed
Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005
Example- 8051 TH0-TL0 Mode 1
Let XTAL clock =12 MHz,therefore
counter clock-input period = 1 µs.
Let Mode T0 is C/T ,internal clock mode
Let T0 be programmed in mode 1
(TH0,TL0) 16-bit counter.
WhenTR0 is set,timer T0 starts and TR0
reset T0 stops.

Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005
Example- 8051/52 16-bit
Counters Resetting
Timer-counter T1 resets on on writing 00H-
00H at TH1-TL1 or on T0 overflow
Timer-counter T0 resets on writing 00H-
00H at TH0-TL0 or on overflow of T0
Timer-counter T2 resets on writing 00H-
00H at TH2-TL2 or on overflow of T2
Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005
68HC11 TCNT Non- Internal clock
input rate only
Programmability programmable

16-bit counter

No TR bit;
Count Inputs from Start/stop and
internal clock reset not
programmable

Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005
Outline
• Timer-Counter start, stop and reset
• Timer Pre-scaling
• Timer-Counter Loading and Reload
• Counting Time Interval Programming
• Timer-Counter Overflow Events

Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005
Pre-scaling
8051 T0 in Mode 0 of TH0 by
Example 32 through
TL0
5-bit TL0 TH0

13-bit counter
Count Inputs from
internal clock or from
pin T1
Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005
Pre scaling of
68HC11 TCNT Example E-clock inputs
by PR0-PR1
bits
16-bit counter programmable
as 1, 4, 8 or 16
with in 64
clock cycles on
power up reset
Count Inputs from
internal E-clock 2 MHz
for 8 MHz
Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005
Example- 68HC11 TCNT
Let XTAL clock = 8 MHz,therefore
counter clock-input period = 0.5 µs.
Let Pre-scaling factor programmed = 8
Therefore, clock-inputs to TCNT at each 4
µs interval
When TCNT = 1FA0H,then after 4∗16µs
TCNT reading will be will be 1FB0H;
after next 1024 µs, 20B0H.
Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005
Outline
• Timer-Counter start, stop and reset
• Timer Pre-scaling
• Timer-Counter Loading and
Reload
• Counting Time Interval Programming
• Timer-Counter Overflow Events

Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005
8051 T0 in Mode 3 TH0 is written
the counts =
Example x0 and TL0 is
TH0 TL0 written the
counts = x1.
TH0
overflows
(256-x0)
Count Inputs from inputs. TL0
internal clock or from times out and
pin T1 overflows
(256-x1)
Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005
inputs.
8051 T1 in Mode 2 TH1 is first
written the
Example counts, x0
TH1 TL1 TL1 auto-
reloads x0
at TH1 on
each
overflow.
Count Inputs from
internal clock or from
pin T1
Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005
Load by
8052 T2 Example writing x at
TH2-TL2.
Times out
16-bit counter and
overflows
after (216 -
x) inputs
Count Inputs from
internal clock or from
pin C/T2 as
programmed
Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005
External
8052 T2 Example CP/RL2 for
counter
reload if
16-bit counter EXEN2 bit
set. Reloads
from the
TH2-TL2
registers
Count Inputs from
internal clock or from
pin C/T2 as
programmed
Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005
Internal clock
68HC11 TCNT Example input rate only
programmable

16-bit counter Read Only

No loading of
Count Inputs from TCNT, No
internal clock resetting by
writing 0000H
into TCNT.
Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005
Example- 8051 TH1-TL1 Mode 1
Let XTAL clock =12 MHz,therefore
counter clock-input period = 1 µs.
Let Mode T1 is C/T ,internal clock mode
Let T1 be programmed in mode 1
(TH1,TL1) 16-bit counter.
WhenTH1-TL1 written (loaded) E0H-01H
then timer T1 will timeout and overflow
after 1FFFH inputs.
Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005
Example- 8051 TL0 Mode 2
Let XTAL clock =12 MHz,therefore
counter clock-input period = 1 µs.
Let Mode T0 is C/T ,internal clock mode
Let T0 be programmed in mode 2; TH0
loads counts,TL0 runs as 8-bit counter.
WhenTH0 (loaded) E0H then timer T0
will timeout and overflow after every 20H
inputs (= 32 µs) as TL0 reloads also from
TH0 on overflow.
Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005
Outline
• Timer-Counter start, stop and reset
• Timer Pre-scaling
• Timer-Counter Loading and Reload
• Counting Time Interval
Programming
• Timer-Counter Overflow Events

Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005
One Timer as Timer and other as counter
In timer mode, a counter-timer device 1
programs the Timer for counting time
interval
When device 1 starts, the counter mode
timer-counter device 2 starts counting.
When device 1 overflows (timeouts), the
ISR stops the device 2 counting.
contd.
Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005
Example- 8051 TH0-TL0 Mode 3
Let XTAL clock =12 MHz,therefore
counter clock-input period = 1 µs.
Let Mode T1 is C/T1= 1 for count mode
TH0 and mode T0 is is C/T0= 0 for
timer mode TL0
Let T0 programmed in mode 3. TH0-TL0
independent counters and TL0 be written
7DH, to set the interval to (100H-7EH) =
83H = 131 µs.
Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education, contd.
2005
8051 TH0-TL0 Mode 3

Step1: Set TR0 = 1, reset TH0 = 00H


Step 2: Run TH0 by setting TR1 =1, so
that TH0 starts counting from 00H.
Step3: TL0 overflows and interrupts, the
ISR for TL0, sets TR1 = 0,it stopsTH0,
therefore counting interval = 131 µs.

Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005
Outline
• Timer-Counter start, stop and reset
• Timer Pre-scaling
• Timer-Counter Loading and Reload
• Counting Time Interval Programming
• Timer-Counter Overflow Events

Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005
Example

Timer-counter T1 mode1 on overflow


after one input from FFFFH and
new reading = 0000H at TH1-TL1

Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005
T0 or T1 or T2 or TCNT
overflow interrupt maskable.
If masked no interrupt service
routine executes on overflow.

Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005
Example- 8051 TH0 Mode 3
Let XTAL clock =12 MHz,therefore
counter clock-input period = 1 µs.
Let Mode T1 is C/T ,internal clock
mode
Let T0 programmed in mode 3. TH0-TL0
independent counters and TH0 be written
81H.
WhenTH0 starts by setting TR1 = 1, Over-
flow will be after (100H-81H) = 127 µs
Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005
Preset time interval ON-OFF of a unit

Load timer T counts = x, and mode set for


internal clock-inputs. Start T . switch ON
and on over flow interrupt, switch OFF.
Application- Output change (s) for a pre-
fixed interval

Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005
Preset long time interval on-off of a unit

Load n-bit timer T counts = x, define


number of overflows = k, mode set for
internal clock-inputs. Switch on an output,
Start T and on overflow interrupt, reload
the counts on 1 to (k-1) th overflows and
switch off the output on k-th over flow
interrupt.
Delay = k.(2n - x).x.p.t, where t = clock
input period, p = pre scaling factor
Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005
Exemplary Applications
Moving robot arm for a defined period.
Output change for a pre-fixed interval
Current output for a fixed interval
A microwave oven ON for a fixed
interval

Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005
Finding a long time interval between
two events.
Reset timer T counts = 0, and mode set for
internal clock-inputs. On first event, start
T and till second event,find k the number
of overflows and on second event stop T.
Exemplary Applications
Find time taken for a weight-lifter to lift
Find time taken for an input event change
Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005
Time interval

Time interval = [(2n.k) +x1].p.t, where t =


clock input period, p = pre scaling factor,
x1 = final counts at the n-bit at T.

Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005
Timer
8051 TL0 timer overflow
mode 2 example interrupt if
not
8-bit counter masked, an
ISR
executes
Example Overflows
after 256
Clock Inputs period = 1 inputs in
µs 256 µs if
for 12 MHz XTAL initial
count bits
Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
all 0s.
2005
Timer
68HC11 TCNT overflow
example interrupt if
not
16-bit counter masked, an
ISR
executes
Example Overflows
after 216
Clock Inputs period = 1/2 inputs in
µs for 8 MHz XTAL, 216 /2 µs
pre-scaling factor set = 1 from
count bits
Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
all 0s.
2005
An overflow delays on pre-scaling
factor >1

Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005
Summary

Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005
Two types of timer-counter devices -
• start,stop, reset and preloading a
count programmable
• Free running timer-counter: start,stop,
reset and preloading counts x,each one
not programmable

Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005
Pre-scaling of timer-counter device -
• Programmable in TCNT 68HC11
• Programmable as 32 in mode-0 at T1
or T0 in 8051
• Pre-scaling delays the overflow rates
and delays increment of counts by the
pre-scaling factor

Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005
Loading of timer-counter device -
• Not Programmable in TCNT 68HC11
• Programmable in T0, T1 or T2 at 8051

Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005
Overflow interrupt(s) of timer-
counter device -
• Initiate an action
• Initiate an action after pre-fixed
number of overflows

Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005
End of Lesson 1

Programmable Timer-Counter
Device

Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005
THANK YOU

Ch06L1-"Microcontrollers....", Raj
Kamal, from Pearson Education,
2005