How to use External (Hardware) Interrupts of AVR Microcontroller (ATmega16)

Summary of How to use External (Hardware) Interrupts of AVR Microcontroller (ATmega16)


This article explains the interrupt concept in AVR ATmega16 microcontrollers, detailing how they suspend normal execution to handle high-priority events via Interrupt Service Routines (ISRs). It outlines the five-step process of interrupt handling and notes that the ATmega16 supports 21 total interrupts, including four external interrupts triggered by specific pins.

Parts used in the ATmega16 Microcontroller Project:

  • ATmega16 Microcontroller
  • External Interrupt Pins
  • Interrupt Service Routine (ISR) Code
  • Program Counter
  • RETI Instruction

This article introduces the concept of interrupts and the different types of interrupts in AVR Microcontroller (ATmega16). Interrupt as the name suggests, interrupts the current routine of the microcontroller. Microcontroller executes instructions in a sequence as per the programs. Sometimes there may be a need of handling planned and higher priority events instantaneously that might occur during the normal operations. To handle such kind of events AVR microcontrollers are equipped with Interrupt Systems.
How to use External (Hardware) Interrupts of AVR Microcontroller (ATmega16)

When an interrupt occurs, the normal flow of instructions is suspended by the microcontroller and the code corresponding to the interrupt, which has occurred, is executed. Once the code corresponding to the interrupt is executed completely the execution again begins from the same instruction where it was stopped.
Following is what happens when an interrupt occurs:
1.      Microcontroller normally completes the instruction which is being executed.
2.      The program control transfers to Interrupt Service Routine (ISR). Each interrupt have an associated ISR which is a piece of code which tells the microcontroller what to do when an interrupt has occurred.
3.      Execution of ISR is performed by loading the beginning address of the corresponding ISR into program counter.
4.      Execution of ISR continues until the return from the interrupt instruction (RETI) is encountered.
5.      When ISR is complete, the microcontroller resumes processing where it left off before the interrupt occurred, i.e., program control is reverted back to the main program.
The whole process can be visualized by the following flow diagram:
How to use External (Hardware) Interrupts of AVR Microcontroller (ATmega16) schematic
Atmega16 Interrupts
Number of available interrupts varies with different microcontrollers of AVR family.  Atmega16 in total has twenty one (21) interrupts available. The available interrupts are categorized in two classes:
1.      External Interrupts- Out of the twenty one interrupts available, four interrupts are directly present on controller pins to handle the interrupts generated by external sources, so they are called as external interrupts. The four available interrupts and their respective pins are shown in the figure below in their order of priority:

Quick Solutions to Questions related to ATmega16 Microcontroller:

  • What is the primary function of an interrupt in a microcontroller?
    An interrupt suspends the current routine to handle planned or higher priority events instantaneously.
  • How many interrupts are available in the ATmega16 microcontroller?
    The ATmega16 has twenty-one interrupts available in total.
  • Can you list the two classes of interrupts mentioned for the ATmega16?
    The interrupts are categorized into External Interrupts and other internal types not detailed in this text.
  • Which instruction ends the execution of an Interrupt Service Routine?
    The return from the interrupt instruction, known as RETI, ends the ISR execution.
  • Does the microcontroller stop immediately when an interrupt occurs?
    No, the microcontroller normally completes the instruction currently being executed before transferring control.
  • Where does program control go after an interrupt is detected?
    Control transfers to the specific Interrupt Service Routine associated with that interrupt.
  • How many external interrupts are directly present on the controller pins?
    There are four external interrupts directly present on the controller pins.
  • What happens to the main program flow after an ISR is complete?
    The microcontroller resumes processing exactly where it left off before the interrupt occurred.

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
Scroll to Top