Author :
Craig
Steiner
Source :
8052.com
Serial
Interrupts
Serial
Interrupts
are
slightly
different
than the
rest of
the
interrupts.
This is
due to
the fact
that
there
are two
interrupt
flags:
RI and
TI. If
either
flag is
set, a
serial
interrupt
is
triggered.
As you
will
recall
from the
section
on the
serial
port,
the RI
bit is
set when
a byte
is
received
by the
serial
port and
the TI
bit is
set when
a byte
has been
sent.
This
means
that
when
your
serial
interrupt
is
executed,
it may
have
been
triggered
because
the RI
flag was
set or
because
the TI
flag was
set--or
because
both
flags
were
set.
Thus,
your
routine
must
check
the
status
of these
flags to
determine
what
action
is
appropriate.
Also,
since
the 8051
does not
automatically
clear
the RI
and TI
flags
you must
clear
these
bits in
your
interrupt
handler.
A brief
code
example
is in
order:
INT_SERIAL: |
JNB RI,CHECK_TI |
;If the RI flag is not set, we jump to check TI |
|
MOV A,SBUF |
;If we got to this line, it’s because the RI bit *was* set |
|
CLR RI |
;Clear the RI bit after we’ve processed it |
CHECK_TI: |
JNB TI,EXIT_INT |
;If the TI flag is not set, we jump to the exit point |
|
CLR TI |
;Clear the TI bit before we send another character |
|
MOV SBUF,#’A’ |
;Send another character to the serial port |
EXIT_INT: |
RETI |
|
As you
can see,
our code
checks
the
status
of both
interrupts
flags.
If both
flags
were
set,
both
sections
of code
will be
executed.
Also
note
that
each
section
of code
clears
its
corresponding
interrupt
flag. If
you
forget
to clear
the
interrupt
bits,
the
serial
interrupt
will be
executed
over and
over
until
you
clear
the bit.
Thus it
is very
important
that you
always
clear
the
interrupt
flags in
a serial
interrupt.