ATMega 328 software interrupts

Thread Starter

Manmeet Singh

Joined May 21, 2008
37
Hello everyone Im planning on using the Arduino UNO for a project of mine and as im waiting for the order to get shipped I thought I would start on the code. Now I am familiar with interrupts but not with the ATMega 328 found on the Arduino UNO. How do I set up the interrupt ISRs?

According to http://arduino.cc/en/Reference/AttachInterrupt
This is not for a specific ATMega processor
Example

void setup()
{
pinMode(pin, OUTPUT);
attachInterrupt(0, blink, CHANGE);
}

void blink()
{
...
}

but then elsewhere I have found this kind of code for the ATMega8


// Definition of interrupt names
#include < avr/io.h >
// ISR interrupt service routine
#include < avr/interrupt.h >

// Install the interrupt routine.
ISR(INT0_vect) {
....
}

void setup() {
...
// Global Enable INT0 interrupt
GICR |= ( 1 < < INT0);
// Signal change triggers interrupt
MCUCR |= ( 1 << ISC00);
MCUCR |= ( 0 << ISC01);
....
}

Neither of them clear an interrupt flag in the ISR either which is strange
to me. Any tips on the right way todo this?

Thanks
 

Tahmid

Joined Jul 2, 2008
343
Hi,
It all depends on which language and compiler you use and that will dictate the way of writing codes and usually varies from compiler to compiler. I don't know about the above, but the bottom one is from the more popular WinAVR or AVR-GCC. There are other compilers like mikroC, mikroBASIC, IAR Workbech, etc. which are all different. So now you have to choose which compiler you want and which language.

Hope this helps.
Tahmid.
 

Tahmid

Joined Jul 2, 2008
343
eg. This code is in WinAVR AVR-GCC
Rich (BB code):
// Definition of interrupt names
#include < avr/io.h >
// ISR interrupt service routine
#include < avr/interrupt.h >

// Install the interrupt routine.
ISR(INT0_vect) {
....
}

void setup() {
...
// Global Enable INT0 interrupt
GICR |= ( 1 < < INT0);
// Signal change triggers interrupt
MCUCR |= ( 1 << ISC00);
MCUCR |= ( 0 << ISC01);
....
This is in mikroBasic:
Rich (BB code):
program something

sub procedure interruptInt0() org IVT_ADDR_INT0
....
end sub

main:
   GICR.INT0 = 1
   MCUCR.ISC00 = 1
   MCUCR.ISC01 = 1
.....
.....
.....
end.
They are the same thing in different languages and different compilers.

Hope this helps.
Tahmid.
 

hgmjr

Joined Jan 28, 2005
9,027
Hello everyone Im planning on using the Arduino UNO for a project of mine and as im waiting for the order to get shipped I thought I would start on the code. Now I am familiar with interrupts but not with the ATMega 328 found on the Arduino UNO. How do I set up the interrupt ISRs?

Rich (BB code):
According to http://arduino.cc/en/Reference/AttachInterrupt
This is not for a specific ATMega processor 
Example
 
int pin 13;  
 
void setup()
{
pinMode(pin, OUTPUT);
attachInterrupt(0, blink, CHANGE);
}
 
void blink()
{
...
}
In looking more closely at your Arduino interrupt effort the one thing I see missing is the pin assignment declaration. The statement that I added using bold red font tells the Arduion compiler that you have assigned the label "pin" to input number 13. It turns out that input 13 is the one that is connected to the heartbeat LED on the Arduino Duemilanove and the Arduino Uno.

The attachInterrupt(0, blink, CHANGE) statement first tells the compiler that the interrupting signal is connected to pin 0. Next it tells the compiler to set up the ATMEGA328's internal registers that control interrupts (specifically the pin change interrupt) and points the interrupt to your interrupt service routine code contained in the function blink(). Lastly, the keyword CHANGE tells the compiler that you want the interrupt to occur each time the signal on input 0 changes from 0 to 1 or from one to 0.

hgmjr
 
Top