11-1
An
sh
ul
Sh ar m
a
Chapter 11: Interrupts programming in Assembly
PIC Microcontroller and Embedded Systems Muhammad Ali Mazidi, Rolin McKinlay and Danny Causey, February 2007.
Introduction
11-2
Interrupts are mechanisms which enable instant response to events such as counter overflow, pin change, data received, etc.
In normal mode, microcontroller executes the main program as long as there are no occurrences that would cause an interrupt.
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
a
11.1:PIC18 interrupts PIC can serve multiple devices using mechanisms of
a Sh ar m
PIC continuously monitors the status of each device
Each device get the attention of the U as the same level of priority
Wastes u-Controllers time by polling devices that do not need service.
Interrupt
ul
Polling
Devices get the attention of the U only when it needs a service
Can service many devices with different level of priorities
sh
An
11-3
When an interrupt is invoked the uC runs the Interrupt Service Routine(ISR)
Interrupt vector table holds the address of ISRs
sh
ul
Sh ar m
a
Interrupt service routine (ISR)
Power-on Reset 0000h
High priority interrupt 0008h
Low priority interrupt 0018h
An
11-4
Steps in executing an interrupt
Sh ar m
a
Upon activation of interrupt the microcontroller Finishes executing the current instruction
Pushes the PC of next instruction in the stack
Jumps to the interrupt vector table to get the address of ISR and jumps to it
Begin executing the ISR instructions to the last instruction of ISR (RETFIE)
Executes RETFIE
sh
ul
An
Pops the PC from the stack
Starts to execute from the address of that PC
11-5
An
sh
ul
Sh ar m
a
Program organization in MPLAB
11-6
Sources of interrupts in PIC18 External hardware interrupts Pins RB0(INT0),RB1(INT1),RB2(INT2)
PORTB change
Timers
a
Sh ar m
Timer0 , Timer1 ,Timer2
ADC (analog to digital converter)
C (compare capture pulse width modulation, PWM)
Serial port TX and RC.
... etc
An
sh
ul
11-7
Enabling and disabling an interrupt
a
When the PIC is powered on (or resets) All interrupts are masked (disabled)
The default ISR address is 0008h
ul
interrupt priorities for interrupts
sh
No
Sh ar m
An
11-8
Enabling and disabling an interrupt In general, interrupt sources have three bits to control their operation. They are:
Flag bit
Sh ar m
a
that allows program execution to branch to the interrupt vector address when the flag bit is set
Priority bit
sh
Enable bit
An
ul
to indicate that an interrupt event occurred
to select high priority or low priority
11-9
Steps in enabling an interrupt Set the GIE bit from INTCON REG
Set the IE bit for that interrupt
If the interrupt is one of the peripheral (timers 1,2 , serial,etc ) set PEIE bit from INTCON reg
An
sh
ul
Sh ar m
a
1110
Sh ar m
a
11 11
An
sh
ul
Show the instructions to : (a) enable (unmask) the Timer 0 interrupt and external hardware interrupt 0 (INT0) (b) disable (mask) the Timer 0 interrupt (c) show how to disable (mask) all the interrupts with a single instruction.
PROGRAMMING TIMER INTERRUPTS Rollover timer flag and interrupt
sh
ul
Sh ar m
a
INTCON with Timer 0 Interrupt Enable and Interrupt Flag
An
•
Following points should be Noticed about Programing a timer:
An
sh
ul
Sh ar m
a
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 and then enter som other task which will keep the U 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 ) to indicate that it can accept new interrupts.
Programming Interrupt in C
11 14
Use "#pragma code" to place code at a specific ROM address.
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
This is done as follows:
#pragma code high_ vector =0x0008
GOTO my_isr endasm
sh
asm
An
{
// High-priority interrupt location
ul
void My_ HiVect_ Int (void)
Sh ar m
a
} #pragma code
//End of code
Now we redirect it from address location 00008 to another program to find the source of the interrupt and finally to the ISR.
This is done with the help of the keyword interrupt as follows: //interrupt is reserved keyword
void my_isr (void)
//used for high-priority interrupt
Sh ar m
a
#pragma interrupt my _isr {
//C18 places RETFIE here automatically due to interrupt keyword
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
#define myPB1bit PORTBbits.RB1 #define myPB7bit PORTBbits.RB7 void T0_ISR(void); void T1_ISR(void); #pragma interrupt chk_isr
//used for high-priority interrupt only
a
void chk_isr (void)
Sh ar m
{
//Timer0 causes interrupt?
T0_ISR();
/Yes. Execute Timer0 ISR
if(PIR1bits.TMR1IF==1)
//Or was it Timer1?
T1ISR();
//Yes. Execute Timer1 ISR
sh
}
ul
if (INTCONbits.TMR0IF==1)
void My_ HiPrio_ Int (void) {
// High-priority interrupt location
An
#pragma code My_ HiPrio_ Int =0x0008
_asm GOTO my_isr _endasm } code #pragma
//End of code
ul
sh
An
a
Sh ar m
//load TH0
TMR1L=0x00;
//load TL0
PIR1bits.TMR1IF=0;
//clear TF1
An
sh
ul
Sh ar m
a
TMR1H=0x35;
An
sh
ul
Sh ar m
a
External hardware interrupt
Example : When a rising edge of the signal is applied to pin INT0, the LED will toggle
ul
sh
An
a
Sh ar m
sh
ul
Sh ar m
a
Negative Edge-triggered interrupts
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
a
Sh ar m
Serial Communication Interrupts Flag Bit
Enable Bit
TXIF (Transmit)
TXIF
PIR1
TXIE
PIE1
RCIF (Receive)
RCIF
PIR1
RCIE
PIE1
ul
Sh ar m
a
Interrupt
An
sh
Serial Port Interrupt Flag Bits and Associated s
PIE1 Bits Holding TXIE and RCIE
Serial Interrupt Enable Flags Example :
An
sh
ul
Sh ar m
a
PICl8 gets data from PORTD and sends it to TXREG continuously while incoming data from the serial port is sent to PORTB. We assume that XTAL = 10 MHz and the baud rate= 9600.
ul
sh
An
a
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.
( c) While each of the INTO-INT2 interrupts can be programmed to trigger on the negative or positive edge, the PORTB-Change interrupt causes an interrupt if any of its pins changes status from HIGH to LOW, or LOW to HIGH.
An
sh
ul
Sh ar m
a
a Sh ar m ul sh An PORTB-Change Interrupt Pins
An
sh
ul
Sh ar m
a
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
a
Sh ar m