Go Down

Topic: Interupts (Read 11211 times) previous topic - next topic


It will be nice to use the interrupt to start a subroutine.

Like :  ON_INT myintfunction()

How can we use interrupts ?

              Best regards



I would very much like to do too and I'm now sure I want to fool around the wiring libraries to extract these features.
While were at it, the math functions could be nice too!


There are 2 external interrupts available. You can set up a service routine for
them with

  .... your code here...

similar for INT1.

This is from the avr gcc documentation.  Read the datasheet to see which pins correspond to the external interrupts.


Arduino 0007 will include functions for attaching and detaching your functions from interrupts.  It should be out as soon as I find a PC to build and test on.


Schweet! I can do some tests if you want :) It'll be BootCamp, but that should do, no?

Thanks Mellis!


Dec 24, 2006, 12:08 am Last Edit: Dec 24, 2006, 12:19 am by macsimski Reason: 1
Actually, as a side effect of my plan to put the arduino in sleep, i have read a bit into interrupts to wake the thing up again. I haven't test it yet, but basically, you have to use the interrupt.h header file from Arduino-0006/tools/avr/avr/include/avr/ and copy it in a same named folder in the Arduino-0006/lib/targets/libraries/ folder.

Include it in your sketch like this:

Code: [Select]
#include interrupt.h

then you can turn interrups on with

Code: [Select]

and turn it off again with

Code: [Select]

don't leave it on forever, otherwise yor program will be interrupted on moments you dont want to.

then your interrupt routines will have a form of:
Code: [Select]


// code here


where _interrupt_vector_name_ is filled in with the apropiate interrupt you want.

these names are defined in the iom8.h file in two different forms. pick the one you like:

Code: [Select]

/* Interrupt vectors */

/* External Interrupt Request 0 */
#define INT0_vect                  _VECTOR(1)
#define SIG_INTERRUPT0                  _VECTOR(1)

/* External Interrupt Request 1 */
#define INT1_vect                  _VECTOR(2)
#define SIG_INTERRUPT1                  _VECTOR(2)

/* Timer/Counter2 Compare Match */
#define TIMER2_COMP_vect            _VECTOR(3)
#define SIG_OUTPUT_COMPARE2            _VECTOR(3)

/* Timer/Counter2 Overflow */
#define TIMER2_OVF_vect                  _VECTOR(4)
#define SIG_OVERFLOW2                  _VECTOR(4)

/* Timer/Counter1 Capture Event */
#define TIMER1_CAPT_vect            _VECTOR(5)
#define SIG_INPUT_CAPTURE1            _VECTOR(5)

/* Timer/Counter1 Compare Match A */
#define TIMER1_COMPA_vect            _VECTOR(6)
#define SIG_OUTPUT_COMPARE1A            _VECTOR(6)

/* Timer/Counter1 Compare Match B */
#define TIMER1_COMPB_vect            _VECTOR(7)
#define SIG_OUTPUT_COMPARE1B            _VECTOR(7)

/* Timer/Counter1 Overflow */
#define TIMER1_OVF_vect                  _VECTOR(8)
#define SIG_OVERFLOW1                  _VECTOR(8)

/* Timer/Counter0 Overflow */
#define TIMER0_OVF_vect                  _VECTOR(9)
#define SIG_OVERFLOW0                  _VECTOR(9)

/* Serial Transfer Complete */
#define SPI_STC_vect                  _VECTOR(10)
#define SIG_SPI                        _VECTOR(10)

/* USART, Rx Complete */
#define USART_RXC_vect                  _VECTOR(11)
#define SIG_UART_RECV                  _VECTOR(11)

/* USART Data Register Empty */
#define USART_UDRE_vect                  _VECTOR(12)
#define SIG_UART_DATA                  _VECTOR(12)

/* USART, Tx Complete */
#define USART_TXC_vect                  _VECTOR(13)
#define SIG_UART_TRANS                  _VECTOR(13)

/* ADC Conversion Complete */
#define ADC_vect                  _VECTOR(14)
#define SIG_ADC                        _VECTOR(14)

/* EEPROM Ready */
#define EE_RDY_vect                  _VECTOR(15)
#define SIG_EEPROM_READY            _VECTOR(15)

/* Analog Comparator */
#define ANA_COMP_vect                  _VECTOR(16)
#define SIG_COMPARATOR                  _VECTOR(16)

/* 2-wire Serial Interface */
#define TWI_vect                  _VECTOR(17)
#define SIG_2WIRE_SERIAL            _VECTOR(17)

/* Store Program Memory Ready */
#define SPM_RDY_vect                  _VECTOR(18)
#define SIG_SPM_READY                  _VECTOR(18)

INT0 and INT1 are external interrupts on the arduino pins 2 and 3 respectivily. they can be configured to react in different way's: trigger on rising or falling slope. LOW and change (like gonging from low to high or the other way round. these settings are controlled with a few bits in the MCUCR register:

two bits to set the interupt mode for each external interupt pin:

ISC01  ISC00 (int0)
ISC11  ISC10 (int1)    interupt if:
0          0                low
0          1                change
1          0                falling slope
1          1                rising slope

Code: [Select]

void setup() {

MCUCR |= (1<<ISC01);
MCUCR |= (0<<ISC00);  // should set a falling slope for int0 = arduino pin 2


this is done in the setup portion of your code.

comments appreciated.

Oh and btw, my sleep project is almost finished. you can follow it here:

"We're all in this together..."


Dec 24, 2006, 12:42 am Last Edit: Dec 24, 2006, 12:56 am by Daniel Reason: 1
Interrupts are in the Arduino 007 beta! Check the December developer's list archives for the link.


for what i've read, that is only for external interupts. but it is good that it will be part of the standard enviroment.
"We're all in this together..."


I just made a similar reply in another thread:  when you do something like this line of code:

   MCUCR |= (0<<ISC00);  // should set a falling slope for int0 = arduino pin 2

You are not really doing anything at all.  Doing a bitwise OR with zero does not change the value of MCUCR.  I suspect what you meant was to ensure that the given bit was turned off.  In that case, what you really need is:

   MCUCR &= ~(1<<ISC00);  // should set a falling slope for int0 = arduino pin 2

Just thought I would point this out, because I keep seeing this in code posted here.  What you had might be working correctly simply because the given bit was already zero.


I started to link my interrupt examples in the playground:


Feel free to add your examples...


@ CosineKitty....

But how can we generate internal interrupts like when a variable reaches a particular value. I actually want to generate interrupt when the distance of my robot from wall reaches a threshold value. obviously this threshold value is calculated in software (Arduino environment).

Are there any internal interrupts in Atmega328?



I actually want to generate interrupt when the distance of my robot from wall reaches a threshold value. obviously this threshold value is calculated in software (Arduino environment).

If the sensor value is read and threshold calculated in the sketch there is hardly a reason I can think of why you need that to generate an interrupt. Just setting a global flag variable in your sketch when the threshold is met can be used anywhere else in the sketch as required.

If you can explain why you think that generating an interrupt is required or desirable, please share your thinking.



I thought that since i am continuously checking the reading of the sensor to see whether it has reached the threshold value, its similar to polling for an external input. So i need interrupt. If it is not so then when do we need internal interrupts like why do we have timer interrupts then? Can you give any practical example of internal interrupts in any system.



Jun 08, 2010, 08:08 pm Last Edit: Jun 08, 2010, 08:11 pm by AWOL Reason: 1
Can you give any practical example of internal interrupts in any system

Well, there are address faults, invalid instruction faults, supervisor calls, access violations...

All of these need to operate at much smaller timescales than anything that involve a moving platform (though not, admittedly, on an AVR!).

If you think you absolutely must have an interrupt, then you're going to need some sort of external comparator.

But you don't, really - trust me.

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.


Ok..i got it. thanks!

Go Up