You are on page 1of 27

4

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

Copyright Politeknik Kota Bharu Page 1


void main (void)
{
TRISC=0b00000011;
TRISD=0x00;
while (1)
{
if (SW1==0)
{
PORTD++;
}
If (SW2==0)
{
PORTD--;
}
}

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.

Copyright Politeknik Kota Bharu Page 2


 Polling method: The microcontroller continuously monitors the status of a given device;
when the status condition is met, it performs the service.
 The advantage of interrupts: microcontroller can serve many devices which each device can
get the attention based on the priority. Interrupts are used to avoid tying down the
microcontroller.
 The polling method cannot assign priority because it checks all devices in a round-robin
fashion.

Review Questions

1. Describe interrupt and pooling method in a microcontroller

2. Define the terms below:


a. Interrupt

b. Interrupt service routine (ISR)

Copyright Politeknik Kota Bharu Page 3


3. Give TWO advantage of interrupt

4. Explain the difference between the interrupt method and pooling method

5. Explain how microcontroller serves device by interrupting signal and pooling methods

Interrupt Service Routine (ISR)

 For every interrupt there is an ISR, or interrupt handler.


 Interrupt Service Routine (ISR) is a software routine in microcontroller firmware, operating
system or device driver whose execution is triggered by the reception of an interrupt
 The microcontroller gets the address of ISR from interrupt vector table.
 Interrupt vector table is the group of memory location set aside to hold the addresses of ISR
 In the PlC18, there are only two locations for the interrupt vector table, locations 0008h (High
Priority Interrupt) and 0018h (Low Priority Interrupt).

Copyright Politeknik Kota Bharu Page 4


Interrupt vector table for PIC18

Steps in executing an interrupt

 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.

Sources of interrupts in PIC18

There are six sources of interrupts, most widely used:

1. An interrupt for each of the Timers


2. Three interrupts are for external hardware interrupts. RB0, RB1, RB2, for interrupts INT0, INT1,
INT2
3. Serial communication’s USART has two interrupts, one for receive and one for transmit
4. The PORTB-Change interrupt

Copyright Politeknik Kota Bharu Page 5


5. The ADC (analog to digital converter)
6. The CCP (compare capture pulse-width-modulation)

Enabling and Disabling Interrupt

 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.

Copyright Politeknik Kota Bharu Page 6


 To enable any one of the interrupt:
o Bit 7 (GIE) of the INTCON register must be set to HIGH
o If GIE=1, all interrupt will be enable
o If GIE=0, all interrupt will be disable
o For some interrupts such as TMR1IF, TMR2IF, TXIF, ……. We also need to set the PEIE
(Peripheral Interrupt Enable)

Figure 4.5: Timer Interrupt Enable Flag

How interrupts are managed?

 In general each interrupt source have following related bits.

Copyright Politeknik Kota Bharu Page 7


o Enable Bit – There are suffixed with IE (Interrupt Enable) example TMR0IE stands for
TIMER0 Interrupt Enable. It can be used to enable/disable the related interrupt. When
set to ’1′ it enables the interrupt.
o Flag Bit – It is set automatically by the related hardware when the interrupt condition
occurs. It is generally suffixed with IF (Interrupt Fag). When it is set to ’1′ we know that
interrupt has occurred. For example when TMR0IF is set by TIMER0, it indicates that
TIMER0 has overflowed.
o Priority Bit – Parity bit is to select high or low priority interrupt. If IP=0, select low
priority interrupt. If IP=1, select high priority interrupt. We won’t be using interrupt
priority feature of PIC18.

Review Questions

1. List FIVE (5) sources of interrupt in the PIC18F

2. Show the steps in executing an interrupt

3. Explain why pooling method cannot assign priority

Copyright Politeknik Kota Bharu Page 8


4. Describe in detail the function of GIE (Global Interrupt Enable) bit in INTCON (Interrupt
Control Register) register

5. Interpret the functions of Interrupt Enable (IE), Interrupt Flag (IF) and Interrupt Priority (IP)
bit for PIC18 interrupt operation

External Hardware Interrupt

 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

Copyright Politeknik Kota Bharu Page 9


Figure 4.6: INT0 – INT2 External Hardware Interrupt

 To enable External Hardware Interrupt 0, INT0


o Bit GIE set to HIGH
o Bit INT0IE set to HIGH
o Bit INT0IF set to LOW
 To enable External Hardware Interrupt 1, INT1
o Bit GIE set to HIGH
o Bit INT1IE set to HIGH
o Bit INT1IF set to LOW
 To enable External Hardware Interrupt 2, INT2
o Bit GIE set to HIGH
o Bit INT2IE set to HIGH
o Bit INT2IF set to LOW

Copyright Politeknik Kota Bharu Page 10


Example 4.2
Connect a switch to INT0 and LED to pin RB7. In this program, every time INT0 is activated, it
toggles the LED while at the same time PORTD runs as a running light
#include <xc.h>
#define _XTAL_FREQ 64000000
#define mybit RB7
void chk_isr (void);
void INT0_ISR (void);

#pragma interrupt chk_isr

void chk_isr (void)


{
if(INT0IF==1)
INT0_ISR( );
}

#pragma code My_HiPrio_Int=0x08

void My_HiPrio_Int (void)


{
_asm
GOTO chk_isr
_endasm
}
#pragma code

void main(void)
{
ADCON1=0x0E;
TRISB7=0;
TRISB0=1;

Copyright Politeknik Kota Bharu Page 11


TRISD = 0x00;
INT0IF = 0;
INT0IE = 1;
GIE = 1;
while (1)
{
LATD=0x01;
__delay_ms(500);
LATD=0x02;
__delay_ms(500);
LATD=0x04;
__delay_ms(500);
LATD=0x08;
__delay_ms(500);
LATD=0x10;
__delay_ms(500);
LATD=0x20;
__delay_ms(500);
LATD=0x40;
__delay_ms(500);
LATD=0x80;
__delay_ms(500);
}
}
void INT0_ISR (void)
{
mybit = ~mybit;
INT0IF = 0;
}

Review Question

1. What pins are assigned to INT0 – INT2

Copyright Politeknik Kota Bharu Page 12


2. Show how to enable
a. INT0

b. INT1

c. INT2

3. Refer to Figure 4.6, show the instruction needed to


a. Enable Hardware Interrupt 0, INT0

b. Enable Hardware Interrupt 1, INT1

c. Enable Hardware Interrupt 2, 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.

Copyright Politeknik Kota Bharu Page 13


Figure 4.8

Copyright Politeknik Kota Bharu Page 14


5. Build a C language program for interface with external hardware interrupt pin RB0. An LED
that connected to pin RD0 will toggle every time the INT0 is activated

Copyright Politeknik Kota Bharu Page 15


6. Build a C language program for interface with external hardware interrupt pin RB1. The
buzzer connected at pin RB7 is turn on and off at the same rate as the pulse are applied for
INT1 pin

Copyright Politeknik Kota Bharu Page 16


Timer Interrupt

 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

INTCON register with Timer0 Interrupt Enable and Interrupt Flag

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 T0_ISR (void);


void T1_ISR (void);

#pragma interrupt chk_isr

Copyright Politeknik Kota Bharu Page 17


void chk_isr (void)
{
if (TMR0IF==1)
T0_ISR();
if (TMR1IF==1)
T1_ISR();
}

#pragma code My_HiPrio_Int=0x08

void My_HiPrio_Int (void)


{
_asm
GOTO chk_isr
_endasm
}
#pragma code

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;
}

Copyright Politeknik Kota Bharu Page 18


}

void T0_ISR (void)


{
LED1=~LED1;
TMR0H = 0x35;
TMR0L = 0x00;
TMR0IF = 0;
}

void T1_ISR (void)


{
LED7=~LED7;
TMR1H = 0x35;
TMR1L = 0x00;
TMR1IF = 0;
}

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.

 Widely use in keypad interfacing

Copyright Politeknik Kota Bharu Page 19


 This might be used as a power saving technique. The processor is placed in the sleep mode
and is activated only when the user changes the state of any on the four inputs. The processor
then performs the defined task and goes back to sleep.

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

Copyright Politeknik Kota Bharu Page 20


#include<xc.h>
#define LED1 RC6
#define LED2 RC7
#define SW1 RB4
#define SW2 RB5

void chk_isr (void);


void RBINT_ISR (void);

#pragma code My_HiPrio_Int=0x0008

void My_HiPrio_Int (void)


{
_asm
GOTO chk_isr
_endasm
}
#pragma code
#pragma interrupt chk_isr

void chk_isr (void)


{
If (RBIF==1)
RBINT_ISR( );
}

void main(void)
{

Copyright Politeknik Kota Bharu Page 21


ADCON1=0x0E;
TRISC6 = 0;
TRISC7 = 0;
TRISB4 = 1;
TRISB5 = 1;
RBIF = 0;
RBIE = 1;
GIE = 1;
while (1);

void RBINT_ISR (void)


{
LED1=SW1;
LED2=SW2;
RBIF = 0;
}

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

Copyright Politeknik Kota Bharu Page 22


Copyright Politeknik Kota Bharu Page 23
Copyright Politeknik Kota Bharu Page 24
Copyright Politeknik Kota Bharu Page 25
Copyright Politeknik Kota Bharu Page 26
Copyright Politeknik Kota Bharu Page 27

You might also like