Interrupts
are a
very
powerful
tool
available
to the
8051
developer,
but when
used
incorrectly
they can
be a
source
of a
huge
number
of
debugging
hours.
Errors
in
interrupt
routines
are
often
very
difficult
to
diagnose
and
correct.
If you
are
using
interrupts
and your
program
is
crashing
or does
not seem
to be
performing
as you
would
expect,
always
review
the
following
interrupt-related
issues:
Register
Protection:
Make
sure
you
are
protecting
all
your
registers,
as
explained
above.
If
you
forget
to
protect
a
register
that
your
main
program
is
using,
very
strange
results
may
occur.
In
our
example
above
we
saw
how
failure
to
protect
registers
caused
the
main
program
to
apparently
calculate
that
25h
+
10h
=
51h.
If
you
witness
problems
with
registers
changing
values
unexpectedly
or
operations
producing
"incorrect"
values,
it
is
very
likely
that
you’ve
forgotten
to
protect
registers.
ALWAYS
PROTECT
YOUR
REGISTERS.
Forgetting
to
restore
protected
values:
Another
common
error
is
to
push
registers
onto
the
stack
to
protect
them,
and
then
forget
to
pop
them
off
the
stack
before
exiting
the
interrupt.
For
example,
you
may
push
ACC,
B,
and
PSW
onto
the
stack
in
order
to
protect
them
and
subsequently
pop
only
ACC
and
PSW
off
the
stack
before
exiting.
In
this
case,
since
you
forgot
to
restore
the
value
of
"B",
an
extra
value
remains
on
the
stack.
When
you
execute
the
RETI
instruction
the
8051
will
use
that
value
as
the
return
address
instead
of
the
correct
value.
In
this
case,
your
program
will
almost
certainly
crash.
ALWAYS
MAKE
SURE
YOU
POP
THE
SAME
NUMBER
OF
VALUES
OFF
THE
STACK
AS
YOU
PUSHED
ONTO
IT.
Using
RET
instead
of
RETI:
Remember
that
interrupts
are
always
terminated
with
the
RETI
instruction.
It
is
easy
to
inadvertently
use
the
RET
instruction
instead.
However,
the
RET
instruction
will
not
end
your
interrupt.
Usually,
using
a
RET
instead
of a
RETI
will
cause
the
illusion
of
your
main
program
running
normally,
but
your
interrupt
will
only
be
executed
once.
If
it
appears
that
your
interrupt
mysteriously
stops
executing,
verify
that
you
are
exiting
with
RETI.