Professional Documents
Culture Documents
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 press Do that Try something else For ages
Interrupt handler
Key pressed read input port store result return
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
ENABLES
FLAGS
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.
timing of interrupt
Vectors
Reset vector, where we start on powerup Interrupt vector where we go on an interrupt
memory
Goto main 0 1 2 3 4
Goto isr
Save context
Registers that are used by the interrupt routine must always be saved. Program counter saved automatically but there are some you must save: W reg STATUS reg 3 PCLATH reg 10 FSR reg 4
Save registers
Isr
;Interrupt Vector - Interrupt Sources Used: ; 1. 2. TIMER0 Overflow GP3 Pin-Change
;Save current W register ;Force to page0 ;Save STATUS in STATUSTEMP ;Save PCLATH ;Save FSR ;select bank 1
Determine source
We next need to inspect the interrupt flags to see what device caused the interrupt: TOIF Timer Overflow Interrupt Flag = bit 2 of INTCON GPIF Gpio interrupt flag= bit 0 of intcon
goto EndIsr
Cleanup code
EndIsr clrf movf movwf movf movwf movf movwf swapf swapf STATUS ;Select Bank0 FSRTEMP,w FSR ;Restore FSR PCLATHTEMP,w PCLATH ;Restore PCLATH STATUSTEMP,w STATUS ;Restore STATUS WTEMP,f WTEMP,w ;Restore W without ; corrupting STATUS bits ;Return from interrupt
retfie
Swapf
This is defined to do the following:
SWAPF f,d Swap halves f f(0:3)<->f(4:7)->d
It does not alter the z flag of the status register
swapf
swapf
WTEMP,f
WTEMP,w