Interrupts Microprocessor and Interfacing
Example: Writing a Game Need to Check Keyboard Input
Method 1: Using a Loop Program keeps checking for keyboard input While (1) [ If Key = Right Then moveRight ElseIf Key = Left Then moveLeft End If ]
Mothod II: Use KeyPress Event Runs only when there is a keyboard interrupt KeyPressEvent(Key) If Key = Left Then MoveLeft ElseIf Key = Right Then MoveRight End If End Subroutine
WHAT’S THE DIFFERENCE BETWEEN METHOD I - METHOD II ?
Method I : Software Polling Method II : Event or Interrupt Driven
I/O Handling Techniques Software Polling Interrupts Direct Memory Access (DMA)
Benefits of Using Interrupts Consumes much less CPU Especially when interrupt generated by hardware Cleaner & Simpler Code Allows Basic Parallel Processing
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
Solution Software Polling Int currentTime; Char data; startTime = time(); While (1) { if (time() – startTime > 500) { output_toggle(LED); startTime = time(); } if (kbhit()) { data = getchar(); }
Same Program with Timer Interrupt timerISR() { output_toggle(LED); } Main() { setupTimer(); while (1) { data = getchar(); }
Interrupt Handling
Multi-Interrupt Handling
Interrupt on the PIC16
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
Interrupts on the PIC
#INT_RBvoid rb_isr(void) { /// interrupt code } Void main() { enable_interrupts(INT_RB); enable_interrupts(GLOBAL); /// main code }
Calling the correct ISR INT Boot Vector 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 registers may need to be saved, but must be done in software.
Benefits of Programming in C Everything is done for you automatically!
DMA (Direct Memory Access) CPU I/O (e.g. Disk) RAM DMA Can send data directly to and from memory
Watching a Movie: How Does the Data Flow? CPU RAM Display
Summary
Analogy I: Hanging a Picture Frame
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.