Interrupts
Interrupts
Introduction
8051 Interrupt organization
Processing Interrupts
Program Design Using Interrupts
Timer Interrupts
Serial Port Interrupts
External Interrupts
Interrupt Timings
Interrupt ISR
level execution
ISR ISR
1
IT1 IE1
INT1 Interrupt
0 polling
sequence
TF1
RI
TI
TF2
EXF2
Interrupt
enables
Accent
Global Enable interrupt
110/11/02 T. L. Jong, Dept. of E.E., NTHU 9
Processing Interrupts
When an interrupt occurs and is accepted by the
CPU, the main program is interrupted. The
following actions occur:
The current instruction completes execution.
The PC is saved on the stack.
The current interrupt status is saved internally.
Interrupts are blocked at the level of the interrupt.
The PC is loaded with the vector address of the ISR
The ISR executes.
The ISR finishes with an RETI instruction, which
retrieves the old value of PC from the stack and
restores the old interrupt status. Execution of the
main program continues where it left off.
110/11/02 T. L. Jong, Dept. of E.E., NTHU 10
Interrupt Vectors
Interrupt vector = the address of the start of the
ISR.
When vectoring to an interrupt, the flag causing
the interrupt is automatically cleared by
hardware. The exception is RI/TI and TF2/EXF2
which should be determined and cleared by
software.
Interrupt Flag Vector Address
System Reset RST 0000H (LJMP 0030H)
External 0 IE0 0003H
Timer 0 TF0 000BH
External 1 IE1 0013H
Timer 1 TF1 001BH
Serial Port RI or TI 0023H
Timer 2 TF2 or EXF2 002BH
110/11/02 T. L. Jong, Dept. of E.E., NTHU 11
Program Design Using Interrupts
I/O event handling:
Polling: main program keeps checking
the flag, waiting for the occurrence of
the event. Inefficient in some cases.
Interrupt-driven: CPU can handle other
things without wasting time waiting for
the event. Efficient, prompt if ISR is not
so complex. Suitable for control
application.
I/O processor: dedicated processor to
handle most of the I/O job without CPU
intervention. Best but most expensive.
110/11/02 T. L. Jong, Dept. of E.E., NTHU 12
8051 Program Design Using
Interrupt
ORG 0000H ;reset entry point
LJMP Main ;takes up 3 bytes
ORG 0003H ;/INT0 ISR entry point
. ;8 bytes for IE0 ISR or
. ; jump out to larger IE0 ISR
ORG 000BH ;Timer 0 ISR entry point
.
.
ORG 0030H ;main program entry point
Main: .
.
.
110/11/02 T. L. Jong, Dept. of E.E., NTHU 13
Small Interrupt Service
Routine
8 bytes for each interrupt vector. Small ISR
utilizes the space.
For example: (assume only T0ISR is
needed in the case)
ORG 0000H
LJMP MAIN
ORG 000BH
T0ISR: .
.
RETI
MAIN: . ;only T0ISR
110/11/02 T. L. Jong, Dept. of E.E., NTHU 14
Large Interrupt Service
Routine
8 bytes not enough. Use LJMP to large ISR
ORG 0000H
LJMP MAIN
ORG 000BH ; T0 ISR entry point
LJMP T0ISR
ORG 0030H ;above int vectors
MAIN: .
.
T0ISR: . ; Timer 0 ISR
.
RETI ;return to main
110/11/02 T. L. Jong, Dept. of E.E., NTHU 15
A 10-KHz Square Wave on
P1.0 Using Timer Interrupts
ORG 0 ;reset entry point
LJMP Main
ORG 000BH ;T0 interrupt vector
T0ISR: CPL P1.0 ;toggle port bit
RETI
ORG 0030H
Main: MOV TMOD,#02H ;T0 MODE 2
MOV TH0,#-50 ;50 S DELAY
SETB TR0 ;START TIMER 0
MOV IE,#82H ;ENABLE T0 INT
SJMP $ ;DO NOTHING
The CPU speed is much higher than 1200 baud serial transmission.
Therefore, SJMP executes a very large percentage of the time.
Time for one char = (1/1200 baud)(8+1+1) = 8333.3 S compared to
1 S machine cycle!
We could replace SJMP instruction with other useful instructions
doing other things.
110/11/02 T. L. Jong, Dept. of E.E., NTHU 21
#include “io51.h”
char *ptr;
void InitialUART(int BaudRate) /*Max baudrate = 9600*/
{
SCON = 0x52;
TMOD = 0x21;
TH1 = 256-(28800/BaudRate); /*11.059M/384=28800*/
TR1 = 1;
}
static const char msg1[]=“UART interrupt message!!”;
void main(void)
{
InitialUART(9600);
EA = 1;
ptr = msg1;
ES = 1;
while(1); /*wait for SP interrupt*/
}
110/11/02 T. L. Jong, Dept. of E.E., NTHU 22
interrupt [0x23] void SCON_int(void) /*Serial port ISR*/
{
if(RI==1)RI=0; /* we did nothing in this program for RxD */
if(TI==1)
{
TI=0;
if(*ptr!=‘\0’) /*string ends with ‘\0’ character*/
{
SBUF=*ptr;
++ptr;
}
else
{
ES=0; /*complete a string tx, clear ES and let*/
TI=1; /*main program decide next move */
}
}
}
110/11/02 T. L. Jong, Dept. of E.E., NTHU 23
External Interrupts
/INT0 (P3.2 or pin12) and /INT1 (P3.3 or pin 13)
produce external interrupt in flag IE0 and IE1 (in
TCON) in S5P2.
/INT0 and /INT1 are sampled once each machine
cycle (S5P2) and polled in the next machine cycle.
An input should be held for at least 12 clock cycles
to ensure proper sampling.
Low-level trigger (IT0 or IT1 =0): interrupt when
/INT0 or /INT1 = 0
Negative edge trigger (IT0 or IT1 = 1): interrupt if
sense high on /INT0 or /INT1 in one machine cycle
and low in next machine cycle.
IE0 and IE1 are automatically cleared when CPU is
vectored to the ISR.
T = 21C
T = 20C
T = 19C
HOT
COLD
P1.7
INT0 P1.7
Door opens
1 sec
P1.7
P1.7 400Hz
1.25 ms
2.5 ms
110/11/02 T. L. Jong, Dept. of E.E., NTHU 28
Intrusion Warning System
ORG 0
LJMP Main
LJMP EX0ISR
ORG 0BH
LJMP T0ISR ;use T0 and R7=20 time 1 s
ORG 1BH
LJMP T1ISR ;use T1 to sound alarm
Main: SETB IT0 ;negative edge trigger
MOV TMOD,#11H ;16-bit timer
MOV IE,#81H ;enable EX0 only
Skip: SJMP $ ;relax & wait for intrusion
9 cycles