Interrupts Microprocessor and Interfacing 261214 Example Writing a

  • Slides: 19
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

Important Note: Must minimize the time an ISR takes to execute �Interrupts should perform

Important Note: Must minimize the time an ISR takes to execute �Interrupts should perform as little work as possible. �Should not call I/O functions �Keyboard input – getchar(), scanf() �I 2 C commands �Read sensor �Should not call delay functions �Delay_ms(), Dealy_us()

Useful technique: Asynchronous Execution Example of a BAD interrupt code design: timer. ISR() {

Useful technique: Asynchronous Execution Example of a BAD interrupt code design: timer. ISR() { output_high(LED); delay_ms(1000); output_low(LED); } Main() { setup. Timer(); while (1) { // do main work } } ISR includes a long delay

After using Asynchronous Execution: Execution Moved to main() int 1 do. Blink = 0;

After using Asynchronous Execution: Execution Moved to main() int 1 do. Blink = 0; timer. ISR() { do. Blink = 1; } Main() { setup. Timer(); while (1) { // do main work if (do. Blink == 1){ output_high(LED); delay_ms(1000); output_low(LED); do. Blink = 0; } } }

Exercise: Use Asynchronous Execution to fix this program IO_ISR() { ss = read. Sensor(1);

Exercise: Use Asynchronous Execution to fix this program IO_ISR() { ss = read. Sensor(1); printf(“%ldrn”, ss); } Main() { setup_IO_Interrupt(); while (1) { // do main work } } IO interrupts happen when the user presses a button connected to a micro-controller