Lecture 11 Interrupts
Lecture 11 Interrupts
Lecture 11
Interrupts
CSE 3442/5442
Embedded Systems 1
Based heavily on slides by Dr. Gergely Záruba and Dr. Roger Walker
External PIC Influence
while(1)
{
if(PORTBbits.RB0 == 1)
break;
}
//or
while(PORTBbits.RB0 == 0); 3
Polling vs. Interrupts
• Interrupts
– Whenever a device (pin, peripheral) needs the PIC’s
service, it notifies by sending an interrupt signal
• Asynchronous (can happen at any time)
– When that signal is detected…
• PIC stops (pauses) its current actions
• Handles (serves) the source of the interrupt
• Returns exactly where the PIC left off (resumes)
– Doesn’t bog-down the PIC’s resources
– Can serve many devices (multiple interrupt sources)
• Each can get the PIC’s attention at any time
– Can assign priorities to each interrupt
• “Interrupt an interrupt”
– Can also ignore (mask) interrupt sources at any time
4
– When sleeping, they can wake up the microcontroller
Interrupt Handling
Main Program Main Program
(No Interrupts) (With Interrupts) (serves) Interrupt
Service
MOVLW 0x30 MOVLW 0x30 Routine
ADDLW 0x1F Interrupt ADDLW 0x1F Interrupt Inst 1
Occurs
Instruction 3 Instruction 3 Interrupt Inst 2
(pause)
Instruction 4 Interrupt Inst 3
…
Instruction 5 Instruction 4
return
Main
Instruction 6 Instruction 5
Program
Instruction 7 Continues Instruction 6
(resumes)
Instruction 8 Instruction 7
5
Instruction 8
Basic Example
6
Basic Example
(No Interrupts)
void main() void main()
{ {
int a, b, c = 0; int a, b, c = 0;
while(1) while(1)
{ //main program { //main program
… …
9
As there is limited space at these
addresses it is a good idea to place
a GOTO instruction at the interrupt
vector jumping to a remote location
10
As there is limited space at these
addresses it is a good idea to place
a GOTO instruction at the interrupt
vector jumping to a remote location
ORG 0000H
GOTO MAIN
ORG 0008H
GOTO HP_ISR
ORG 0018H
GOTO LP_ISR
ORG 50H
HP_ISR …
ORG 150H
LP_ISR …
ORG 250H 11
MAIN …
As there is limited space at these
addresses it is a good idea to place
a GOTO instruction at the interrupt
vector jumping to a remote location 50H
ORG 0000H HP_ISR
GOTO MAIN 150H
LP_ISR
ORG 0008H 250H
GOTO HP_ISR MAIN
ORG 0018H
GOTO LP_ISR
ORG 50H
HP_ISR …
ORG 150H
LP_ISR …
ORG 250H 12
MAIN …
What Happens When an Interrupt Hits?
1. Timers
2. Hardware Interrupts (external pins, INT)
– PORTB: RB0, RB1, and RB2
3. Serial Communication
– Receive and Transmit
4. PORTB-Change
5. ADC
6. CCP/PWM
14
Simplified View of Interrupts
15
Simplified View of Interrupts
16
Masking Interrupts
20
Logical View of
All Interrupts
21
Logical View of
All Interrupts
1
22
What Happens to Other
Important Registers?
• What happens to other important registers (WREG,
Status, BSR) that may be impacted by an interrupt
– especially as they should be found the same way as they
were left when returning
• The solution lies in the ISR having to save these
registers at the beginning and restoring at the end
• High-Priority
– PIC18 automatically stores them in shadow registers
– To restore registers use RETFIE 1
• Low-Priority
– Programmer must store them manually 23
What Happens to Other Important
Registers? – Fast Context Switching
• There is a one-deep shadow register set for WREG,
Status, and BSR (similar to CALL and RETURN)
When jumping to
High-Priority ISR
When
returning
from High-
Priority ISR
24
Shadow Registers
Main Program
(With Interrupts)
Interrupt Inst 3
…
Instruction 4
RETFIE 1
Instruction 5
Instruction 6
Instruction 7
25
Instruction 8
Shadow Registers
Main Program
(With Interrupts)
Interrupt Inst 3
…
Instruction 4
RETFIE 1
Instruction 5
Instruction 6
Instruction 7
26
Instruction 8
Shadow Registers
Main Program
(With Interrupts)
Interrupt Inst 3
…
Instruction 4
RETFIE 1
Instruction 5
Instruction 6
Instruction 7
27
Instruction 8
External INT Interrupts
• INT0, INT1, and INT2 are all interrupts assigned to
digital I/O pins
– To use them the corresponding TRISB bits have to be set
• INT interrupts are edge triggered (not level), thus a
change must happen on the pins to trigger an interrupt
• Whether rising (default) or falling edge triggers the
interrupt is software (INTCON2.INTEDGx bits) selectable
• When triggered (like many other flags) the ISR should
explicitly clear the INTxIF flag
• INT0 is always of high priority, the other two can be set
28
External PORTB Interrupts
30
INTCON2
31
INTCON3
32
PIR1
33
PIR2
34
PIE1
35
PIE2
36
IPR1
37
IPR2
38
RCON
39
Interrupt Programming from Assembly
• High-Priority
void interrupt My_ISR_High(void)
{
//interrupt handling for HP
}
• Low-Priority
void interrupt low_priority My_ISR_Low(void)
{
//interrupt handling for LP
45
}
Interrupt Handling in XC8
47
Summary
• Interrupts are a great way to handle peripheral attention or
external happenings
• Some of the most used interrupts are timers (later),
external hardware, serial communications, and ADC ready
• All interrupts in the PIC18 can be masked in a group or
individually
• We can have two levels of priorities, with an almost fully
configurable what interrupt belong to what level
relationship
• Programming ISRs from C requires knowledge of how the
compiler is told about ISRs
– Consult the compiler’s user guide for specifics
48