Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Interrupts and the Atmega328
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Interrupts and the Atmega328

by Autronius on Wed Jul 21, 2010 12:39 am

I've been doing some studying of interrupts lately and have learned that there are 2 pins on the arduino that have hardware interrupts built in - Digital Pins 3 and 4, I believe (its a bit late to dig through my notes at the moment).

My question is are these specific to those arduino pins, or are they built into the 328 chip?

This is significant if I am to remove the chip from the arduino and hook it up in a more permanent circuit with the current code. I have heard there are ways to program additional interrupts to pins but that they arn't as efficient as the hardware ones.
Autronius
 
Posts: 15
Joined: Fri May 14, 2010 6:59 pm

Re: Interrupts and the Atmega328

by mtbf0 on Wed Jul 21, 2010 7:39 am

yes, there are two pins on the arduino that can generate their own interrupts and, yes, it is a feature of the atmegax8. these are digital pins 2 and 3, which are pins 2 and 3 or PORTD on the atmega. these interrupts are exposed in the arduino ide via the attachInterrupt and detachInterrupt calls.

truth is, though, that every pin on the arduino is capable of generating what is known as a pin change interrupt. they are a little more difficult to use, though, and are not supported in the arduino ide. there is one pin change interrupt for pins 8-13, (PORTB pins), another for pins 0-7 (PORTD pins), and another for pins 14-19, (PORTC pins), then it's up to you to figure out which pin actually generated the interrupt. this is not difficult and is done by examining a mask register.

must go to work, now, so i refer you to the atmega328 datasheet. it's my favorite book.
"i want to lead a dissipate existence, play scratchy records and enjoy my decline" - iggy pop, i need more
User avatar
mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am
Location: oakland ca

Re: Interrupts and the Atmega328

by Autronius on Wed Jul 21, 2010 1:15 pm

I'm glad that you mentioned the datasheet again. I have it here on my desktop and decided to open it up again. In a moment of divine providence, my eyes caught a glimpse of "11. Interrupts", which I decided to click.

Things are starting to make more sense, thanks!

I'm still not sure of the difference between external interrupts and pin change interrupts, but I've only briefly glanced at the datasheet right now (I'll read more in depth while I'm at work haha).
Autronius
 
Posts: 15
Joined: Fri May 14, 2010 6:59 pm

Re: Interrupts and the Atmega328

by uhe on Thu Jul 22, 2010 4:33 pm

Autronius wrote:I'm still not sure of the difference between external interrupts and pin change interrupts, but I've only briefly glanced at the datasheet right now (I'll read more in depth while I'm at work haha).

Pin change interrupts just say that *something happened*, interrupts on INT0 & INT1 can be configured to trigger on one of 4 conditions: low level, high->low edge, low->high edge and any change like the PCINT interrupts.
"I have not failed. I've just found 10,000 ways that won't work." - Thomas Edison
uhe
 
Posts: 178
Joined: Mon Sep 03, 2007 4:50 pm
Location: metric world

Re: Interrupts and the Atmega328

by samroesch on Wed Sep 22, 2010 8:51 pm

What happens if two interrupts trigger at the same time, or what if Interrupt 1 triggers a subroutine, and during this time (before the subroutine ends) the other triggers?

My application is two gear tooth sensors, before and after a transmission.

Possibilities I hypothesized:
-New interrupt takes over from old one, a count is missed.
-No interrupts can happen until the first interrupt routine is complete.
-Atmega328 explodes.
samroesch
 
Posts: 17
Joined: Mon Feb 08, 2010 2:48 pm

Re: Interrupts and the Atmega328

by zener on Wed Sep 22, 2010 9:57 pm

I think the key here is to understand the difference between an interrupt occuring/executing and an interrupt flag being set. I believe it is true that an interrupt cannot occur during servicing of a prior interrupt, but the interrupt flag can be set nonetheless. So upon returning from the first interrupt, the second executes immediately so it is not missed. However, I have only scaled part way up the wall of ignorance, so if this is not exactly correct, hopefully a smarter person will say so.

zener
 
Posts: 4567
Joined: Sat Feb 21, 2009 2:38 am

Re: Interrupts and the Atmega328

by samroesch on Wed Sep 22, 2010 10:50 pm

That makes good sense to me. Electrical engineers are so clever!
The next possible situation I can see that would mess things up is the second flag is set, and is still waiting for the first interrupt to finish, when the second interrupt is triggered again. The second triggering would seemingly be missed. I imagine this is not likely as long as the interrupt routine is nice and short.
samroesch
 
Posts: 17
Joined: Mon Feb 08, 2010 2:48 pm

Re: Interrupts and the Atmega328

by adafruit_support_bill on Thu Sep 23, 2010 5:53 am

That pretty much sums it up. Keep the ISR's to the bare essentials to minimize the chances of missing an interrupt. If there is more work to do, queue it up and process it after you exit the ISR.

adafruit_support_bill
 
Posts: 78733
Joined: Sat Feb 07, 2009 10:11 am

Re: Interrupts and the Atmega328

by uhe on Thu Sep 23, 2010 1:11 pm

samroesch wrote:Possibilities I hypothesized:
-New interrupt takes over from old one, a count is missed.
-No interrupts can happen until the first interrupt routine is complete.
-Atmega328 explodes.

I vote for Atmega328 explodes :D Since it won't do, with avr-libc you'll get No interrupts can happen until the first interrupt routine is complete.:
The AVR hardware clears the global interrupt flag in SREG before entering an interrupt vector. Thus, normally interrupts will remain disabled inside the handler until the handler exits, where the RETI instruction (that is emitted by the compiler as part of the normal function epilogue for an interrupt handler) will eventually re-enable further interrupts.[*]

So if you want nested interrupts you have to enable them and deal with the problems that come along.
Btw. what is a gear tooth sensor?
"I have not failed. I've just found 10,000 ways that won't work." - Thomas Edison
uhe
 
Posts: 178
Joined: Mon Sep 03, 2007 4:50 pm
Location: metric world

Re: Interrupts and the Atmega328

by zener on Thu Sep 23, 2010 2:14 pm

samroesch wrote:The next possible situation I can see that would mess things up is the second flag is set, and is still waiting for the first interrupt to finish, when the second interrupt is triggered again. The second triggering would seemingly be missed. I imagine this is not likely as long as the interrupt routine is nice and short.

Well I suppose in your ISR you could pole the interrupt flags to check for such an occurrence. If you found one you could reset it and pole it again. But you would have to see if their is any timing advantage to this, or consider if one lost pulse is so bad. Especially if you are doing some digital filtering/running average. You could probably live with it.

zener
 
Posts: 4567
Joined: Sat Feb 21, 2009 2:38 am

Re: Interrupts and the Atmega328

by samroesch on Thu Sep 23, 2010 3:01 pm

Btw. what is a gear tooth sensor?


I think it is essentially a hall effect sensor, it sends a pulse every time a tooth of a gear or sprocket passes by so you can calculate the speed.

It's being used on a Mini Baja Racer, a design team at my university. I'm hoping to log engine rpm, rpm after the transmission/clutch, and transmission temperature so we can know how heat affects the efficiency of our CVT (continually variably transmission). http://bit.ly/doShOf
samroesch
 
Posts: 17
Joined: Mon Feb 08, 2010 2:48 pm

Re: Interrupts and the Atmega328

by zener on Thu Sep 23, 2010 3:43 pm

Some are passive (coil with magnet) so make sure what kind you have.

zener
 
Posts: 4567
Joined: Sat Feb 21, 2009 2:38 am

Re: Interrupts and the Atmega328

by uhe on Thu Sep 23, 2010 5:35 pm

I see. If it is a that 'slow' real world application I'd say you don't need to worry, 16MHz is really fast!
Just make sure that your ISR routine is short (as all ISRs should be).
"I have not failed. I've just found 10,000 ways that won't work." - Thomas Edison
uhe
 
Posts: 178
Joined: Mon Sep 03, 2007 4:50 pm
Location: metric world

Please be positive and constructive with your questions and comments.