How to work with External (Hardware) Interrupts of PIC18F4550

Summary of How to work with External (Hardware) Interrupts of PIC18F4550


PIC18F4550 supports hardware and software interrupts allowing immediate servicing of events (vs inefficient polling). Interrupts transfer execution to a fixed vector so peripherals like timers and ADC can be handled asynchronously, enabling concurrent tasks. The MCU provides many sources (external INTx, timers, ADC, EUSART, USB, CCP, comparator, resets, etc.) and uses 10 control registers to manage interrupt behavior. Example use: combining timer and ADC for a real-time temperature clock without polling.

Parts used in the PIC18F4550 Interrupts Project:

  • PIC18F4550 microcontroller
  • External interrupt pins (INT0, INT1, INT2)
  • Timer peripherals (Timer0, Timer1, Timer2, Timer3)
  • ADC module
  • Analog comparator
  • RB port (for change-detect interrupt)
  • Streaming Parallel Port
  • EUSART (receive and transmit)
  • Master Synchronous Serial Port
  • CCP modules (CCP1, CCP2)
  • USB interface
  • Oscillator with fail detection
  • Reset and power management circuitry (Reset, Brown-Out, Watch-dog, Power-On Reset)
  • EEPROM/Flash write control
  • Bus collision detection

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.

Interrupts of PIC

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:

Quick Solutions to Questions related to PIC18F4550 Interrupts Project:

  • What is the advantage of interrupts over polling?
    Interrupts allow immediate servicing of events without continuous CPU waiting, making programs more efficient than polling.
  • How does the PIC18F4550 handle an interrupt?
    On an interrupt, execution transfers to a predefined Interrupt Vector Address where the processor executes the interrupt service routine.
  • Can internal peripherals generate interrupts in PIC18F4550?
    Yes, internal peripherals such as timers, ADC, EUSART, CCP, and others generate software interrupts.
  • Does PIC18F4550 support external hardware interrupts?
    Yes, it supports external interrupts on pins INT0, INT1, and INT2.
  • What kinds of interrupts are available on PIC18F4550?
    It includes resets, external INTx, timers, ADC, comparator, port change, parallel port, EUSART, MSSP, CCP1/CCP2, USB, EEPROM/Flash write, bus collision, voltage detect, and oscillator fail interrupts.
  • How many registers control interrupts in PIC18F4550?
    A total of 10 registers are used to control interrupt operation in PIC18F4550.
  • Can interrupts help when using multiple peripherals like Timer and ADC?
    Yes, interrupts let the controller serve each peripheral when they signal completion, allowing the CPU to perform other tasks concurrently.
  • Is polling recommended for handling events on PIC18F4550?
    No, polling is inefficient compared to interrupts because the processor must continuously wait for event signals.

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter