Interrupts Microprocessor and Interfacing 261214 Example Writing a

  • Slides: 15
Download presentation
Interrupts Microprocessor and Interfacing 261214

Interrupts Microprocessor and Interfacing 261214

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 �Software Polling �Interrupts �Direct Memory Access (DMA)

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

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

Exercise: Program a PIC Microcontroller �Blink an LED every 0. 5 sec �Also receives serial data from the computer What’s wrong with this program? While (1) { output_toggle(LED); delay_ms(500); data = getchar(); }

A better program, but not best While (1) { output_toggle(LED); delay_ms(500); if (kbhit()) {

A better program, but not best While (1) { output_toggle(LED); delay_ms(500); if (kbhit()) { data = getchar(); } }

How do we fix the problem? Available Commands �Getchar() – wait and return serial

How do we fix the problem? 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 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