megaAVR® Interrupt Code Example


This page provides a basic interrupt code example for the ATmega328PB MCU. The project configures the Timer/Counter1 module to operate in Clear-Timer-On-Compare (CTC) mode, and, on a period match, generates an interrupt event every 100 mS. The ISR manipulates a "tick" signal variable that is used by the main loop to toggle LED0 every 100 mS.


Hardware Tools

Tool About Purchase
ATmega328PB Xplained Mini
Evaluation Kit

Software Tools

Tool About Installers
Windows Linux Mac OSX
Atmel® Studio
Integrated Development Environment

Exercise Files

File Download
Windows Linux Mac OSX
Example Project

We recommend extracting the .zip file to your C:\ folder.

You should see the folder C:\MTT\8avr\mega\code-examples\interrupt-example\8avr-mega-int-usage containing the solution 8avr-mega-int-usage.atsln

 Connection Diagram

The USART0 module on the target ATmega328PB device is connected to the USART interface on the mEDBG chip. The mEDBG chip performs USB-serial conversion by enumerating as a CDC-class virtual COM port on the PC and presenting the target USART data on this interface. The mEDBG also controls the programming/debug interface on the target device, as well as supplying a 16MHz clock when the Xplained board is connected via USB cable to a PC. The LED0 is connected to port PB5 as shown:



Attach the ATmega328PB Xplained Mini board to your computer using a USB A-to-MicroB cable. Start Atmel Studio 7. If the board has been successfully enumerated, you should see the board image come up in Studio as shown:


The board is identified by the last four digits in its serial number (see sticker on bottom of board). In the above example, the last four digits are "3352"


Open the Solution


To understand how interrupts were configured and enabled in this example (main.c file), please review the megaAVR® Interrupt Configuration page.


Rebuild the Solution



Program the Fuses

There are several key hardware configuration settings that need to be configured. The following fuse settings need to be programmed into the device:

  • HIGH: 0xDF
  • LOW: 0xC0
  • EXT: 0xFC

Enter the Device Programming dialog as shown:


In the Device Programming dialog box, select the Tool, Device and Interface as shown, then press Apply:


To verify a connection, select Read and verify that a Device Signature is found:


Select the Fuses sub-section, Enter the 3 Fuse Byte Values above, then press Program as shown:



Program the Hex File

While still in the Device Programming dialog box, select "Memories" as shown. The path to the solution's hex file should already be listed in the dialog. Press Program as shown:





This project has provided an example of how to setup and use interrupts on the megaAVR MCU.

 Learn More

megaAVR Interrupt Overview
Learn more >
megaAVR Interrupt Configuration
Learn more >
Special Considerations
Learn more >
© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
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.