Académique Documents
Professionnel Documents
Culture Documents
An
sh
ul
Sh
ar
m
PIC Microcontroller
and Embedded Systems
Muhammad Ali Mazidi,
Rolin McKinlay and
Danny Causey, February
2007.
Introduction
11-2
Upon
interrupt,
microcontroller
stops
the
execution of main program and commences the
special part of the program(ISR) which will
analyze and handle the interrupt.
An
sh
ul
Sh
ar
m
11.1:PIC18 interrupts
PIC can serve multiple devices using mechanisms of
a
Sh
ar
m
Each device get the attention of the CPU as the same level of priority
Interrupt
ul
Polling
Devices get the attention of the CPU only when it needs a service
sh
An
11-3
When an interrupt is
invoked the uC runs the
Interrupt Service
Routine(ISR)
sh
ul
Sh
ar
m
An
11-4
Steps in executing an
interrupt
Sh
ar
m
Executes RETFIE
sh
ul
An
11-5
An
sh
ul
Sh
ar
m
11-6
PORTB change
Timers
Sh
ar
m
... etc
An
sh
ul
11-7
ul
sh
No
Sh
ar
m
An
11-8
Flag bit
Sh
ar
m
Priority bit
sh
Enable bit
An
ul
11-9
Steps in enabling an
interrupt
Set the GIE bit from
INTCON REG
An
sh
ul
Sh
ar
m
1110
Sh
ar
m
11
11
An
sh
ul
sh
ul
Sh
ar
m
An
An
sh
ul
Sh
ar
m
1. We must avoid using the memory space allocated to the interrupt vector table. Therefore, we place all
the initialization codes in memory starting at an address such as 100H. The GOTO instruction is the
first instruction that the PIC18 executes when it is awakened at address 00000 upon power-on reset
(POR).
2. In the MAIN program, we enable (unmask) the Timer 0 interrupt, TMR0IE followed by the GIE to
enable all interrupts globally.
3. In the MAIN program, we initialize the Timer 0 register and then enter som other task which will
keep the CPU busy.
For example , the loop gets data from PORTC and sends it to PORTD. While the PORTC
data is brought in and issued to PORTD continuously, the TMR0IF flag is raised as soon as
Timer 0 rolls over, and the microcontroller gets out of the loop and goes to 00008H to
execute the ISR associated with Timer0.
At this point, the PIC18 clears the GIE bit (D7 of INTCON) to indicate that it is currently
serving an interrupt and cannot be interrupted again; in other words, no interrupt inside the
interrupt.
4. The ISR for Timer0 is located starting at memory location 00200H because it is too large to fit into
address space 08-l7H, the address allocated to high-priority interrupts.
5. In the ISR for Timer0, clearing of TMR0IF is needed before the RETFIE instruction. This will ensure
that a single interrupt is serviced once and is not recognized as multiple interrupts.
6. RETFIE must be the last instruction of the ISR. Upon execution of the RETFIE instruction, the PIC18
automatically enables the GIE (D7 of the INTCON register) to indicate that it can accept new
interrupts.
Programming Interrupt in C
11
14
Because the Cl8 does not place an ISR at the interrupt vector table automatically,
we must use Assembly language instruction GOTO at the interrupt vector to
transfer control to the ISR
GOTO my_isr
endasm
sh
asm
An
ul
Sh
ar
m
}
#pragma code
//End of code
Sh
ar
m
EXAMPLE
sh
ul
An
Use Timer 0 and Timer 1 interrupts to generate square waves on pins RB1 and
RB7, respectively, while data is being transferred from PORTC to PORTD.
#include <p18F458.h>
#define myPB1bit PORTBbits.RB1
#define myPB7bit PORTBbits.RB7
void T0_ISR(void);
void T1_ISR(void);
#pragma interrupt chk_isr
Sh
ar
m
T0_ISR();
if(PIR1bits.TMR1IF==1)
T1ISR();
sh
ul
if (INTCONbits.TMR0IF==1)
An
_asm
GOTO my_isr
_endasm
} code
#pragma
//End of code
ul
sh
An
Sh
ar
m
//load TH0
TMR1L=0x00;
//load TL0
PIR1bits.TMR1IF=0;
//clear TF1
An
sh
ul
Sh
ar
m
TMR1H=0x35;
An
sh
ul
Sh
ar
m
Example :
When a rising edge of the signal is
applied to pin INT0, the LED will
toggle
ul
sh
An
Sh
ar
m
sh
ul
Sh
ar
m
An
Example :
Pin RBI (INTI) is connected to a pulse generator
and the pin RB7 is connected to an LED. The
program will toggle the LED on the falling edge of
the pulse
ul
sh
An
Sh
ar
m
Serial Communication
Interrupts
Flag Bit
Register
Enable Bit
Register
TXIF
(Transmit)
TXIF
PIR1
TXIE
PIE1
RCIF
(Receive)
RCIF
PIR1
RCIE
PIE1
ul
Sh
ar
m
Interrupt
An
sh
An
sh
ul
Sh
ar
m
ul
sh
An
Sh
ar
m
PORTB-CHANGE INTERRUPT
Differences between the PORTB-Change interrupt and INTO-INT2
interrupts:
(a) Each of the INTO-INT2 interrupts has its own pin and is
independent of the others. These interrupts use pins PORTB.O
(RBO), PORTB. l (RB I), and PORTB.2 (RB2), respectively. The
PORTB-change interrupt uses all four of the PORTB pins RB4PB7 and is considered to be a single interrupt even though it can
use up to four pins.
(b) While each of the INTO-INT2 interrupts has its own flag, and
is independent of the others, there is only a single flag for the
PORTB-Change interrupt.
An
sh
ul
Sh
ar
m
a
Sh
ar
m
ul
sh
An
PORTB-Change Interrupt Pins
An
sh
ul
Sh
ar
m
Example :
Connect SW1 and SW2 to pins RB4 and RB5 respectively. In this
program, the activation of SW1 and SW2 will result in changing
the state of LED I and LED2 respectively.
PORTB-Change Interrupt
ul
sh
An
Sh
ar
m