How to work with External (Hardware) Interrupts of PIC Microcontroller (PIC18F4550)

InterruptsΒ are special events that require immediate attention. They cause the processor to cease the running task to serve a special task for which the interrupt event had occurred. After the special task is over, the processor resumes performing the original task.

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. To understand the difference between polling and interrupts well, refer introductory paragraphs ofΒ 8051 Interrupts.
Β 
This article is based onΒ PIC18F4550Β microcontroller’s interrupt system. The configuration and implementation ofΒ PICΒ Hardware Interrupts are explained here.
Β 

AΒ PIC microcontrollerΒ consists of bothΒ software and hardware generated interrupts. TheΒ hardware interruptsΒ are produced by external hardware at certain pins of theΒ microcontroller. The software interrupts, on the other hand, are generated by internal peripherals of the controller. This software interrupt helps the programmer to use more than one internal peripheral in single application and serve them individually when they respond.

Β 
Following is an example to illustrate the interrupts better. Suppose a programmer wants to make a real time watch which shows ambient temperature as well. The programmer has to use two internal peripherals of the controller, namely, aΒ TimerΒ and an ADC channel. Consider this project without using interrupt : the programmer has to take care of both peripherals one by one continuously by polling them. This is not an efficient way of programming.
Β 
In Interrupt method, the controller serves the Timer when it overflows and the ADC when the A/D (analog to digital) conversion is done. Along with these, the microcontroller can also perform other tasks, likeΒ displaying some text on LCD. Therefore use of interrupt makes the program more efficient and logical.
Β 
When an Interrupt occurs in a PIC Microcontroller, the program execution gets transferred to a predefined Interrupt Vector Address from where the processor gets what operations to perform in for a particular interrupt occurrence. The detailed working of interrupts can also be studied fromΒ 8051 InterruptsΒ orΒ AVR Interrupts.
Β 
Interrupts in PIC18F4550:Β 
PIC18F4550Β has followingΒ internal and externalΒ interrupts:

Β·Β Β Β Β Β Β Β Β Β Reset, Brown-Out Reset, Watch-dog Reset, Power On Reset

Β·Β Β Β Β Β Β Β Β Β External Interrupt 0 (INT0)

Β·Β Β Β Β Β Β Β Β Β External Interrupt 1 (INT1)

Β·Β Β Β Β Β Β Β Β Β External Interrupt 2 (INT2)

Β·Β Β Β Β Β Β Β Β Β Timer 0 Interrupt

Β·Β Β Β Β Β Β Β Β Β Timer 1 Interrupt

Β·Β Β Β Β Β Β Β Β Β Timer 2 Interrupt

Β·Β Β Β Β Β Β Β Β Β Timer 3 Interrupt

Β·Β Β Β Β Β Β Β Β Β ADC Interrupt

Β·Β Β Β Β Β Β Β Β Β Analog Comparator Interrupt

Β·Β Β Β Β Β Β Β Β Β RB Port change Enable Interrupt

Β·Β Β Β Β Β Β Β Β Β Streaming Parallel Port Read/Write Interrupt

Β·Β Β Β Β Β Β Β Β Β EUSART Receive Interrupt

Β·Β Β Β Β Β Β Β Β Β EUSART Transmit Interrupt

Β·Β Β Β Β Β Β Β Β Β Master Synchronous Serial Port Interrupt

Β·Β Β Β Β Β Β Β Β Β CCP1 Interrupt (Capture, Compare, PWM)

Β·Β Β Β Β Β Β Β Β Β Oscillator Fail Interrupt

Β·Β Β Β Β Β Β Β Β Β USB Interrupt

Β·Β Β Β Β Β Β Β Β Β Data EEPROM/Flash Write Operation Interrupt

Β·Β Β Β Β Β Β Β Β Β Bus Collision Interrupt

Β·Β Β Β Β Β Β Β Β Β High/Low-Voltage Detect Interrupt

Β·Β Β Β Β Β Β Β Β Β CCP2 Interrupt

Β 
A total of 10 registers are used to control the interrupt operation in PIC18F4550 which are as follows:
Β·Β Β Β Β Β Β Β Β Β RCON
Β·Β Β Β Β Β Β Β Β Β INTCON, INTCON2, INTCON3
Β·Β Β Β Β Β Β Β Β Β PIR1, PIR2
Β·Β Β Β Β Β Β Β Β Β PIE1, PIE2
Β·Β Β Β Β Β Β Β Β Β IPR1, IPR2
Β 
In this article, the hardware interrupts of PIC18F4550 have been explained. To use the External PIC Interrupts, INTCON registers are required to be configured. The PIE (Peripheral Interrupt Enable) and PIR (Peripheral Interrupt Request) registers are used to configure the Peripheral (Internal) Interrupts. The peripheral interrupts will be covered in subsequent articles.
Β 
Registers for External (Hardware) Interrupts:
The INTCON registers contain various enable, priority and flag bits for different hardware interrupt operations.
Β·Β Β Β Β Β Β Β Β Β Flag bits Β Β Β Β Β Β Β Β  Β Β Β Β Β Β Β Β Β Β  : Β Β Β Β Β Β Β Β Β  indicate that an interrupt event has occurred.
Β·Β Β Β Β Β Β Β Β Β Enable bits Β Β Β Β  Β Β Β Β Β Β Β Β Β  : Β Β Β Β Β Β Β Β Β  allow the program execution to be mapped toΒ Interrupt Vector AddressΒ when Flag bits are set.
Β·Β Β Β Β Β Β Β Β Β Priority bits Β Β Β  Β Β Β Β Β Β Β Β Β Β Β  : Β Β Β Β Β Β Β Β Β  select high or low priority.
Β 
The individual INTCON registers and their configurations have been explained below:
Β 
1.Β INTCON (Interrupt Control Register)Β 
Bit 7
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
GIE/GIEH
PEIE/GIEL
TMR0IE
INT0IE
RBIE
TMR0IF
INT0IF
RBIF

Fig. 2: Bit Configuration of INTCON /Interrupt Control Register 1 for various hardware interrupt operation in PIC Microcontroller

Β 
INT0IF:Β This is External Interrupt 0 (INT0) flag bit.
1 = The INT0 External interrupt occurred (must be cleared in software)
0 = The INT0 External Interrupt did not occur
Β 
INT0IE:Β This bit enables/disables the External Interrupt 0.
1 = Enables the External Interrupt 0
0 = Disables the External Interrupt 0
Β 
PEIE/GIEL:Β This bit is used to enable/disable all the peripheral interrupts (Internal interrupts) of the controller. But GIE/GIEH bit must be set to high first.
1 = Enables all Peripheral Interrupts
0 = Disables all Peripheral Interrupts
Β 
GIE/GIEH:Β This is Global Interrupt Enable bit. This bit is set to high to enable all interrupts of PIC18F4550.
1 = Enables interrupts
0 = Disables all interrupts
Β 
2.Β INTCON2 (Interrupt Control Register 2):Β 
Bit 7
Β 
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
RBPU
INTEDG0
INTEDG1
INTEDG2
β€”
TMR0IP
β€”
RBIP
Β 
Fig. 3: Bit Configuration of INTCON /Interrupt Control Register 2 for various hardware interrupt operation in PIC MicrocontrollerΒ 
Β 
INTEDG0, INTEDG1, INTEDG2:Β These bits are used select the triggering edge of the corresponding interrupt signal on which the controller should respond.
1 = Interrupt on rising edge
0 = Interrupt on falling edge
Β 
3.Β INTCON3Β (Interrupt Control Register 3):
Bit 7
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
INT2IP
INT1IP
β€”
INT2IE
INT1IE
β€”
INT2IF
INT1IF
Β 
Fig. 4: Bit Configuration of INTCON /Interrupt Control Register 3 for various hardware interrupt operation in PIC Microcontroller
Β 
Β INT1IF, INT2IF:Β These are External Interrupt 1 and 2 flag bits, respectively.
1 = The INTx External Interrupt occurred (must be cleared in software)
0 = The INTx External Interrupt did not occur
Β 
INT1IE, INT2IE:Β These bits enable/disable the External Interrupt 1 and 2, respectively.
1 = Enables the External Interrupt x
0 = Disables the External Interrupt x
Β 
INT1IP, INT2IP:Β These bits are used to set priority of the interrupts 1 and 2, respectively.
1 = High priority
0 = Low priority
Β 
Β 
Working with an External PIC Interrupt:
Objective:Β To configure the External Interrupt 0 (INT0) and invert (or toggle) the output at PORTD when interrupt occurs.
The output at PORTD is monitored through a set of 8 LEDs. Their connections with PIC18F4550 are shown in the circuit diagram tab.
Β 
Programming Steps:
1. Enable the External Interrupt 0 by setting INT0IE bit high (INTCON).
2. Set the interrupt on falling edge by setting the INTEDG0 bit to zero (INTCON2).
3. Enable Global Interrupt by setting GIE bit to high (INTCON).
4. Start a while loop and initialize PORTD with certain value.
5. Write the ISR (Interrupt Service Routine) for this interrupt by using interrupt() as ISR header.
6. Clear the INT0IF bit (INTCON).
7. Write instruction to invert or toggle the value at PORTD.
Β 
In this algorithm, the program control will jump to ISR when a falling edge signal appears at INT0 (Pin 33) ofΒ PIC18F4550.

Project Source Code

###


//Program to depict the working of External Interrupt0 of PIC18F4550

void main()
{
TRISD=0; // Configure PortD as output port
INTCON=0x10; // Enable INT0
INTCON2=0; // Set Falling Edge Trigger for INT0

INTCON.GIE=1; // Enable The Global Interrupt
while(1)
{
LATD=0x55; //Set some value at PortD
}
}

void interrupt() // Interrupt ISR
{
INTCON.INT0IF=0; // Clear the interrupt 0 flag
LATD=~LATD; // Invert (Toggle) the value at PortD
Delay_ms(1000); // Delay for 1 sec
}

###

Circuit Diagrams

Circuit Diagrams .

Project Components

Project Video

Source: How to work with External (Hardware) Interrupts of PIC Microcontroller (PIC18F4550)


About The Author

Muhammad Bilal

I am a highly skilled and motivated individual with a Master's degree in Computer Science. I have extensive experience in technical writing and a deep understanding of SEO practices.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.