Professional Documents
Culture Documents
INTERRUPT PROGRAMMING
IN C
4.1 Know PIC Interrupt
Introduction
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
PIC can serve several devices. There are two methods by which device receive service from
microcontroller
o Polling
o Interrupt
Polling method
PIC continuously monitors the status of a given device; when the status condition is met, it
perform the service
After that, it move on to monitor the next device until each one is serviced
Each device get the attention of the CPU as the same level of priority
Wastes microcontrollers time by polling devices that do not need service
Example 4-1
The status of SW1 and SW2 are monitored in “main function”
#include <xc.h>
#define SW1 RC0
#define SW2 RC1
Interrupt Method
Whenever any device needs the microcontroller’s service, the device notifies it by sending an
interrupt signal. Upon receiving an interrupt signal, the microcontroller stops whatever it is doing
and serves the device. The program associated with interrupt is called Interrupt Service Routine
(ISR).
An Interrupt is a signal to the processor (microcontroller) emitted by hardware or software
indicating an event that needs immediate attention
The processor responds by suspending its current activities, saving its state, and executing a
small program called an interrupt handler (or interrupt service routine, ISR) to deal with the
event
After the interrupt handler finishes, the processor resumes execution of the previous thread
Devices get the attention of the CPU only when it needs a service
Can serve many devices with different level of priorities
Interrupt vs Polling
Interrupt method: The microcontroller stops whatever it is doing upon receiving an interrupt
signal and serves the device. The program associated with the interrupt is called the interrupt
service routine (ISR) or interrupt handler.
Review Questions
4. Explain the difference between the interrupt method and pooling method
5. Explain how microcontroller serves device by interrupting signal and pooling methods
Upon activation of an interrupt, the microcontroller goes through the following steps:
1. Microcontroller finishes the instruction it is executing and saves the address of next
instruction.
2. It jumps to interrupt vector table.
3. Microcontroller gets the address of ISR and jumps to it.
4. It starts to execute the interrupt service routine until it is finish.
5. The microcontroller return to the place where it was interrupted. Then it starts to execute
from that address.
Upon reset, all interrupts are disabled (masked), meaning that none will be responded to by
the microcontroller if they are activated. The interrupts must be enabled (unmasked) by
software in order for the microcontroller to respond to them.
The D7 bit of the INTCON (Interrupt Control) register is responsible for enabling and disabling
the interrupts globally.
Review Questions
5. Interpret the functions of Interrupt Enable (IE), Interrupt Flag (IF) and Interrupt Priority (IP)
bit for PIC18 interrupt operation
PIC18 has three external hardware interrupt: INT0, INT1 and INT2.They are located on pins
RB0, RB1 and RB2
All three hardware interrupt are directed to vector table location 0x0008, unless we specify
otherwise
void main(void)
{
ADCON1=0x0E;
TRISB7=0;
TRISB0=1;
Review Question
b. INT1
c. INT2
4. Referring to Figure 4.8, build a C program with input on Pin RB1 (INT1) is connected to a
pulse generator and output on pin RB7 is connected to the LED. This program will toggle the
LED on the falling edge of the pulse. LED is turned on and off at the same rate as the pulses
applied to the INT1 pin.
In chapter 3, we discussed how to use Timer0, 1, 2 and 3 with the pooling method. In pooling
TMR0IF, we have to wait until TMR0IF is raised. The problem with this method that the
microcontroller is tied down waiting for TMR0IF to be raised, and cannot do anything else.
If the timer interrupt in the interrupt register is enabled, TMR0IF is raised whenever the timer
rolls over and the microcontroller jumps to interrupt vector table to service ISR. In this way
the microcontroller can do other thing until it is notified that the timer has rolled over
To use an interrupt instead of polling:
o Enable the interrupt
o Enable the interrupt for the specific timer
Example 4.3
This program uses Timer0 (16-bit mode, and no prescaler) and Timer1 (16-bit mode, and no
prescaler) to generate square waves on pin RB1 and RB7 respectively while data is being
transfer from PORTC to PORTD
#include<xc.h>
#define LED1 RB1
#define LED7 RB7
void main(void)
{
ADCON1=0x0E;
TRISB1=0;
TRISB7=0;
TRISC = 0xFF;
TRISD = 0x00;
T0CON = 0x80; //timer0, 16 bit mode, no prescalar
TMR0H = 0x35;
TMR0L = 0x00;
T1CON = 0x88; //timer1, 16 bit mode, no prescalar
TMR1H = 0x35;
TMR1L = 0x00;
TMR0IF = 0;
TMR1IF = 0;
TMR0IE = 1;
TMR1IE = 1;
TMR0ON = 1;
TMR1ON = 1;
PEIE = 1;
GIE = 1;
while (1)
{
PORTD=PORTC;
}
Review Question
1. Write a program using timer 0 (16 bit mode, no prescalar) interrupt to create a square
wave of 1 kHz on pin RB7 while data from PORTC is being sent to PORTD. Assume XTAL =
10 MHZ
PORTB-Change interrupt
The PORTB (RB4 – RB7) can cause an interrupt when any change is detected on any one of
them.
Example
We have connected 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 LED1 and LED2
void main(void)
{
Review question
1. We have connected a door sensor to pin RB4 and a buzzer to pin RC5. Every time the
door is opened, it sounds the buzzer