interrupts how to do 2 things at the same time. need for interrupts computers often have to deal...

22
Interrupts How to do 2 things at the same time

Upload: brian-reynolds

Post on 28-Mar-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

Interrupts

How to do 2 things at the same time

Page 2: 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

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

Page 3: 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

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.

Page 4: 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

Idea of interrupts

• Main programrepeat

Do this

Do that

Try something else

For ages

• Interrupt handlerKey pressed

read input port

store result

return

press

Page 5: 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

• 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.

Page 6: 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

An interrupt is a procedure called by the hardware of the computer rather

than by your program.

Page 7: 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

Interrupt control registers

Page 8: 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

Intcon

global interupt

enable• INT pin interrupt

• TMR0 overflow interrupt

• GP port change interrupt

• GP port change interrupt

• INT pin interrupt• TMR0 overflow interrupt

FLAGSENABLES

Page 9: 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

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 0004hOnce in the Interrupt Service Routine, the source(s) ofthe interrupt can be determined by polling the interruptflag bits. The interrupt flag bit(s) must be cleared insoftware before re-enabling interrupts to avoid GP2/INT recursive interrupts.

Page 10: 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

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.

Page 11: 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

timing of interrupt

Page 12: 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

Vectors

• Reset vector, where we start on powerup

• Interrupt vector where we go on an interrupt

Goto main

Goto isr

01234

memory

Page 13: 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

Sample start of programORG 0x000 ; processor reset vectorgoto Init ; go to beginning of programORG 0x004 ; Interrupt vector location

goto isr ; go to interrupt routine

Page 14: 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

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

Page 15: 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

Save registersIsr;Interrupt Vector - Interrupt Sources Used: 1. TIMER0 Overflow; 2. GP3 Pin-Change

movwf WTEMP ;Save current W registermovf STATUS,wclrf STATUS ;Force to page0movwf STATUSTEMP ;Save STATUS in STATUSTEMP

movf PCLATH,wmovwf PCLATHTEMP ;Save PCLATHmovf FSR,wmovwf FSRTEMP ;Save FSRBANK1 ;select bank 1

Page 16: 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

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

Page 17: 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

Check what is enabled

• We only need to test these flags if they are enabled.

• This means we must first check the relevant interrupt enable bits

Page 18: 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

Interrupt Source Checks;****************************************************************************;****************************************************************************Timer0InterruptCheck btfss INTCON,TOIE ;Is T0IE Set?

goto Next1 ;No ;Yes

btfsc INTCON,TOIF ;Is TOIF Set?goto Timer0Interrupt ;Yes

Next1GPIFInterruptCheck btfss INTCON,GPIE ;Is GPIE Set?

goto Next2 ;Nobtfsc INTCON,GPIF ;Yes ;Is GPIF Set?goto GPIFInterrupt ;Yes

Page 19: 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

Interrupt Source Code

Timer0Interrupt

call Display ;Update LED Array

bcf INTCON,T0IF ;Clear TMR0 Interrupt Flag

; this prevents interrupt

; from occuring again

; spuriously

goto EndIsr

Page 20: 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

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

Page 21: 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

Cleanup codeEndIsr

clrf STATUS ;Select Bank0movf FSRTEMP,wmovwf FSR ;Restore FSRmovf PCLATHTEMP,wmovwf PCLATH ;Restore PCLATHmovf STATUSTEMP,wmovwf STATUS ;Restore STATUSswapf WTEMP,f swapf WTEMP,w ;Restore W without

; corrupting STATUS bitsretfie ;Return from interrupt

Page 22: 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

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 WTEMP,f swap halves of wtemp

swapf WTEMP,w swap again and store in W

Net result is that W contains original form of wtemp