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 it 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 the 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, the 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 this must clear these bits in the interrupt handler.

                              JNB
INT_SERIAL:RI,CHECK_TI   ; If the RI flag is not set, then jump to check TI

MOV A, SBUF    
If got to this line, its because the RI bit was set

CLR RI
JNB
Clear the RI bit after being processed

CHECK_TI:
TI,EXIT_INT; If the TI flag is not set, Then jump to the exit point
CLR TI; Clear the TI bit before send another character
MOV SBUF, #A; Send another character to the serial port
EXIT_INT:RETI

The 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 forget to clear the interrupt bits, the serial interrupt will be executed over and over until the bit is cleared. Thus it is very important that it always clear the interrupt flags in a serial interrupt.

Comments

Popular posts from this blog

General puprose registers in 8051.

TCON Special Function register.

TMOD Special Function Register.