Chapter 4 - Interrupt Module
Chapter 4 - Interrupt Module
CHAPTER 4
INTERRUPTS MODULE
HCMUT - 2011
INTERRUPTS
Definition
What is interrupt?
“Interrupt is the signal sent to the micro to mark the event that
requires immediate attention”
2
2011 – Vo Tuong Quan
INTERRUPTS
How many types of connection between MCU and
external devices?
1. Polling
- MCU accesses at the exact time interval the external
device, and gets the required information.
- The processor must access the device itself and request
the desired information that is needed to be processed.
- Waste of time: The micro needs to wait and review whether
a new information arrived.
2. Interrupt
- Interrupt is the signal sent to the micro to mark the event
that requires immediate attention.
- Interrupt is “asking" the processor to stop to perform the
current program and to “make time” to execute a special
code.
3
2011 – Vo Tuong Quan
INTERRUPTS
Interrupt sources?
- Software Interrupt
Example: Procedure - when there is a procedure call, the
processor stops the execution of the program, jumps to the
place in memory that reserved for a procedure – executes
the procedure and only then returns back to the program
and continues to execute.
- Hardware Interrupt
The hardware interrupts are sent to the microcontroller by
external hardware devices.
4
2011 – Vo Tuong Quan
INTERRUPTS
Interrupt priority?
5
2011 – Vo Tuong Quan
INTERRUPTS
Interrupt types
There are normally 14 interrupt type
XXXIF is an interrupt flag that shows the result that we are
getting from an interrupt.
XXXIE is an interrupt enable bit that is used to enable or
“block” the interrupt.
We can determine whether or not to allow the system to
address the interrupts. This is done by using Global
Interrupt Enable bit GIE.
6
2011 – Vo Tuong Quan
INTERRUPTS
Interrupt types
1. EEIF - EEPROM Write Operation Interrupt Flag bit
2. PSPIF – Parallel Slave Port Read/Write Interrupt Flag bit
3. ADIF - A/D Converter Interrupt Flag bit
4. RCIF - USART Receive Interrupt Flag bit
5. TXIF - USART Transmit Interrupt Flag bit
6. SSPIF - Synchronous Serial Port (SSP) Interrupt Flag bit
7. CCP1IF - CCP1 Interrupt Flag bit
8. TMR2IF - TMR2 to PR2 Match Interrupt Flag bit
9. TMR1IF - TMR1 Overflow Interrupt Flag bit
10.CCP2IF - CCP2 Interrupt Flag bit
11.BCLIF - Bus Collision Interrupt Flag bit
12.T0IF – Timer 0 interrupt flag
13.INTIF - RB0/INT External Interrupt Flag bit
14.RBIF - RB Port Change Interrupt Flag bit
7
2011 – Vo Tuong Quan
INTERRUPTS
Interrupt types
What are the difference between RBIF and EXT (External
Interrupts)?
8
2011 – Vo Tuong Quan
INTERRUPTS
Interrupt function
What are the functions of interrupts?
9
2011 – Vo Tuong Quan
INTERRUPTS
Interrupt function
The interrupt functions is called by the hardware itself and
operates by following steps:
12
2011 – Vo Tuong Quan
INTERRUPTS
Example 1 (Cont’d) – Sample code
void main(void)
{
TRISB=0; // Set PORT B as an OUTPUT
TRISC=0; // Set PORT C as an OUTPUT
PORTC=0; // Set the value of PORT C to 0
INTIF=0; // Reset the external interrupt flag
INTIE=1; // Enable external interrupts from RB0
GIE=1; // Global interrupt enable
while(1)
{
PORTB++;// Increase the PORT B value by 1.
for(i=0;i<30000;i++); // a simple delay
}
}
13
2011 – Vo Tuong Quan
INTERRUPTS
Example 2 - Read the circuit, the following code and show up
the purpose of the program
14
2011 – Vo Tuong Quan
INTERRUPTS
Example 2 (Cont’d)
15
2011 – Vo Tuong Quan
INTERRUPTS
Example 2 - Read the circuit, the following code and add in
more sub programs to make the full program.
16
2011 – Vo Tuong Quan
INTERRUPTS
Example 2 (Cont’d)
17
2011 – Vo Tuong Quan
INTERRUPTS
Example 2 (Cont’d)
What should we input more in the following program?
18
2011 – Vo Tuong Quan
INTERRUPTS
Using functions of CCS-C Compiler
enable_interrupts ( level )
disable_interrupts ( level )
ext_int_edge ( source , edge )
19
2011 – Vo Tuong Quan
INTERRUPTS
Example 3:
Find the errors in the circuit and codes and show the solutions.
20
2011 – Vo Tuong Quan
INTERRUPTS
Example 3: (Cont’d)
#include < 16F877.h > void main ( )
#device PIC16F877 *=16 { set_tris_b ( 0xF0 ) ;
#use delay (clock = 20000000 ) set_tris_d ( 0x00 ) ;
#byte portb = 0x06 enable_interrupts ( INT_RB ) ;
#byte portd = 0x08 enable_interrupts ( GLOBAL ) ;
while( true )
#INT_RB {
Void RB_LED ( ) }
{ }
portd=portb;
}
21
2011 – Vo Tuong Quan