Interrupts Microprocessor and Interfacing
Example: Writing a Game Need to Check Keyboard Input
Method 1: Using a Loop Do If GetKeyState(vbKeyRight) Then moveRight ElseIf GetKeyState(vbKeyLeft) Then moveLeft End If Loop
Mothod II: Use KeyPress Event KeyPressEvent(KeyAscii As Integer) If KeyAscii = LeftKey Then MoveLeft ElseIf KeyAscii = RightKey Then MoveRight End If End Sub
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_high(LED), Output_low(LED) Time() – returns current time (in ms) Kbhit() – returns true if serial data is available
Solution Int currentTime; Char data; startTime = time(); While (1) { if (time() – startTime > 500) { blink_LED(); startTime = time(); } if (kbhit()) { data = getchar(); }
Same Program with Timer Interrupt timerISR() { blinkLED(); } Main() { setTimer(); data = getchar(); }
Interrupt Handling
Multi-Interrupt Handling
Interrupt on the PIC
Available Interrupts Include 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
DMA (Direct Memory Access) CPU I/O (e.g. Disk) RAM DMA Can send data directly to and from memory