0% found this document useful (0 votes)
132 views28 pages

TOPIC4 - Part 1 - External HW Interrupt

The code enables the external interrupt INT0 on pin RB0 by setting the GIE, INT0IE, and INTEDG0 bits. It defines an interrupt service routine (ISR) that toggles the LED on pin RD0 when the INT0 interrupt flag is set. The ISR also clears the interrupt flag. This will cause the LED to toggle each time the button is pressed due to the interrupt triggering on the rising edge of the button input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
132 views28 pages

TOPIC4 - Part 1 - External HW Interrupt

The code enables the external interrupt INT0 on pin RB0 by setting the GIE, INT0IE, and INTEDG0 bits. It defines an interrupt service routine (ISR) that toggles the LED on pin RD0 when the INT0 interrupt flag is set. The ISR also clears the interrupt flag. This will cause the LED to toggle each time the button is pressed due to the interrupt triggering on the rising edge of the button input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

DEC40053 – EMBEDDED SYSTEM APPLICATIONS

TOPIC 4
INTERRUPT
Part 1: External Hardware Interrupt
2
LEARNING OUTCOMES
At the end of this session, student should be able
to:

1 Describe interrupt and polling methods in a


microcontroller.
2 List source of interrupts in the PIC.

3 Describe interrupt enabling and disabling


process.

4 Write program code for Interrupt Service


Routine (ISR)
Apply C program to interface with external
5 hardware interrupt
3
INTRODUCTION

 Embedded systems have to handle real world events such as


the detection of a key being pressed, synchronization of
video output, or handle the transmission and reception of
data packets for a communication device. These events have
to be handled in real time, which means an action has to
take place within a particular time period to process the
event.

 Almost all the real time applications are implemented using


Interrupts.
4
INTERRUPT

• Interrupt is a signal sent by peripheral or software to a processor that


requires the CPU to stop the current program execution and perform
some service related to the event.

• The program associated with the interrupt is called the interrupt service
routine (ISR) or interrupt handler.
 An Interrupt requires immediate attention, only once the microcontroller
will finish executing the interrupt code, then it can go back to continue
with the main program.
5
POLLING
 The processor can also serve these events by polling method. But polling
is an inefficient technique as compared to interrupts.
 In the polling method, the processor has to continuously wait for the
event signal. Thus it always remains busy using extra resources.
 Although polling can monitor the status of several devices and serve
each of them as certain condition are met, this method wastes much of
the microcontroller’s time by polling devices that do not need service.

void main(void)
{
TRISB=0b00000111;
TRISD=0X00;
ADCON1=0XFF;
while (1)
{
if (SW1==0) // check if SW1 pressed
count++; POLLING
else if (SW2==0) //check if SW2 pressed
count--;
}
}
6
INTERRUPT vs POLLING

Interrupt Polling
 Services based on request  Monitors status of device and
from devices serves
 Service based on priority. CPU  No priority, round robin.
drops whatever it was doing When it sees a request, it
and serves the device and serves the device and then
then return back to its keeps polling.
original task.  CPU is always ‘busy’ with
 CPU is always ‘free’, when not polling doing the “while any
serving any interrupt request” loop
7
SOURCES OF INTERRUPT IN PIC18F4550

Example of source of interrupts:


For this chapter we will discuss
1. Timers (Timer 0, 1, 2) these two interrupts
2. External hardware interrupt : Pin
RB0 (INT0)/ RB1 (INT1)/ RB2
(INT2)
3. The PORTB-Change interrupt:
Pin RB7 - RB4
4. Interrupts for serial
communication USART (Receive
and Transmit) : Pin RC6 (TX) &
RC7 (RX)
5. The CCP (Compare Capture
PWM)
6. The ADC (Analog-to-Digital
Converter)
7. And many more…
Bits to control interrupt operation:

Each interrupt source has three bits to control its operation:

1. Flag bit to indicate that an interrupt event occurred (eg.


INT0IF)
2. Enable bit that allows program execution to branch to
the interrupt vector address when the flag bit is set (eg.
INT0IE)
3. Priority bit to select high priority or low priority (eg.
INT1IP)
9
PIC18 INTERRUPT REGISTER

 There are ten registers which are used to control


interrupt operation :

 INTCON
 INTCON2
 INTCON3
 PIR1, PIR2
 PIE1, PIE2
 IPR1, IPR2
 RCON
INTCON REGISTER 10

 This register is used to configure the interrupt control logic circuitry.


 Bits 0 to 6 are used to configure the interrupt enable/disable status and the
interrupt flags for the four interrupt sources.

The GIE bit is the bit 7 of


INTCON and when set,
enables all un-masked
GIE=1; // enable all interrupt globally
interrupts. INT0IE=1; // enable external interrupt INT0
TMR0IE=1; // enable Timer0 interrupt
No interrupt to the CPU when
GIE bit is clear.
INTCON2 REGISTER 11

 External interrupts on the RB0, RB1 and RB2 pins are positive (rising) edge-
triggered.
 If the corresponding INTEDGx bit in the INTCON2 register is set (= 1), the interrupt
is triggered by a rising edge;
 if the bit is clear, the trigger is on the falling edge (negative edge-triggered).

INTEDG0=0; // makes INTEDG0 a negative edge-triggered interrupt, in which when a


// high-to-low signal is applied to pin RB0
INTCON3 REGISTER 12

 The PIC18F4550 device has multiple interrupt sources and an interrupt


priority feature that allows each interrupt source to be assigned a high priority
level or a low priority level.
 High priority interrupt events will interrupt any low priority interrupts that
may be in progress.

 Interrupt priority for INT1 and INT2 is determined by the value contained in the
interrupt priority bits, INT1IP (INTCON3) and INT2IP (INTCON3).
 There is no priority bit associated with INT0. It is always a high priority interrupt
source.
ENABLING AND DISABLING 13

INTERRUPT
PROGRAMMING EXTERNAL HARDWARE 1
4
INTERRUPT

 There are three pins of


external interrupts in the
PIC18:
1. INT0 (RB0)
2. INT1 (RB1)
3. INT2 (RB2)

 These pins are edge-triggered.


 The register associated external
hardware interrupt are INTCON,
INTCON2 and INTCON3
External
Interrupt
pins
ENABLING EXTERNAL HW 15

INTERRUPT
 All three external hardware interrupts must be enabled
before they can take effect.
 The following logic diagram shows the bits used to control its
operation.
STEP TO ENABLE EXTERNAL HARDWARE 16
INTERRUPT

1. Enable Global Interrupt Enable bit (GIE) Bit D7 of


the INTCON register must be set to HIGH.

2. Enables the INTxIE external interrupt.

3. Clear INTxIF

4. Set condition caused interrupt (falling/rising edge) at


bit INTEDGx
EXAMPLE 1 17

Write a program code to enable INT0 interrupt.


Interrupt will trigger when the input change from
LOW to HIGH.

Solution:

GIE=1; // Enable Global Interrupt Enable bit (GIE.)


INT0IE=1; //Enables the INT0 external interrupt
INT0IF=0; //clear interrupt flag
INTEDG0=1; // interrupt on rising edge;
18
Interrupt Service Routine (ISR)

Step to write ISR :

1. Declare function "void interrupt ISR()“

The words "interrupt" in the function causes it


becomes as Interrupt Service Routine (ISR) when
an interrupt occurs.

2. Check the related interrupt flag. Example:


if(INT0IF==1) //check if INT0 interrupt occurs

3. Write operation for the related interrupt


4. Clear the related interrupt flag. Example:
INT0IF=0; //clear INT0 interrupt flag
19
Interrupt Service Routine (ISR)

Example of ISR program code:

void interrupt ISR()


{
if(INT0IF==1) //check if INT0 interrupt occurs
{

Write operation for the related interrupt


}
INT0IF=0; //clear INT0 interrupt flag
}
//Toggled a LED using INTERRUPT
EXAMPLE 2 #include <xc.h>
20

//Configuration bits for PIC18F4550

Write a program to toggle a //Interrupt Service Routine (ISR)


void interrupt ISR();
LED on RD0 when the push
void main (void)
button on RB0 is pressed. {
Use interrupt method to TRISB0=1;
TRISD0=0;
monitor the input device ADCON1=0x0F;
(push button). //Enable Interrupt Register
GIE=1; // Enable Global Interrupt Enable bit (GIE.)
INT0IE=1; //Enables the INT0 external interrupt
INT0IF=0; //clear interrupt flag
INTEDG0=1; // interrupt on rising edge;

while(1); //Wait

void interrupt ISR()


{
if ( INT0IF==1 )
{
LATD0=LATD0^1; //Toggle LED
}
INT0IF=0; //Clear Interrupt Flag, ready for next interrupt
}
//SOLUTION

EXAMPLE 3 #include <xc.h>


#define _XTAL_FREQ 20000000
21

#define LED RA2


void interrupt ISR();

A control system use a LED and void main (void)


digital sensor as the following {
TRISB1=1;
diagram. In normal condition, the TRISA2=0;
ADCON1=0x0F;
LED will turn off. If the switch is
pressed, the LED on RA2 will blink //Enable Interrupt INT0
GIE=1; // Enable Global Interrupt Enable bit (GIE.)
3 times. Write a program for the INT1IE=1; //Enables the INT0 external interrupt
INT1IF=0;
control system using external INTEDG1=0;
hardware interrupt. INT1IP=1; //enable interrupt priority

while(1); //Wait
}

void interrupt ISR()


{
if ( INT1IF ==1 )
{
for(int i=0;i<3;i++)
{
LED= 1; //Turn on LED
__delay_ms(300); //delay 300ms
LED= 0; //Turn off LED
__delay_ms(300); //delay 300ms
}
}
INT1IF=0; //Clear Interrupt Flag, ready for next interrupt
}
Example 4 22

Refer to the following diagram. Write a program to read the


status of SW1 and SW2 as external hardware interrupt. When the
SW1 pressed, LED 1 will turn on and LED2 will turn off.
Otherwise, when SW2 pressed, LED1 will turn off LED2 turn on.
The interrupt is triggered by a falling edge of the pulse.
Example 4
Solution //Interrupt Service Routine (ISR)
#include <xc.h>
#define LED1 RB2 void interrupt INT_ISR()
#define LED2 RB3 {
#define _XTAL_FREQ 20000000 if(INT0IF ==1)
void interrupt INT_ISR();
{
LED1= 1; // turn on LED 1 and turn
void main (void)
off. LED2
{
LED2= 0;
TRISB=0b00000011;
__delay_ms(300); //delay 300ms
ADCON1=0x0F; INT0IF=0; //Clear IINT0IF
  }
//Enable Interrupt  
GIE=1; // Enable Global Interrupt if(INT1IF ==1)
Enable {
INT0IE=1; //Enables the INT0 interrupt LED1= 0; // turn off LED1 and turn
INT0IF=0; //clear INT0IF onLED2
INTEDG0=0; //interrupt on falling edge LED2= 1;
__delay_ms(300); //delay 300ms
INT1IP=1; //Enables the INT1 l interrupt
priority INT1IF=0; //Clear INT1IF,
INT1IE=1; //Enables the INT1 interrupt }
INT1IF=0; //clear INT1IF
}
ACTIVITY 1 24

Figure shows a push button switch is connected to the RB0/INT0 of PIC18F4550. This
switch produces the interrupt when pressed. Four red LEDs are connected to RC0
through RC3 port and an yellow LED is connected to port RA0. The main program is a
4-bit binary up counter that increments at a rate of approximately one second. The
value of the counter is sent to PORT-C and displayed on the four red LEDs. The
yellow LED is toggled on and off when the pushbutton switch is pressed. The switch
is active low and therefore, the interrupt is programmed on the falling edge of the
signal (INTEDGx = 0). The PIC runs at 4.0 MHz clock derived from the internal source.
Write C program.
REFERENCES 25

1. Muhammad Ali Mazidi, Rolin D Mkinlay, Danny Causey, PIC


Microcontroller and Embedded System, Pearson
International Edition.
2. https://en.wikipedia.org/wiki/Interrupt
INTCON
Register
INTCON2
Register
INTCON3
Register

You might also like