Interrupts Microprocessor and Interfacing 261313.

27
Interrupts Microprocessor and Interfacing 261313

Transcript of Interrupts Microprocessor and Interfacing 261313.

Page 1: Interrupts Microprocessor and Interfacing 261313.

Interrupts

Microprocessor and Interfacing261313

Page 2: Interrupts Microprocessor and Interfacing 261313.

Example: Writing a Game

Need to Check Keyboard Input

Page 3: Interrupts Microprocessor and Interfacing 261313.

Method 1: Using a LoopProgram keeps checking for keyboard input

While (1) [ If Key = Right Then

moveRight ElseIf Key = Left Then moveLeft End If]

Page 4: Interrupts Microprocessor and Interfacing 261313.

Mothod II: Use KeyPress EventRuns only when there is a keyboard interrupt

KeyPressEvent(Key) If Key = Left Then MoveLeft ElseIf Key = Right Then MoveRight End If End Subroutine

Page 5: Interrupts Microprocessor and Interfacing 261313.

WHAT’S THE DIFFERENCE BETWEEN

METHOD I - METHOD

II ?

Page 6: Interrupts Microprocessor and Interfacing 261313.

Method I : Software Polling

Method II : Event or Interrupt Driven

Page 7: Interrupts Microprocessor and Interfacing 261313.

I/O Handling Techniques

Software Polling

Interrupts

Direct Memory Access (DMA)

Page 8: Interrupts Microprocessor and Interfacing 261313.

Benefits of Using Interrupts

Consumes much less CPU Especially when interrupt generated by hardware

Cleaner & Simpler Code

Allows Basic Parallel Processing

Page 9: Interrupts Microprocessor and Interfacing 261313.

Exercise: Program a PIC Microcontroller Blink an LED every 0.5 sec Also receives serial data from the computer

Available Commands Getchar() – wait and return serial data Output_toggle(LED) Time() – returns current time (in ms) Kbhit() – returns true if serial data is available

Page 10: Interrupts Microprocessor and Interfacing 261313.

Solution Software PollingInt currentTime;

Char data;

startTime = time();

While (1) {

if (time() – startTime > 500) {

output_toggle(LED);

startTime = time();

}

if (kbhit()) {

data = getchar();

}

}

Page 11: Interrupts Microprocessor and Interfacing 261313.

Same Program with Timer InterrupttimerISR() {

output_toggle(LED);

}

Main() {

setupTimer();

while (1) {

data = getchar();

}

}

Page 12: Interrupts Microprocessor and Interfacing 261313.

Interrupt Handling

Page 13: Interrupts Microprocessor and Interfacing 261313.

Multi-Interrupt Handling

Page 14: Interrupts Microprocessor and Interfacing 261313.

Interrupt on the PIC16

Page 15: Interrupts Microprocessor and Interfacing 261313.

Available Interrupts INT Pin Interrupt (external interrupt) TMR0 Overflow Interrupt PORTB Change Interrupt (pins RB7:RB4) Comparator Change Interrupt Parallel Slave Port Interrupt USART Interrupts Receive Interrupt Transmit Interrupt A/D Conversion Complete Interrupt LCD Interrupt. Data EEPROM Write Complete Interrupt Timer1 Overflow Interrupt Timer2 Overflow Interrupt CCP Interrupt SSP Interrupt

Page 16: Interrupts Microprocessor and Interfacing 261313.

Interrupts on the PIC

Page 17: Interrupts Microprocessor and Interfacing 261313.

Interrupts on the PIC

Page 18: Interrupts Microprocessor and Interfacing 261313.

#INT_RBvoid rb_isr(void) { /// interrupt code}

Void main() {enable_interrupts(INT_RB); enable_interrupts(GLOBAL);

/// main code

}

Page 19: Interrupts Microprocessor and Interfacing 261313.

Calling the correct ISR

INTBoot Vector

ISRCheck which INT occurred

Int TimerInt SerialInt I/Oetc

Page 20: Interrupts Microprocessor and Interfacing 261313.

Context Switching: How do switch to and from an interrupt

PC Saved Automatically

Other registers may need to be saved, but must bedone in software.

Page 21: Interrupts Microprocessor and Interfacing 261313.

Benefits of Programming in C

Everything is done for you automatically!

Page 22: Interrupts Microprocessor and Interfacing 261313.

DMA (Direct Memory Access)

CPU

I/O(e.g. Disk)

RAMDMA

Can send data directly to and from memory

Page 23: Interrupts Microprocessor and Interfacing 261313.

Watching a Movie:How Does the Data Flow?

CPU

RAM

Display

Page 24: Interrupts Microprocessor and Interfacing 261313.

Summary

Page 25: Interrupts Microprocessor and Interfacing 261313.

Analogy I: Hanging a Picture Frame

Page 26: Interrupts Microprocessor and Interfacing 261313.

Analogy II: A Simple Game

We need one volunteer The instructor will show a number to the class

but hide it from the player We will simulate Polling, Interrupt, and DMA

by playing this game.

Page 27: Interrupts Microprocessor and Interfacing 261313.

5 11 9 3 13

17 1 152319

14 6 211610

4 22 2 2524

7 2012 8 18