megaAVR® Interrupts Overview

The megaAVR® family provides several different interrupt sources, all of which are maskable and are divided into three categories:

  • Internal Peripheral Interrupts
    • Associated with Timers, USART, SPI, ADC peripherals
  • External Pin Interrupts
    • Associated with the INT0-INT7 external interrupt pins
  • Pin Change Interrupts
    • Associated with PCINT0-PCINT2 external interrupts occurring on a port pin change

Peripherals are assigned individual interrupt enable bits in their respective interrupt mask register which must be written as a logic one together with the Global Interrupt Enable I-bit in the Status Register in order to enable the interrupt.


Reset & Interrupt Vector Locations

The Reset & Interrupt sources each have a separate program vector in the program memory space. The lowest addresses in the program memory space are by default defined as the Reset and Interrupt Vectors as shown:


Vector Relocation

The user can relocate the RESET vector as well as the start location of the Interrupt vectors to the Boot Flash Section of program memory space by programming the BOOTRST fuse bit to "0" and setting the IVSEL bit of the Microcontroller Configuration Register (MCUCR) to "1". The possible RESET and interrupt vector placement are shown here:

BOOTRST IVSEL Reset Addr. Interrupt Vector Start Addr.
1 0 0x0000 0x0002
1 1 0x0000 Boot Reset Addr. + 0x0002
0 0 Boot Reset Addr. 0x0002
0 1 Boot Reset Addr. Boot Reset Addr. + 0x0002

The Boot Reset Address is set by BOOTSZ0/BOOTSZ1 fuse bits as shown here for ATmega328PB:


Fuses are programmed using a special programming procedure within Atmel Studio 7 or other programmer.

To avoid unintentional changes of Interrupt Vector tables, a special write procedure must be followed to change the IVSEL bit:

  • Write the Interrupt Vector Change Enable (IVCE) bit to one.
  • Within four cycles, write the desired value to IVSEL while writing a zero to IVCE.

Here is a code sample showing how to modify the IVSEL bit and relocate the interrupt vectors:

Priority Level

Each vector has a pre-determined priority level: The lower the address the higher is the priority level. RESET has the highest priority, and next is INT0 – the External Interrupt Request 0. The following chart depicts the partial vector listing for the ATmega328PB MCU:


Interrupt Processing

When an interrupt occurs, the Global Interrupt Enable I-bit is cleared and all interrupts are disabled. The I-bit is automatically set when a Return from Interrupt instruction – RETI – is executed.

User software can write logic one to the I-bit to enable nested interrupts. All enabled interrupts can then interrupt the current interrupt routine.

There are basically two types of interrupts:

Persistent Interrupts

This type of interrupt will trigger as long as the interrupt condition is present. These interrupts do not necessarily have Interrupt Flags.

Example: USART Receive Complete Interrupt
The USART contains a Receive Complete Flag (RXC) which is set if there is unread data in the receive buffer. When the Receive Complete Interrupt Enable (RXCIE) in UCSRnB is set, the USART Receive Complete interrupt will be executed as long as the RXC Flag is set (provided that global interrupts are enabled). When interrupt-driven data reception is used, the receive complete routine must read the received data from UDR in order to clear the RXC Flag, otherwise a new interrupt will occur once the interrupt routine terminates.

Non-Persistent Interrupts

This type of interrupt is triggered by an event that sets an Interrupt Flag. For these interrupts, the Program Counter is vectored to the actual Interrupt Vector in order to execute the interrupt handling routine, and hardware clears the corresponding Interrupt Flag. Interrupt Flags can also be cleared by writing a logic one to the flag bit position(s) to be cleared. If an interrupt condition occurs while the corresponding interrupt enable bit is cleared, the Interrupt Flag will be set and remembered until the interrupt is enabled, or the flag is cleared by software. Similarly, if one or more interrupt conditions occur while the Global Interrupt Enable bit is cleared, the corresponding Interrupt Flag(s) will be set and remembered until the Global Interrupt Enable bit is set, and will then be executed by order of priority.

Example: Timer/Counter0 Overflow Interrupt
Bit-0 of the Timer0 Interrupt Flag Register (TIFR0) contains the TOV0 interrupt flag. This flag is set when an overflow occurs in Timer/Counter0. TOV0 is cleared by hardware when executing the corresponding interrupt handling vector. Alternatively, TOV0 is cleared by writing a logic one to the flag. When the SREG I-bit, TOIE0 (Timer/Counter0 Overflow Interrupt Enable), and TOV0 are set, the Timer/Counter0 Overflow interrupt is executed.

 Learn More

megaAVR Interrupt Configuration
Learn more >
Special Considerations
Learn more >
megaAVR Interrupt Example
Learn more >
© 2017 Microchip Technology, Inc.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.