Presentation on theme: "Interrupts How to do 2 things at the same time. Need for interrupts Computers often have to deal with asynchronous events. That is to say events that."— Presentation transcript:
Need for interrupts Computers often have to deal with asynchronous events. That is to say events that occur times that are unpredictable relative to the rest of your program. Whilst a computer is busy running a program something unexpected may occur: –A packet may arrive on a communications line –A key may be pressed –A clock tic occurs
Alternative- Polling One alternative is to have your program running a loop checking for input/output events. This is called Polling. It is what all the examples so far have done. Polling wastes processor resources checking for events that rarely happen Polling makes it hard to get the computer to do any useful work.
Idea of interrupts Main program repeat Do this Do that Try something else For ages Interrupt handler Key pressed read input port store result return press
Interrupts occur between two instructions. Control is transferred by the hardware to an interrupt location. The interrupt routine does its stuff It then returns to the following instruction in the main program.
An interrupt is a procedure called by the hardware of the computer rather than by your program.
Intcon global interupt enable INT pin interrupt TMR0 overflow interrupt GP port change interrupt INT pin interrupt TMR0 overflow interrupt FLAGS ENABLES
what happens When an interrupt is serviced: The GIE is cleared to disable any further interrupt The return address is pushed onto the stack The PC is loaded with 0004h Once in the Interrupt Service Routine, the source(s) of the interrupt can be determined by polling the interrupt flag bits. The interrupt flag bit(s) must be cleared in software before re-enabling interrupts to avoid GP2/ INT recursive interrupts.
Interrupts on the PIC Context Saving During Interrupts During an interrupt, only the return PC value is saved on the stack. Typically, users may wish to save key registers during an interrupt, e.g., W register and STATUS register. This must be implemented in software.
Interrupt Source Code Timer0Interrupt callDisplay ;Update LED Array bcfINTCON,T0IF ;Clear TMR0 Interrupt Flag ; this prevents interrupt ; from occuring again ; spuriously gotoEndIsr
Restore old registers Finally we must restore any saved registers There is a particular problem with this because of the fact that our saving code may change the flags in the status register This requires care to get round
Cleanup code EndIsr clrfSTATUS;Select Bank0 movfFSRTEMP,w movwfFSR;Restore FSR movfPCLATHTEMP,w movwfPCLATH;Restore PCLATH movfSTATUSTEMP,w movwfSTATUS;Restore STATUS swapfWTEMP,f swapfWTEMP,w ;Restore W without ; corrupting STATUS bits retfie ;Return from interrupt
Swapf This is defined to do the following: SWAPFf,dSwap halves f f(0:3) f(4:7)->d It does not alter the z flag of the status register swapfWTEMP,f swap halves of wtemp swapfWTEMP,w swap again and store in W Net result is that W contains original form of wtemp