Click here to Skip to main content
15,881,588 members
Articles / Internet of Things / Arduino
Tip/Trick

Introduction to Simulate External Interrupts Using Atmel Studio for AVR

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
21 Jun 2016CPOL2 min read 20.1K   4  
Tip on how to simulate external interrupts using Atmel Studio 7 for AVR Atmega MCUs

Introduction

This tip is for beginners who want to explore AVR family of Micro controllers (MCUs) without buying actual hardware, or who want to simulate stuff before programming the code into flash. Let’s get started then…

What You Need

Download Atmel Studio 7. This IDE is free for download and just requires you to register. This is a Windows only software. Other OS fans, please excuse…

Follow the steps on how to install, it asks for few reboots that’s for installing few drivers…

I am following this example which explains how to work with external interrupts in Atmega8, but in that how to simulate without actual hardware is not given. In my tip, I will explain how to simulate external interrupts using Stimuli file. For a good understanding of this tip, please read the article given in this link. I changed a few lines to fit my requirement…

Brief Working

Simulators enable us to play around before actually using the hardware. In this tip, I am simulating external interrupt on Atmega8A MCU. I have enabled PIN2 (INT0) to receive interrupt, enabled global interrupt using sei(). ISR routine for INT0 is implemented where PIN6 is toggled. While debugging this code by executing the stimuli file, it writes to PIN2 so that interrupt is generated and ISR is called. Please see this link for a complete explanation.

Using the Code

Basically Stimuli file is something which when run from debug environment gives various inputs to simulated Micro controller. Stimuli file can have comments, delays, assignments and directives. Delay is how many CPU cycles it has to wait before executing the next statement, it starts with #. Assignments can be various inputs to PINs or PORTs. Directives are used to control execution and for logging, it starts with $. Paste this code into a text file with extension .stim file.

C++
// Set PIN2 (INT0) high and low repeatedly
$repeat 2000000
    PIND |= 0x04
    #20
    PIND = 0x0
    #20
$endrep

Actual AVR C code...

C++
#include <avr/io.h>
#include <avr/interrupt.h>

ISR (INT0_vect)
{
    /* interrupt code here */
    PORTD ^= (1 << PIND6);     // toggle PIND6
}

int main(void)
{
    DDRD &= ~(1 << DDD2);      // Clear the PD2 pin
    // PD2 (INT0 pin) is now an input

    DDRD |= (1 << PIND6);      // make PIND6 an output
    
    MCUCR |= (1 << ISC00);     // set INT0 to trigger on ANY logic change
    GICR |= (1 << INT0);       // Turns on INT0

    sei();                     // turn on interrupts

    while(1)
    {
        /*main program loop here */
                
    }
}

Under Project properties->Tools, you need to select the debugger as simulator and set the stimuli file...

Image 1

Place debug break point at infinite while loop and choose execute stimuli from Debug->Execute Stimulifile. The Stimuli file starts toggling PIN2.

Image 2

To observe what execution of Stimuli file is doing, open IO window, go to Debug->Windows->IO, Select I/O Port(PORTD). You will see PIN2 getting toggled repeatedly. This causes the interrupt to occur and call ISR function. If you keep debug breakpoint in ISR function, it will get repeatedly hit.

Image 3

Points of Interest

Do select proper device when creating the project, the project files which I uploaded (Interrupt.zip) has Atmega8A as I am working with those MCUs. Do play around with other windows in Debug to explore more...

History

Please feel free to point out any mistakes, I will be glad to correct the same...

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) EMC
India India
I have worked in ESM, Storage and Embedded domains. Mainly taken part in design, coding and maintaining in c/c++ on windows.
As a hobby tinkering with embedded/electronic devices (new found interestSmile | :) )...

Comments and Discussions

 
-- There are no messages in this forum --