cs4101 interrupt l.
Skip this Video
Loading SlideShow in 5 Seconds..
CS4101 嵌入式系統概論 Interrupt PowerPoint Presentation
Download Presentation
CS4101 嵌入式系統概論 Interrupt

Loading in 2 Seconds...

play fullscreen
1 / 38

CS4101 嵌入式系統概論 Interrupt - PowerPoint PPT Presentation

  • Uploaded on

CS4101 嵌入式系統概論 Interrupt. 金仲達教授 國立清華大學資訊工程學系. 11/22/2010. (Slides from An Embedded Software Primer , David E. Simon, Addison Wesley). Outline. Interrupt basics Interrupt in 8051 Shared data problem in interrupt. Recall Your Earlier Mission. An intrusion detection system. Spotlight.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'CS4101 嵌入式系統概論 Interrupt' - mike_john

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
cs4101 interrupt

CS4101 嵌入式系統概論Interrupt




(Slides fromAn Embedded Software Primer, David E. Simon, Addison Wesley)


Interrupt basics

Interrupt in 8051

Shared data problem in interrupt

recall your earlier mission
Recall Your Earlier Mission

An intrusion detection system





The motion detectors will track the intruder and direct the spotlight and camera to follow the intruder

system design
System Design







What software is needed on PCM7230to read PIR’s through 8051?

now focus on 8051 pir
Now, Focus on 8051+PIR
  • Need to know:
  • Which PIR is triggered
  • Time sequence of their triggering
how did 8051 know pir triggered
How Did 8051 Know PIR Triggered?

This is called ...?

void main (void) {


P3_6 = 1; //set PIR port input

P3_7 = 1; //set switch port input


while (1) { /* Loop forever */

while (P3_6 == 0 && P3_7 == 1 && ...) ...; // loop

if (P3_7 == 0) { ... }// switch is active low

else if (P3_6 == 1 ) { ... } // PIR is active high

else if ...


analogy checking for boiling soup
Analogy: Checking for Boiling Soup

This is called polling and it wastes your time!

Open the covers one by one and round-robin

If the soup in a pot boils, then handle that pot


how to avoid wasting your time
How to Avoid Wasting Your Time?


  • Do other things and come back later
    • How do you know time is up and you should come back?

 need a clock and notification

    • How do you know how long you can be away?

 you can try not to go away for too long

 you can guess (e.g., based on past experiences)

 you can ask the pots to notify you

how interrupt works for you
How Interrupt Works for You?

You are free to do any other things

When the soup in a pot boils, the pot will notify you by giving you a signal and interrupting whatever you are doing

You remember where you are interrupted

You return and check to see which pot sent you the interrupt and what services it requests

You follow the instructions as requested to do the service

You return to the interrupted work after you are done with the requested services

interrupt basics
Interrupt Basics

Interrupts are triggered when certain events occur in the hardware

Usually done by sending a signal to one of the processor’s IRQ (interrupt request) pins

interrupt service routine
Interrupt Service Routine

An interrupt signal stops the processor at the next instruction and the processor saves the address of the next instruction on the stack and jumps to the interrupt service routine (ISR) or interrupt handler

ISR is basically a subroutine to perform certain operations to handle the interrupt with a RETURN instruction at the end

It is a good practice to save register state and reset the interrupt in ISRs

ISRs are similar to a CALL except that the call to the ISR is automatically made

interrupt service routine13
Interrupt Service Routine

The following shows an example of an ISR

Task CodeISR





DIV R1, 2 ...

JCOND ZERO, END ;ISR code comes here


... POP R2

... POP R1


... ...

saving and restoring the context
Saving and Restoring the Context

The task code has no idea of the changes taking place in registers like R1 or R2 in the ISR

If R1 is modified by the ISR, we might get an incorrect final result.

Due to limited number of registers, the ISRs and task codes usually have to work with same registers.

To solve this problem it is common practice to save the register contents onto the stack (saving the context) and restoring them at the end of the ISR (restoring the context).

disabling interrupts
Disabling Interrupts

Programs may disable interrupts

In most cases the program can select which interrupts to disable during critical operations and which to keep enabled by writing corresponding values into a special register.

Nonmaskable interrupts cannot be disabled and are used to indicate power failures or serious event.

Certain processors assign priorities to interrupts, allowing programs to specify a threshold priority so that only interrupts having higher priorities than the threshold are enabled and the ones below it are disabled.

some common questions
Some Common Questions
  • How does processor know where to find ISR?
    • ISR is at a fixed location, e.g., in 8051, the first interrupt pin always causes 8051 to jump to 0x0003
    • A table in memory contains addresses of ISR these addresses are called interrupt vector the table is called interrupt vector table
  • How does processor know where interrupt vector table is?
    • At a fixed location, e.g., 0x00000 for Intel 80186
    • Devices provide the interrupt vectors
    • Programs may set the location of the table
    • ...
some common questions17
Some Common Questions
  • Can a processor be interrupted in the middle of an instruction?
    • Usually not
    • Exceptions: critical hardware failure, long-running instructions (e.g. moving data in memory)
  • If two interrupts occur at the same time, which ISR does the process do first?
    • Prioritize the interrupt signals
  • Can an interrupt signal interrupt another ISR?
    • Interrupt nesting usually allowed according to priority
    • Some processor may require re-enabling by your ISR
some common questions18
Some Common Questions
  • What happens when an interrupt is signaled while the interrupt is disabled?
    • Processors usually remember the interrupt signals and jump to the ISR when the interrupt is enabled
  • What happens when we forget to re-enable disabled interrupts?
  • What happens if we disable a disabled interrupt?
  • Are interrupts enabled or disabled when the processor first starts up?

Interrupt basics

Interrupt in 8051

Shared data problem in interrupt

interrupts in 8051
Interrupts in 8051

Original 8051 has 6 sources of interrupts


Timer 0 overflow

Timer 1 overflow

External Interrupt 0

External Interrupt 1

Serial Port events






interrupt vectors
Interrupt Vectors

Each interrupt has a specific place in program memory where execution of interrupt service routine begins

Note: only 8 memory locations between vectors

writing the isr
Writing the ISR

Example: ISR for Timer0 interrupt

ORG 0000H ;reset


ORG 000BH ;Timer0 entry point

T0ISR: . ;Timer0 ISR begins


RETI ;return to main program

MAIN: . ;main program




when an interrupt occurs
When an Interrupt Occurs

Following actions are taken by 8051:

The current Program Counter is saved on the stack, low-byte first.

Interrupts of the same and lower priority are blocked.

In the case of Timer and External interrupts, the corresponding interrupt flag is cleared.

Program execution transfers to the corresponding interrupt handler vector address.

The Interrupt Handler Routine executes.

when an interrupt ends
When an Interrupt Ends

An interrupt ends when the ISR executes the RETI (Return from Interrupt) instruction.

The following actions are taken by 8051:

Two bytes are popped off the stack into the Program Counter to restore normal program execution.

Interrupt status is restored to its pre-interrupt status

interrupt enable ie register
Interrupt Enable (IE) Register

EA ---- ET2 ES ET1 EX1 ET0 EX0

  • All interrupt are disabled after reset
    • Must be enabled by software for 8051 to respond to
    • Done by the 8-bit Interrupt Enable Register (IE)
    • EA: Global enable/disable
    • ES: Serial interface
    • ET1: Timer 1
    • EX1: External interrupt 1
    • ET0: Timer 0
    • EX0: External interrupt 0 (0=Disabled, 1=Enabled)
enabling and disabling an interrupt
Enabling and Disabling an Interrupt






  • By bit operation:

SETB EA ;Enable All

SETB ET0 ;Enable Timer0 ovrf

SETB ET1 ;Enable Timer1 ovrf

SETB EX0 ;Enable INT0

SETB EX1 ;Enable INT1

SETB ES ;Enable Serial port

  • By Mov instruction:

MOV IE, #10010110B


A 10khz square wave with 50% duty cycle

ORG 0 ;Reset entry point

LJMP MAIN ;Jump above interrupt

ORG 000BH ;Timer 0 interrupt vector

T0ISR:CPL P1.0 ;Toggle port bit

RETI ;Return from ISR to Main prog

ORG 0030H ;Main Program entry point

MAIN: MOV TMOD,#02H ;Timer 0, mode 2

MOV TH0,#-50 ;50 us delay

SETB TR0 ;Start timer

MOV IE,#82H ;Enable timer 0 interrupt

SJMP $ ;Do nothing just wait


interrupt priorities
Interrupt Priorities

What if two interrupt sources interrupt at the same time?

The interrupt with the highest PRIORITY gets serviced first.

All interrupts have a power on default order.

External interrupt 0 (INT0)

Timer interrupt0 (TF0)

External interrupt 1 (INT1)

Timer interrupt1 (TF1)

Serial communication (RI+TI)

Priority can be set to “high” or “low” by IP reg.

ip interrupt priority register
IP: Interrupt Priority Register

PS : Serial interface

PT1: Timer 1

PX1: External interrupt 1

PT0: Timer 0

PX0: External interrupt 0

0 = Low priority

1 = High priority

----- ----- PT2 PS PT1 PX1 PT0 PX0

interrupt priorities30
Interrupt Priorities

A low-priority interrupt can be interrupted by high-priority interrupt, but not by another low-priority one

A high-priority interrupt can’t be interrupted by any other interrupt source

If interrupt requests of the same priority level are received simultaneously, an internal polling sequence determines which request is serviced, so within each priority lever there is a second priority structure


Interrupt basics

Interrupt in 8051

Shared data problem in interrupt

the shared data problem
The Shared-Data Problem

In many cases the ISRs need to communicate with the task codes through shared variables.


Task code monitors 2temperatures and alarm if they differ

An ISR reads temperatures from hardware

the shared data problem33
The Shared-Data Problem

Now, consider the assembly code:

When temperatures are 70 degrees and an interrupt occurs between the two MOVES

The temperatures now become 75 degrees

On returning from ISR, iTemp[1] will be assigned 75 and an alarm will be set off even though the temperatures were the same

the shared data problem34
The Shared-Data Problem

Problem is due to shared array iTemperatures.

These bugs are very difficult to find as they occur only when the interrupt occurs in between the first 2 MOVE instructions, other than which code works perfectly.

solving shared data problem
Solving Shared-Data Problem

Disable interrupts during instructions that use the shared variable and re-enabling them later

while (TRUE)


disable(); // Disable interrupts

iTemp0 = iTemperatures[0];

iTemp1 = iTemperatures[1];

enable(); // Re-enable interrupts



solving shared data problem36
Solving Shared-Data Problem

“Atomic” and “Critical Section”

A part of a program that be interrupted


An ISR that updates iHours, iMinutes and iSeconds every second through a hardware timer interrupt:

long iSecondsSinceMidnight (void) {

long lReturnVal;


lReturnVal =

(((iHours*60)+iMinutes)*60)+iSeconds; enable();

return (lReturnVal);


interrupt latency
Interrupt Latency

Interrupt latency is the amount of time taken to respond to an interrupt. It depends on:

Longest period during which the interrupt is disabled

Time to execute ISRs of higher priority interrupts

Time for processor to stop current execution, do the necessary ‘bookkeeping’ and start executing the ISR

Time taken for the ISR to save context and start executing instructions that count as a ‘response’

Make ISRs short

Factors 4 and 2 are controlled by writing efficient code that are not too long.

Factor 3 depends on HW, not under software control

sources of interrupt overhead
Sources of Interrupt Overhead

Handler execution time

Interrupt mechanism overhead

Register save/restore

Pipeline-related penalties

Cache-related penalties