Is there a way to enable interrupts while in ISR?

I'm using Arduino mega 2560. Lets say i'm using TIMER1 and TIMER2. I'm using ISR(TIMER1_COMPA_vect) and ISR(TIMER2_COMPA_vect). TIMER1 will trigger an interrupt every 1 sec while TIMER2 will trigger an interrupt every 100 ms (10 times in a sec). I want to meke it so if i'm in ISR(TIMER1_COMPA_vect) and TIMER2 cause an interrupt, it will jump from ISR(TIMER1_COMPA_vect) to ISR(TIMER2_COMPA_vect). In other word, i want go give TIMER2 the higher priority. is it possible?

Enabling interrupts with in an ISR will crash your program.

Mark

so whats the priorities of the timer interrupts? if TIMER1 and TIMER2 cause an interrupt at the same time. which timer the program will respond first?

holmes4: Enabling interrupts with in an ISR will crash your program.

Mark

That is not necessarily true. Done without care it certainly is. But, if you know what you are doing, it can be done.

That OP needs to ask indicates that enabling interrupts in an interrupt handler is outside his/her current skill set.

if TIMER1 and TIMER2 cause an interrupt at the same time. which timer the program will respond first?

That is not possible. Nothing happens "at the same time" on the Arduino.

Here's chapter 7.2 of Atmel's ATmega2560 datasheet on the subject

All interrupts have a separate Interrupt Vector in the Interrupt Vector table. The interrupts have priority in accordance with their Interrupt Vector position. The lower the Interrupt Vector address, the higher the priority.

The Interrupt Vector addresses are tabulated in 14.1. Interrupt priority, and interrupts in general, are discussed in greater detail in 7.8. You can find the datasheet link on the Arduino Mega 2560 product page, here: http://arduino.cc/en/Main/ArduinoBoardMega2560

As PaulS says, it's possible to successfully use nested interrupts on an AVR, but it's indeed tricky. The AVR doesn't let you block interrupts of any particular priority level. Interrupts are either enabled, allowing every interrupt that's individually enabled to take control of the processor, or they're disabled.

The canonical way to manage nested interrupts on an AVR is just like the canonical way to manage "goto" statements: find another way to write the code so you don't have to use them.

You haven't said what makes you want to do this. Intuitively, I suspect that your ISR for the lower-priority timer takes a long time to execute; otherwise, I'd expect that you'd be indifferent to which interrupt was acknowledged first. If that's so, I'd recommend that you consider rewriting the code to obviate nested interrupts. That means rewriting the ISR to do less work, and moving some of the work to loop(). A simple way to do that is to have the ISR do whatever input/output is required, save the results in volatile global variables, and set a flag to indicate that it has executed, and exit; loop() examines the flag, and, if it's set, does the work and clears the flag. The first rule of ISR's is to keep them short.

Another way to manage a possible conflict between timer interrupts is to check the higher-priority timer's flag inside the lower-priority timer's ISR, and, if it's set, do whatever the higher-priority timer's ISR would do, and clear the flag - as long as the whole ISR doesn't take too long.

I could speculate further about what you're trying to do, but it would a lot easier if you'd just tell us. What are you hoping to accomplish with nested interrupts? Please be specific.

PaulS: Nothing happens "at the same time" on the Arduino.

I think that they can happen simultaneously. On the 2560, all the timers except Timer2 share the same prescaler. If Timer0 and Timer1 use the same I/O clock divisor, and Timer0 interrupts, say, every 10 ticks, and Timer1 every 11 ticks, I think they'll coincide every 110 ticks. I think that for timer interrupts, simultaneity is quite common.

phoenix2000:
so whats the priorities of the timer interrupts?
if TIMER1 and TIMER2 cause an interrupt at the same time. which timer the program will respond first?

Interrupt priorities are in the order of their vector addresses, with priority increasing as vector address decreases. So all of the TIMER2 interrupts have a higher priority than the TIMER1 interrupts. If both interrupts are pending at the same time the TIMER2 interrupt will be serviced first.

Once you’re in an ISR interrupts will be disabled and priority doesn’t matter until interrupts are reenabled upon exit. If you have some need to handle another interrupt immediately you could reenable interrupts within the ISR. But you must be careful. One thing to guard against is the possibility that you will get another interrupt from the same source before you have exited its ISR. That’s the sort of scenario that could crash your program. Another thing to consider is if there are other interrupts besides the two you’re working with that might instead take control.

The usual rule is to avoid reenabling interrupts in an ISR because you can get into trouble. But rules are made to be broken.

Thanks for the responses. I'm using a touch screen with my arduino and what i want to do is to use TIMER1 interrupt to refresh the screen somthing like 2-5 times a second while the TIMER2 interrupt will check the for touch somthing like 50-100 times a second. Refreshing the screen can take sometimes more then 100 ms while getting touch information is somthing like 4-6 ms. First i thought about do this in the ISR but now i see its useless so i think i will use the timers only to set the flags for the loop() to know when to refresh the screen and when to get the touch coordinates.

so i think i will use the timers only to set the flags for the loop() to know when to refresh the screen and when to get the touch coordinates.

If loop() is going to look at flags, set by interrupt handlers willy-nilly based only on clock ticks, it might as well just look at the clock. Timers triggering interrupts are not contributing value here.

I don't know, maybe I have bad eyes, but my eyes can't tell any difference if the screen refreshes a few milliseconds later.

This does not seem like a job for interrupts. This sounds like a job for writing non-blocking code. Any time I have fiddled with displays my loop turned over way faster than my display updated. So just check the clock and update the display from loop if you need to. It doesn't have to be exact if we're only talking about a few milliseconds.

http://www.gammon.com.au/interrupts

TIMER1 will trigger an interrupt every 1 sec while TIMER2 will trigger an interrupt every 100 ms (10 times in a sec).

Well, can't you count to 10 in the Timer 2 interrupt? That way you know when a second has elapsed. (That is, every 10 interrupts of 100 mS).