SAM D21 NVIC Example Project

Last modified by Microchip on 2023/11/10 11:09


This page provides a simple SAM D21 Nested Vectored Interrupt Controller (NVIC) GNU Compiler Collection (GCC) code example for the ATSAMD21J18A MCU. The project configures the Timer/Counter 3 module (TC3) to produce interrupts every 100 ms, using the default CPU clock frequency (1 MHz). The TC3 handler routine toggles the onboard LED on every interrupt event.

This code example uses a direct/indirect MCU register C-coding style (i.e., no software framework) and is built using the GCC compiler toolchain within the Microchip Studio.

Visit the following page to learn more about how to configure the NVIC and Timer/Counter modules for this specific application:

Reference Materials

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

You should see the folder C:\MTT\32arm\samd21\code-examples-gcc\nvic\interrupt-example-tc containing the solution interrupt-example-tc.atsln.

Back to top

Connection Diagram

SAM D21 Xplained Pro contains an Embedded Debugger (EDBG) that can be used to program and debug the ATSAMD21J18A using Serial Wire Debug (SWD) interface. The EDBG also includes a Virtual COM port interface over UART, a data gateway interface (DGI) over a serial peripheral interface (SPI) and a two-wire interface (TWI), and it monitors four of the SAM D21 general-purpose input/output (GPIOs). Microchip Studio is used as a front-end for the EDBG.

The LED0 is driven by this application and is connected to port PB30 as shown in the accompanying image.

LED0 is driven by this application and is connected to port PB30

Back to top


Attach the SAM D21 Xplained Pro board to your computer using a USB A-to-MicroB cable. Start Microchip Studio. If the board has been successfully enumerated, you should see the board image come up in Microchip Studio as shown in the accompanying image.

Image comes up in Atmel Studio

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

Open the Solution

 Select File > Open > Project/Solution…
Open the solution

Navigate to the solution folder and select the interrupt-example-tc.atsln solution file:

Open the interrupt example solution file

Configure the Debugger

Next, you need to configure the debugger in Microchip Studio to discover and connect to the target EDBG IC on your Xplained Pro board.

First, navigate to Project > Properties:

Open the project properties

Next, under the project's Tool setting, select your EDBG target from the pull-down. Select SWD as the interface:

Selecting your EDBG target from the pull-down

Save the tool setting by clicking the Save All button:

Click the Save All button

Rebuild/Program the Target

Finally, click on the Start Without Debugging icon in Microchip Studio which information re-builds the HEX file from the project source code, (ii) downloads/programs the HEX file onto the target MCU, (iii) and releases the target MCU Reset pin, allowing the program to execute.
Start Without Debugging icon

If prompted, upgrade the EDBG firmware on the board:
EDBG firmware on the board

You need to click on Start Without Debugging again after an EDBG firmware upgrade in order to rebuild/program the target.

Back to top


LED0 (PB30) toggles every 100 ms as the resultsLED0 (PB30) toggles every 100 ms.


This project has provided a simple example of how to set up and use Interrupts on the SAM D21 MCU.

Back to top