Interrupt
Interrupt
Interrupt
INTERRUPT
Lecturer: Dr. Bui Ha Duc
Dept. of Mechatronics
Email: ducbh@hcmute.edu.vn
1
Communicate with physical world
inputs
Physical Microcontroller
world
outputs
2
Software flow
Method 1: Polling
• Simple, easy to
setup
• Work well for
simple tasks
3
Software flow
• Can be expanded to
support multiple
processes
Disavantages?
• Hard to apply to
complex task
• difficult to define
priorities between
different services
• poor responsiveness
• Waste energy Polling method for application with
multiple devices that need processing
4
Interrupt Driven
Method 2: Interrupt
• Waste energy -> sleep
mode, peripheral can
wake up the processor
when it requires a service
7
How CPU processes the Interrupt
1. Finish processing 2. Run interrupt 3. Resume
current instruction. Save service routine. main program
program counter and Restore Program
flags to stack counter and flags
from stack
http://learn.mikroe.com/ebooks/piccprogramming/chapter/pic16f887-basic-features/ 8
Interrupt Service Routine (ISR)
• Subroutines used to service an
interrupt are call ISR
9
Types of Interrupts
• Maskable interrupt: is a hardware interrupt that can be
ignored by setting interrupt registers
• Interrupts from peripheral modules
• E.g. adc interrupt, uart interrupt
• External pin interrupts
• E.g external interrupt
10
Pic18F Interrupts
PIC18 Microcontroller family
• Has multiple sources that can send interrupt requests
• Does not have any non-maskable or software interrupts;
• All interrupts are maskable (can be disabled)
• Has a priority scheme divided into two groups
• High priority vector, address 0x000008
• low priority, address 0x000018
• Uses many Special Function Registers (SFRs) to
implement the interrupt process
11
PIC18 Interrupt Sources
• Divided into two groups:
• External sources
• Internal peripheral sources
• External sources:
• Three pins of PORTB -RB0/INTO, RB1/INT1,and RB2/INT2 (edge
driven)
• Change in logic levels of pins RB4-RB7 of PORTB can be
recognized as interrupts
• Internal peripheral sources
• Timers, A/D Converter, Serial I/O, and Low-Voltage Detection
Module
12
PIC18 Interrupt Sources
• To recognize the occurrence of an interrupt request, the
MPU needs to check the following three bits:
• The flag bit (IF) to indicate that an interrupt request is present
• Automatically set by hardware if an interrupt occur
• Must be clear in interrupt service function manually
• The enable bit (IE) to redirect the program execution to the interrupt
vector address
• The priority bit (IP) (if set) to select priority
Interrupt
IF
event
& & CPU
IE
𝐈𝐏/𝐈𝐏 GIE
13
Interrupt Diagram
14
Interrupt Priority
15
Interrupt registers
10 SFRs used to setup the interrupt process:
• RCON Register Control (global priority)
• INTCON Interrupt Control
• INTCON2 Interrupt Control2 Handle external sources
• INTCON3 Interrupt Control3
• PIR1 and PIR2 Peripheral Interrupt Register1 & 2 Handle
• PIE1 and PIE2 Peripheral Interrupt Enable 1 & 2 internal
sources
• IPR1 and IPR2 Interrupt Priority Register 1 & 2
16
Interrupt registers
• RCON
• Enable interrupt priority by setting bit IPEN to 1
• C code:
RCONbits.IPEN = 1;
17
Interrupt registers
• INTCON
• C code
ADCON1 |=0x0F; // make all IO pins digital
TRISB = 1; // make RB0 input
RCONbits.IPEN = 1;
INTCONbits.INT0IE = 1; // enable INT0 interrupt
INTCONbits.GIEH = 1; // enable high priority
19
Interrupt registers
• INTCON2
• C code
INTCON2bits.INTEDG0 = 1; //make INT0 positive edge trigger
20
Interrupt registers
• INTCON3
INTxIP: INTx External Interrupt INTxIE: INTx External Interrupt INTxIF: INTx External Interrupt
Priority bit, x = 1~2 Enable bit, x = 1~2 Flag bit, x = 1~2
1 = High priority 1 = Enable INTx interrupt
0 = Low priority 0 = Disable INTx interrupt
21
Peripheral Interrupt Register
• Each peripheral interrupt is control by 3 bit IF, IE, IP in peripheral
registers
• Interrupt Flag register 1
22
Peripheral Interrupt Register
• Interrupt Flag register 2
23
Writing an ISR in C
• An interrupt service routine is recognized by the keyword
interrupt, followed by the name of the routine.
void interrupt Myint(void) Interrupt function
{
//Body of the interrupt service routine
//Clear interrupt flags
}
• The interrupt priority can be specified after the keyword
interrupt
void interrupt low_priority Myint(void){
}
void interrupt high_priority Myint(void){
}
• It is important to place as little code as possible inside the
interrupt function.
24
Writing an ISR in C
• The interrupt function may have to deal with multiple interrupt
sources
• Check the interrupt flag and enable bit to determine which interrupt is
called
int tick_count;
void high_priority interrupt tcInt(void)
{
if (TMR0IE && TMR0IF) { // any timer 0 interrupts?
TMR0IF=0;
++tick_count;
}
if (TMR1IE && TMR1IF) { // any timer 1 interrupts?
TMR1IF=0;
tick_count += 100;
}
// process other interrupt sources here, if required
return;
}
25
INTx Pin Interrupts
• 3 External interrupts on
• RB0/AN12/INT0/FLT0/SDI/SDA
• RB1/AN10/INT1/SCK/SCL
• RB2/AN8/INT2/VMO
• These interrupts are edge trigger
27
Example
28