Interrupts Microprocessor and Interfacing 261313 Example Writing a

  • Slides: 27
Download presentation
Interrupts Microprocessor and Interfacing 261313

Interrupts Microprocessor and Interfacing 261313

Example: Writing a Game Need to Check Keyboard Input

Example: Writing a Game Need to Check Keyboard Input

Method 1: Using a Loop Program keeps checking for keyboard input While (1) [

Method 1: Using a Loop Program keeps checking for keyboard input While (1) [ If Key = Right Then move. Right Else. If Key = Left Then move. Left End If ]

Mothod II: Use Key. Press Event Runs only when there is a keyboard interrupt

Mothod II: Use Key. Press Event Runs only when there is a keyboard interrupt Key. Press. Event(Key) If Key = Left Then Move. Left Else. If Key = Right Then Move. Right End If End Subroutine

WHAT’S THE DIFFERENCE BETWEEN METHOD I - METHOD II ?

WHAT’S THE DIFFERENCE BETWEEN METHOD I - METHOD II ?

Method I : Software Polling Method II : Event or Interrupt Driven

Method I : Software Polling Method II : Event or Interrupt Driven

I/O Handling Techniques n Software Polling n Interrupts n Direct Memory Access (DMA)

I/O Handling Techniques n Software Polling n Interrupts n Direct Memory Access (DMA)

Benefits of Using Interrupts n Consumes much less CPU q Especially when interrupt generated

Benefits of Using Interrupts n Consumes much less CPU q Especially when interrupt generated by hardware n Cleaner & Simpler Code n Allows Basic Parallel Processing

Exercise: Program a PIC Microcontroller n n Blink an LED every 0. 5 sec

Exercise: Program a PIC Microcontroller n n Blink an LED every 0. 5 sec Also receives serial data from the computer Available Commands q q Getchar() – wait and return serial data Output_toggle(LED) Time() – returns current time (in ms) Kbhit() – returns true if serial data is available

Solution Software Polling Int current. Time; Char data; start. Time = time(); While (1)

Solution Software Polling Int current. Time; Char data; start. Time = time(); While (1) { if (time() – start. Time > 500) { output_toggle(LED); start. Time = time(); } if (kbhit()) { data = getchar(); } }

Same Program with Timer Interrupt timer. ISR() { output_toggle(LED); } Main() { setup. Timer();

Same Program with Timer Interrupt timer. ISR() { output_toggle(LED); } Main() { setup. Timer(); while (1) { data = getchar(); } }

Interrupt Handling

Interrupt Handling

Multi-Interrupt Handling

Multi-Interrupt Handling

Interrupt on the PIC 16

Interrupt on the PIC 16

Available Interrupts n n n n INT Pin Interrupt (external interrupt) TMR 0 Overflow

Available Interrupts n n n n INT Pin Interrupt (external interrupt) TMR 0 Overflow Interrupt PORTB Change Interrupt (pins RB 7: RB 4) 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 Timer 1 Overflow Interrupt Timer 2 Overflow Interrupt CCP Interrupt SSP Interrupt

Interrupts on the PIC

Interrupts on the PIC

Interrupts on the PIC

Interrupts on the PIC

#INT_RBvoid rb_isr(void) { /// interrupt code } Void main() { enable_interrupts(INT_RB); enable_interrupts(GLOBAL); /// main

#INT_RBvoid rb_isr(void) { /// interrupt code } Void main() { enable_interrupts(INT_RB); enable_interrupts(GLOBAL); /// main code }

Calling the correct ISR Boot Vector INT ISR Check which INT occurred Int Timer

Calling the correct ISR Boot Vector INT ISR Check which INT occurred Int Timer Int Serial Int I/O etc

Context Switching: How do switch to and from an interrupt PC Saved Automatically Other

Context Switching: How do switch to and from an interrupt PC Saved Automatically Other registers may need to be saved, but must be done in software.

Benefits of Programming in C Everything is done for you automatically!

Benefits of Programming in C Everything is done for you automatically!

DMA (Direct Memory Access) CPU RAM DMA I/O (e. g. Disk) Can send data

DMA (Direct Memory Access) CPU RAM DMA I/O (e. g. Disk) Can send data directly to and from memory

Watching a Movie: How Does the Data Flow? Display CPU RAM

Watching a Movie: How Does the Data Flow? Display CPU RAM

Summary

Summary

Analogy I: Hanging a Picture Frame

Analogy I: Hanging a Picture Frame

Analogy II: A Simple Game n n n We need one volunteer The instructor

Analogy II: A Simple Game n n n 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.

5 11 9 3 13 17 1 15 23 19 14 6 21 16

5 11 9 3 13 17 1 15 23 19 14 6 21 16 10 4 22 2 25 24 7 20 12 8 18