CSCC85 Summer 2004: Interrupts
CSCC85 Summer 2004: Interrupts
Chapter 6 Interrupts
Introduction
An interrupt is the occurrence of an event that causes a temporary suspension of a program while the event is serviced by another program. Allow a system to respond asynchronously to an event and deal with the event while another program is executing. An interrupt-driven system gives the illusion of doing many things simultaneously by temporarily suspending execution of one program, executing another, then returning to the first program. Similar to a subroutine except that the interruption is a response to an event that occurs asynchronously with the main program. It is not known when the program will be interrupted.
Introduction
Interrupt service routine (ISR) or interrupt handler program that deals with the interrupt executes in response to the interrupt (generally performs an input or output operation to a device) when an interrupt occurs: 1)main program temporarily suspends execution and branches to the ISR 2)ISR executes, performs the operation and terminates with a return from interrupt instruction 3)main program continues where it left off main program operates at base-level, ISRs operate at interrupt-level
Introduction
time Main program
Main *
Main * ISR
Main **
FIGURE 6-1
Program execution with and without interrupts
Enabling/Disabling Interrupts
individually enabled/disabled through bit-addressable SFR interrupt enable (IE) register at address 0A8H global enable/disable bit also available 2 bits must be set to enable any interrupt; the individual enable bit and the global enable bit Example: timer 1 interrupts are enabled as follows: SETB ET1 SETB EA ;enable Timer 1 interrupt ;set global enable bit
Enabling/Disabling Interrupts
Interrupt Priority
each interrupt source is individually programmed to one of two priority levels (high and low priorities) through the bitaddressable SFR interrupt priority (IP) at address 0B8H IP is cleared after system reset (lower priority default) allows an ISR to be itself interrupted by a new, higher priority interrupt (a high priority interrupt cannot be interrupted) main program executes at base level and can always be interrupted if two interrupts of different priority occur simultaneously, the higher priority interrupt will be serviced first if two interrupts of the same priority occur simultaneously, a polling sequence determines which is serviced first
Interrupt Priority
Polling Sequence
if two interrupts of the same priority occur simultaneously, a fixed polling sequence determines which one is serviced first sequence is: external 0 Timer 0 external 1 Timer 1 serial port Timer 2 (8052 only)
IP Register
RI TI TF2 EXF2
FIGURE 6-2
Accept interrupt
Interrupt Flags
The flag bits that generate interrupts are:
Processing Interrupts
When an interrupt occurs and is accepted by the CPU, the main program in interrupted. The following actions accur: current instruction completes execution PC is saved on the stack current interrupt status is saved internally interrupts are blocked at the level of the interrupt PC is loaded with the vector address of the ISR ISR executes when ISR completes, a RETI (return from interrupt) instruction executes
Interrupt Vectors
interrupt vector the value loaded into the PC when an interrupt is accepted it is the address of the start of the ISR for the interrupting source External code memory
FFFF
Main program 0030 002F 0000 LJMP MAIN Reset and interrupt entry points
system reset vector (RST at address 0000H) is included in this table since it behaves like an interrupt
Interrupt Vectors
vectoring to an interrupt automatically clears the flag that caused the interrupt (done in hardware) exceptions are RI and TI for serial port interrupts (and TF2 and EXF2 for Timer 2 interrupts) since there are two possible sources for each of these interrupts these bits must be tested in the ISR to determine the source of the interrupt, and then the interrupting flag is cleared by software and a branch to the appropriate action occurs since the interrupt vectors are at the beginning of code memory, the first instruction of the main program is often a jump to skip over it. (eg. LJMP 0030H)
TOISR: MAIN:
MAIN: TOISR:
TOISR:
MAIN:
MAIN:
T0ISR: ;
MOV TH0,#-71 SETB TR0 SETB TF1 MOV IE,#8AH SJMP $ CPL P1.7 RETI continued on next page...
143 s 71 s
8051 P1.7
2 ms 1 ms
P1.6