microprocessor and interfacing 261214. example: writing a game need to check keyboard input

19
Interrupts Microprocessor and Interfacing 261214

Upload: lesley-harrington

Post on 29-Jan-2016

227 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Microprocessor and Interfacing 261214. Example: Writing a Game Need to Check Keyboard Input

InterruptsMicroprocessor and Interfacing

261214

Page 2: Microprocessor and Interfacing 261214. Example: Writing a Game Need to Check Keyboard Input

Example: Writing a Game

Need to Check Keyboard Input

Page 3: Microprocessor and Interfacing 261214. Example: Writing a Game Need to Check Keyboard Input

Method 1: Using a LoopProgram keeps checking for keyboard input

While (1) [ If Key = Right Then

moveRight ElseIf Key = Left Then moveLeft End If]

Page 4: Microprocessor and Interfacing 261214. Example: Writing a Game Need to Check Keyboard Input

Mothod II: Use KeyPress EventRuns only when there is a keyboard interrupt

KeyPressEvent(Key) If Key = Left Then MoveLeft ElseIf Key = Right Then MoveRight End If End Subroutine

Page 5: Microprocessor and Interfacing 261214. Example: Writing a Game Need to Check Keyboard Input

What’s the Difference Between

Method I - Method II ?

Page 6: Microprocessor and Interfacing 261214. Example: Writing a Game Need to Check Keyboard Input

Method I : Software Polling

Method II : Event or Interrupt Driven

Page 7: Microprocessor and Interfacing 261214. Example: Writing a Game Need to Check Keyboard Input

I/O Handling TechniquesSoftware Polling

Interrupts

Direct Memory Access (DMA)

Page 8: Microprocessor and Interfacing 261214. Example: Writing a Game Need to Check Keyboard Input

Benefits of Using InterruptsConsumes much less CPU

Especially when interrupt generated by hardware

Cleaner & Simpler Code

Allows Basic Parallel Processing

Page 9: Microprocessor and Interfacing 261214. Example: Writing a Game Need to Check Keyboard Input

Exercise: Program a PIC MicrocontrollerBlink an LED every 0.5 secAlso receives serial data from the computer

While (1) {output_toggle(LED);delay_ms(500);data = getchar();

}

What’s wrong with this program?

Page 10: Microprocessor and Interfacing 261214. Example: Writing a Game Need to Check Keyboard Input

A better program, but not best

While (1) {output_toggle(LED);delay_ms(500);if (kbhit()) {

data = getchar();}

}

Page 11: Microprocessor and Interfacing 261214. Example: Writing a Game Need to Check Keyboard Input

How do we fix the problem?Available Commands

Getchar() – wait and return serial dataOutput_toggle(LED)Time() – returns current time (in ms)Kbhit() – returns true if serial data is available

Page 12: Microprocessor and Interfacing 261214. Example: Writing a Game Need to Check Keyboard Input

Solution Software PollingInt currentTime;Char data;startTime = time();

While (1) {if (time() – startTime > 500) {

output_toggle(LED);startTime = time();

} if (kbhit()) {

data = getchar(); }

}

Page 13: Microprocessor and Interfacing 261214. Example: Writing a Game Need to Check Keyboard Input

Same Program with Timer InterrupttimerISR() {output_toggle(LED);

}

Main() {setupTimer(); while (1) {

data = getchar();}

}

Page 14: Microprocessor and Interfacing 261214. Example: Writing a Game Need to Check Keyboard Input

Interrupt Handling

Page 15: Microprocessor and Interfacing 261214. Example: Writing a Game Need to Check Keyboard Input

Multi-Interrupt Handling

Page 16: Microprocessor and Interfacing 261214. Example: Writing a Game Need to Check Keyboard Input

Important Note:Must minimize the time an ISR takes to executeInterrupts should perform as little work as

possible.Should not call I/O functions

Keyboard input – getchar(), scanf()I2C commandsRead sensor

Should not call delay functionsDelay_ms(), Dealy_us()

Page 17: Microprocessor and Interfacing 261214. Example: Writing a Game Need to Check Keyboard Input

Useful technique:

Asynchronous Execution

timerISR() {output_high(LED);delay_ms(1000);output_low(LED);

}

Main() {setupTimer(); while (1) {// do main work}

}

Example of a BAD interrupt code design:

ISR includes a long delay

Page 18: Microprocessor and Interfacing 261214. Example: Writing a Game Need to Check Keyboard Input

After using Asynchronous Execution:Execution Moved to main()

int1 doBlink = 0;timerISR() {

doBlink = 1;}

Main() {setupTimer(); while (1) {// do main workif (doBlink == 1){

output_high(LED);delay_ms(1000);output_low(LED);doBlink = 0;

}}

}

Page 19: Microprocessor and Interfacing 261214. Example: Writing a Game Need to Check Keyboard Input

Exercise: Use Asynchronous Execution to fix this program

IO_ISR() {ss = readSensor(1);printf(“%ld\r\n”, ss);

}

Main() {setup_IO_Interrupt(); while (1) {// do main work}

}

IO interrupts happen when the user presses a button connected to a micro-controller