Author :
Craig
Steiner
Source :
8052.com
Book :
The
8051/8052
Microcontroller:
Architecture,
Assembly
Language,
And
Hardware
Interfacing
(Paperback)
Timing
the
length
of
events
The 8051
provides
another
cool toy
that can
be used
to time
the
length
of
events.
For
example,
let's
say
we're
trying
to save
electricity
in the
office
and
we're
interested
in how
long a
light is
turned
on each
day.
When the
light is
turned
on, we
want to
measure
time.
When the
light is
turned
off we
don't.
One
option
would be
to
connect
the
lights
witch
to one
of the
pins,
constantly
read the
pin, and
turn the
timer on
or off
based on
the
state of
that
pin.
While
this
would
work
fine,
the 8051
provides
us with
an
easier
method
of
accomplishing
this.
Looking
again at
the TMOD
SFR,
there is
a bit
called
GATE0.
So far
we've
always
cleared
this bit
because
we
wanted
the
timer to
run
regardless
of the
state of
the
external
pins.
However,
now it
would be
nice if
an
external
pin
could
control
whether
the
timer
was
running
or not.
It can.
All we
need to
do is
connect
the
light
switch
to pin
INT0
(P3.2)
on the
8051 and
set the
bit
GATE0.
When
GATE0 is
set
Timer 0
will
only run
if P3.2
is high.
When
P3.2 is
low
(i.e.,
the
light
switch
is off)
the
timer
will
automatically
be
stopped.
Thus,
with no
control
code
whatsoever,
the
external
pin P3.2
can
control
whether
or not
our
timer is
running
or not.
<<< Click
here to
come
back on
(8051
-
Timers)