Вы находитесь на странице: 1из 44

Microprocessors and Microcontrollers

Interrupts

EE3954
by
Maarten Uijt de Haag, Tim Bambeck
Interrupts.1

Interrupts
Definitions

Polling and interrupts are used when interfacing


and communicating with internal and external
devices and programs
Polling:

Internal or external devices are checked on a regular


basis to find out if they need service; if they require a
particular function to be executed.

Interrupts:

An event from either an internal or external source


where a processor will suspend the execution of its
current process (program) and switch to a different
instruction sequence.
Interrupts.2

Polling
If the switch is closed,
Then turn the LED on!

VDD
R

RB4

Polling the
Button

Set up
the I/O pins

RB0
Switch
Closed ?

Y
PIC16F877

Turn LED on

Turn LED off

VSS
VSS

Interrupts.3

Polling

PORTB
TRISB
STATUS
RP0
RP1

INIT:

POLL:
LED_OFF
LED_ON:

Source Code + Comments

equ
equ
equ
equ
equ

0x06
0x06
0x03
d5
d6

; in Bank 0 & 2
; in Bank 1 & 3
; in all Banks

org
nop
bcf
bsf
bsf
bcf
bcf
bcf
btfss
goto
bcf
goto
bsf
goto
end

0x000

; program starts at address 0

STATUS, RP1
STATUS, RP0
TRISB, 0
TRISB, 4
STATUS, RP0
PORTB, 4
PORTB, 0
LED_ON
PORTB, 4
POLL
PORTB, 4
POLL

; access Bank 1
; so we can get to TRIS Regs.
; set pin 0 of PORTB to input
; set pin 4 of PORTB to output
; access bank 0
; initially, turn the LED off
; check if PORTB pin 0 is 0 or 1
; goto TURN LED on
; Turn LED off, (set RB4=0)
; go back and keep polling
; Turn LED on, (set RB4=1)
; go back and keep polling

Interrupts.4

Interrupt Usage of a System

Push Button
Interrupt

Timer Interrupts

Serial Interrupt

Main Program Loop

Interrupts.5

Interrupts
If the switch is closed,
Then turn the LED on!

VDD

Some
Main Program

Interrupt
Service Routine

Set up
the I/O pins

Set up
the I/O pins

RB4

Set up
Interrupts

RB0

Switch
Closed ?

Y
Process A

PIC16F877

Turn LED on

Turn LED off

Go Back to
Process A (RETFIE)

VSS
VSS

Interrupts.6

Interrupts

Transfer of Control
Instruction N
Instruction N+1
Return of the program
Control from the end
of the ISR to the next
(N+1) instruction of
The main program

End main program

Interrupt Occurs

Program control is transferred


to the first instruction of the
interrupt service routine (ISR)

Instruction ISR-1
Interrupt Service Routine (ISR)
Return from Interrupt
Interrupts.7

PIC Microcontroller
Microcontroller has various on-chip peripheral devices that can
interrupt the main program

Analog-to-Digital Converter (ADC),


Timers and counters (8-bit, 16-bit),
Parallel port,
Serial communication,
RS232 (UART), I2C, SSP

Comparator,
Pulse-Width-Modulation (PWM)
External signal or interrupt
Interrupts.8

Signal:
on-change
ADC

Signal:
edge
Parallel
port
UART

Comparator,
PWM

SSP
Parallel
port
Multiplexing of I/O Pins

Pin Diagram from Datasheet

Interrupts.9

Example:

Signal:
on-change

On-change interrupt (RB4, RB5, RB6, RB7):


If the input on the pin is changed (0 => 1 or 1 => 0)
an interrupt will occur

Interrupts.10

Interrupt

So what happens when an interrupt occurs

Interrupt Vector
0 0000 0000 0100 (13bit)
( = 0x0004 )
In other words, the current value of the PC is stored on the
STACK and 0x0004 is put into PC: next instruction is at
0x0004

Interrupts.11

Interrupt

So what happens when an interrupt occurs

INIT:
MAIN:
ISR:

org
goto
org
goto

goto

retfie

0x000
INIT
0x004
ISR

Remember from the Labs

Interrupt Vector

MAIN

instead of return

Interrupt Service Routine (ISR)


Works just like a subroutine

Interrupts.12

Interrupt

Returning from an ISR

Put the value on top of the STACK back into the Program Counter (PC);
in other words, return to whatever the main program was doing.

Interrupts.13

Interrupt Service Routine


Example

INIT:
MAIN:
SUMISR:
NXT:

org
goto
org
goto

goto

movlw
movwf
movlw
xorwf
incf
btfss
goto
movwf
retfie

0x000
INIT
0x004
SUMISR
MAIN
0x20
FSR
0x00
INDF,W
FSR,F
FSR,6
NXT
PORTE

Suppose that we want to


write an ISR that XORs the
values stored in memory
locations 0x20 through 0x3f
and write the result to
PORTE.

Interrupts.14

Interrupts

INT Pin Interrupt (external interrupt-RB0)


Timer overflow interrupt,
Comparator change interrupt,
PORTB change interrupt (RB4-RB7),
Receive (communication) interrupt,
Transmit (comm.) interrupt,
A/D conversion complete interrupt,
Parallel slave port interrupt,
Etc.
Interrupts.15

Interrupts
INTCON (address 0x0B, 0x8B, 0x10B, 0x18B)
Control and Status of interrupts

Ending with an E
indicates an enable
bit: if the bit is set
the corresponding
interrupt is enabled

Ending with an F
indicates a flag: if
the bit is set, the
corresponding
interrupt has
Interrupts.16
occurred

Interrupts.17

Interrupts

Control Registers

Used for: Configuration for Peripherals

PIE1 & PIE2 (address 0x8C and

0x8D respectively

PIR1 & PIR2 (address 0x0C and

0x0D respectively

Contain bits to enable/disable peripheral


interrupts for use
Contain bits to identify which interrupt
occurs (flags):
Corresponding bits are set when the
interrupt occurred;

Interrupts.18

PIE & PIR

from PIC16F877 datasheet

From page 15 and 16 of PIC16F877 datasheet (DS)

Interrupts.19

in: INTCON

in: PIE1, PIE2


PIR1, PIR2

For PIC16F877
(See section 12 of DS)
Interrupts.20

In General for
Mid-range devices

Interrupts.21

Lets Look at an Example


Use the PORTB
on-change interrupt
to turn on the LED.

VDD
R

RB1

RB4

PIC16F877

VSS
VSS

Interrupts.22

Step 1

Set up the interrupt in main program


INTCON Register ( 0x0B in all Banks ):

Interrupts.23

Step 1

Set up the interrupt in main program


INTCON
TRISB
PORTB

INIT:

equ
equ
equ

0x0B
0x06
0x06

org
goto
org
goto
clrf
bsf
bsf
bcf
bcf
bsf
bcf

0x000
INIT
0x004
ISR
STATUS
STATUS,5
TRISB, 4
TRISB, 1
STATUS,5
INTCON,3
INTCON,0

; access
; bank 1
; pin 4 of PORTB: input
; pin 1 of PORTB: output
; access bank 0
; enable PORTB on-change interrupt
; YOU MUST CLEAR THE FLAG

(see next slide)

Interrupts.24

Step 2

Write the ISR


MAIN:

ISR:
LED_OFF:
LED_ON:

goto
btfss
goto
bcf
bcf
retfie
bsf
bcf
retfie

MAIN
PORTB, 4
LED_ON
PORTB, 1
INTCON,0
PORTB, 1
INTCON,0

; check if pin 4 changed from 0 to 1


; if not so, turn on LED (1 to 0)
; otherwise, turn off LED
; YOU MUST CLEAR THE FLAG
; return to whatever you were doing
; turn on LED
; YOU MUST CLEAR THE FLAG
; return to whatever you were doing

The flag is the only indication for the microcontroller


that the interrupt occurred, if you do not clear it in
the ISR the interrupt condition will remain!!!

Interrupts.25

Step 3

Enable the Global interrupt in main program


INTCON
TRISB
PORTB

INIT:

MAIN:

equ
equ
equ

0x0B
0x06
0x06

org
goto
org
goto
clrf
bsf
bsf
bcf
bcf
bsf
bcf
bsf

0x000
INIT
0x004
ISR
STATUS
STATUS,5
TRISB, 4
TRISB, 0
STATUS,5
INTCON,3
INTCON,0
INTCON,7

; access
; bank 1
; pin 4 of PORTB: input
; pin 0 of PORTB: output
; access bank 0
; enable PORTB on-change interrupt
; YOU MUST CLEAR THE FLAG
; enable all interrupts
Interrupts.26

Interrupt Latency
Time from interrupt event (the interrupt flag bit gets set) to the
time that the instruction at address 0x004 starts execution

Synchronous (typical internal) events: latency is 3TCY


Asynchronous (typical external) events: latency is 3TCY to 3.75TCY
with the instruction cycle time

In general:
see the individual sections of the peripheral devices for the exact latency

Interrupts.27

Interrupt Latency

Interrupts.28

Oops
What can go wrong with the combination of the following main program
and ISR?

MAIN:

ISR:

movf
addlw
movwf

goto

TEMP1, W
0x23
TEMP2

movlw
movwf
retfie

0x20
ISR_TMP,F

suppose that the interrupt


occurs here

MAIN

Interrupts.29

Oops Again
What can go wrong with the combination of the following main program
and ISR?

MAIN:

ISR:

movf
sublw
btfss

goto

TEMP1, W
0x23
STATUS,Z

movlw
addwf
retfie

0x20
ISR_TMP,F

suppose that the interrupt


occurs here

MAIN
Instruction alters the Z bit

Interrupts.30

Context Saving
During Interrupts

Save the CONTEXT:


at least W and STATUS,
but also all Registers you plan to use in
the ISR that are used in the Main
Program.

Save CONTEXT in RAM Common Area


0x71 -0x7F (recall 0x70 used by ICD2)
Interrupts.31

Common RAM Area

Advantage of this common area is


that you DO NOT have to change
banks

Remember this Data Memory area from Lab #1

Interrupts.32

Context Saving
Why Not:

ISR:

movwf
movf
movwf

movf
movwf
movf

W_TEMP
STATUS,W
STATUS_TEMP

; DOES NOT affect STATUS


; DOES affect STATUS
; DOES NOT affect STATUS

STATUS_TEMP,W
STATUS
W_TEMP,W

; DOES affect STATUS


; DOES NOT affect STATUS
; DOES affect STATUS

So, while you are saving W and STATUS you actually


changing the contents of STATUS. You do not want this to
happen so you use SWAPF which does not affect any flags in
STATUS
Example: suppose STATUS = 0x00?

Interrupts.33

Context Saving
Why Not:

Contents After Execution


Label

Instr.

Argument

ISR:

movwf

STATUS

W_TEMP

STATUS_TEMP

W_TEMP

0x12

0x00

0x12

movf

STATUS,W

0x00

0x04

0x12

movwf

STATUS_TEMP

0x00

0x04

0x12

0x00

0x04

movf

STATUS_TEMP, W

0x00

0x04

0x12

0x00

movwf

STATUS

0x00

0x00

0x12

0x00

movf

W_TEMP,W

0x12

0x00

0x12

0x00

Before this program segment: W = 0x12 and STATUS = 0x00

Interrupts.34

Context Saving
Why Not:

Contents After Execution


Label

Instr.

Argument

ISR:

movwf

STATUS

W_TEMP

STATUS_TEMP

W_TEMP

0x00

0x12

0x00

movf

STATUS,W

0x12

0x12

0x00

movwf

STATUS_TEMP

0x12

0x12

0x00

0x12

movf

STATUS_TEMP, W

0x12

0x??

0x00

0x12

movwf

STATUS

0x12

0x12

0x00

0x12

movf

W_TEMP,W

0x00

0x16

0x00

0x12

Before this program segment: W = 0x00 and STATUS = 0x12

Interrupts.35

So
What instruction moves to/from register without affecting any flags?

swapf
Problem is that this instruction swaps the nibbles of the byte

Solution:
Just swap them back

Interrupts.36

Context Saving
Contents After Execution
Label

Instr.

Argument

ISR:

movwf

STATUS

W_TEMP

STATUS_TEMP

W_TEMP

0x00

0x12

0x00

swapf

STATUS,W

0x21

0x12

0x00

movwf

STATUS_TEMP

0x21

0x12

0x00

0x21

swapf

STATUS_TEMP, W

0x12

0x00

0x21

movwf

STATUS

0x12

0x12

0x00

0x21

swapf

W_TEMP,F

0x12

0x12

0x00

0x21

swapf

W_TEMP,W

0x00

0x12

0x00

0x21

Before this program segment: W = 0x00 and STATUS = 0x12

Interrupts.37

Context Saving
During Interrupts

ISR:

movwf
swapf
movwf

swapf
movwf
swapf
swapf

W_TEMP

; Copy W to a Temporary Register


; regardless of current bank
STATUS,W
; Swap STATUS nibbles and place
; into W register
STATUS_TEMP ; Save STATUS to a Temporary register
; in Bank0

STATUS_TEMP,W
; Swap original STATUS register value
; into W (restores original bank)
STATUS
; Restore STATUS register from
; W register
W_TEMP,F
; Swap W_Temp nibbles and return
; value to W_Temp
W_TEMP,W
; Swap W_Temp to W to restore original
; W value without affecting STATUS
Interrupts.38

Multiple Interrupts
What to do when the Microcontroller has to deal with interrupts
for multiple sources, e.g. timer and PORTB change interrupt?

Check for the flags

Interrupts.39

Multiple Interrupts
Example:

suppose we could get an interrupt for


1) a PORTB pin level change,
2) a TIMER 0 overflow (0xFF -> 0x00)

Interrupts.40

Multiple Interrupts

INIT:
MAIN:
RB_ISR:
T0_ISR:

org
goto
org
btfsc
goto
btfsc
goto
retfie

retfie

retfie

0x0000
INIT
0x0004
INTCON, RBIF
RB_ISR
INTCON, T0IF
T0_ISR

Interrupts.41

Multiple Interrupts
Or

INIT:
MAIN:
RB_ISR:
T0_ISR:

org
goto
org
btfsc
goto
goto

retfie

retfie

0x0000
INIT
0x0004
INTCON, RBIF
RB_ISR
T0_ISR

Interrupts.42

Multiple Interrupts
Or

INIT:
MAIN:
ISR:
T0_ISR:
RB_ISR:

org
goto
org
goto

btfsc
goto

retfie

retfie

0x0000
INIT
0x0004
ISR

INTCON, RBIF
RB_ISR

Interrupts.43

Example in the Timer Section

Interrupts.44

Вам также может понравиться