Академический Документы
Профессиональный Документы
Культура Документы
Interrupts
EE3954
by
Maarten Uijt de Haag, Tim Bambeck
Interrupts.1
Interrupts
Definitions
Interrupts:
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
VSS
VSS
Interrupts.3
Polling
PORTB
TRISB
STATUS
RP0
RP1
INIT:
POLL:
LED_OFF
LED_ON:
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
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
Push Button
Interrupt
Timer Interrupts
Serial Interrupt
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
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
Interrupt Occurs
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
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
Interrupts.9
Example:
Signal:
on-change
Interrupts.10
Interrupt
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
INIT:
MAIN:
ISR:
org
goto
org
goto
goto
retfie
0x000
INIT
0x004
ISR
Interrupt Vector
MAIN
instead of return
Interrupts.12
Interrupt
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
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
Interrupts.14
Interrupts
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
0x8D respectively
0x0D respectively
Interrupts.18
Interrupts.19
in: INTCON
For PIC16F877
(See section 12 of DS)
Interrupts.20
In General for
Mid-range devices
Interrupts.21
VDD
R
RB1
RB4
PIC16F877
VSS
VSS
Interrupts.22
Step 1
Interrupts.23
Step 1
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
Interrupts.24
Step 2
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
Interrupts.25
Step 3
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
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
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
MAIN
Instruction alters the Z bit
Interrupts.30
Context Saving
During Interrupts
Interrupts.32
Context Saving
Why Not:
ISR:
movwf
movf
movwf
movf
movwf
movf
W_TEMP
STATUS,W
STATUS_TEMP
STATUS_TEMP,W
STATUS
W_TEMP,W
Interrupts.33
Context Saving
Why Not:
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
Interrupts.34
Context Saving
Why Not:
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
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
Interrupts.37
Context Saving
During Interrupts
ISR:
movwf
swapf
movwf
swapf
movwf
swapf
swapf
W_TEMP
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?
Interrupts.39
Multiple Interrupts
Example:
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
Interrupts.44