Go Down

Topic: Can you interrupt an interrupt? (Read 357 times) previous topic - next topic

scswift

If I enable interrupts in the middle of an interrupt, what will happen?

I have a DAC interrupt to update my sound, and I also have a servo interrupt.  The servos will most likely jitter if the DAC interrupt triggers just before they update... I had this issue with another project a long time ago.  At that time I was just using a piezo speaker for my sound, so I just checked to see if the servo interrupt was about to trigger and skipped that sample, but with my new board the sound is output through an amp and so skipping samples would be heard more easily. 

There's a few different strategies I'm considering here. 

The first is to just let the servos twitch occasionally.  That's not particularly appealing though. 

The second is to either skip a sample when the servo is about to trigger, or delay the sample and play it on the next DAC update.  The first method keeps the sound at the same speed but may cause pops if there is too large a discontinuity between samples.  The second may slow down the sound sightly and cause warbling, though since the servos don't update too fast this would probably not be noticeable as it was when I was skipping samples to update my LEDs at 60fps.  (I've since patched that issue by adding another data bus.)

And the third is to delay the sample, but only long enough for the servo interrupt to finish.  This would reduce any glitches in the audio to an absolute minimum and I'm fairly certain it would be unnoticeable, since the servo interrupt is time sensitive, but very short.  This is why I want to know if I can enable interrupts within an interrupt.

If I can't do that, or it would be a bad idea to (I guess the millisecond timer could get called while I'm in the middle of the servo interrupt if I enabled all interrupts) then is there some simple way I could cause the DAC interrupt to trigger right after the servo interrupt if it needs to be called?  Or call the servo interrupt at the right time from within the DAC interrupt?  I assume I could mess with the DAC interrupt counter but I'd like to keep things as simple as possible. 

I did notice the DAC interrupt seems to call another interrupt to fill the SD card.  Or perhaps it is just turning it on to be called once.  I'm not sure.  But I also seem to recall seeing interrupts enabled and disabled in an interrupt somewhere.  Which is why I'm asking about that.

marco_c

You can interrupt an interrupt. That's why the advice is to keep them short and fast so that you don't miss any other interrupts.
Arduino libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com

Nick Gammon

What interrupt does the DAC use? And the servo?
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

dc42

Yes, if an interrupt service routine re-enables interrupts, then it can be interrupted. But this is generally a bad idea unless you know exactly what you are doing. It sounds to me that you are trying to do too much work in your DAC interrupt service routine.
Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Go Up