Pages: [1]   Go Down
Author Topic: Interupts  (Read 4486 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Like :  ON_INT myintfunction()


How can we use interrupts ?


               Best regards

                       Georges
Logged

Montréal, Qc
Offline Offline
Full Member
***
Karma: 1
Posts: 185
Practice safe hex!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

"Pilots believe in a clean living... they never drink wisky from a dirty glass."

Austin, TX
Offline Offline
Full Member
***
Karma: 2
Posts: 182
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

ISR(INT0_vect)
{
   .... 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.
Logged

Chris J. Kiick
Robot builder and all around geek.

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 12
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Montréal, Qc
Offline Offline
Full Member
***
Karma: 1
Posts: 185
Practice safe hex!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Thanks Mellis!
Logged

"Pilots believe in a clean living... they never drink wisky from a dirty glass."

Amsterdam
Offline Offline
Full Member
***
Karma: 1
Posts: 139
he's looking at you, kiddy...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#include interrupt.h

then you can turn interrups on with

Code:
sei();

and turn it off again with

Code:
cli();

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:
ISR(_interrupt_vecor_name_){

// 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:
/* 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:

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:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1166480143
« Last Edit: December 23, 2006, 06:19:11 pm by macsimski » Logged

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

Daniel
Guest
 Bigger Bigger  Smaller Smaller  Reset Reset

Interrupts are in the Arduino 007 beta! Check the December developer's list archives for the link.
« Last Edit: December 23, 2006, 06:56:37 pm by Daniel » Logged

Amsterdam
Offline Offline
Full Member
***
Karma: 1
Posts: 139
he's looking at you, kiddy...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

for what i've read, that is only for external interupts. but it is good that it will be part of the standard enviroment.
Logged

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

Florida, USA
Offline Offline
Full Member
***
Karma: 0
Posts: 146
meow!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I started to link my interrupt examples in the playground:


http://www.arduino.cc/playground/Code/Interrupts


Feel free to add your examples...
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 33
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@ 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?

Thanks!
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17301
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

Lefty
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 33
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Thanks!
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26495
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.


« Last Edit: June 08, 2010, 01:11:31 pm by AWOL » Logged

"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.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 33
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok..i got it. thanks!
Logged

Pages: [1]   Go Up
Jump to: