0

I'm working on Arduino uno and trying to use more then one timer. I'm using timer1 for my software watchdog (I needed bigger interval for that, so my program won't reset itself after 8 seconds). In addition, I'm using timer2 for regular interrupt like turn off pin after 8 seconds. The problem is that the timer1's interrupt override timer2's interrupt.

Is it possible to to use both of them?

3
  • 2
    No, timer1's interrupts do not override timer2's interrupts. Jun 29, 2016 at 7:35
  • Why are you using a timer for the watchdog instead of the normal watchdog timer in interrupt-and-reset mode? Jun 29, 2016 at 8:06
  • Could you post some code, so we could see why the timer1 interrupt might override the timer2 interrupt?
    – Gerben
    Jun 29, 2016 at 8:52

1 Answer 1

1

In general, once an embedded processor's interrupt fires, the interrupt routine clears the interrupt. Usually inside the interrupt routine. Failure to clear the interrupt normally inhibits further interrupts.

That said, in the Arduino paradigm, this is "taken care of" by the Arduino Timer library described here. In the example code:

/*
 *  Timer1 library example
 *  June 2008 | jesse dot tane at gmail dot com
 */

#include "TimerOne.h"

void setup()
{
  pinMode(10, OUTPUT);
  Timer1.initialize(500000);         // initialize timer1, and set a 1/2 second period
  Timer1.pwm(9, 512);                // setup pwm on pin 9, 50% duty cycle
  Timer1.attachInterrupt(callback);  // attaches callback() as a timer overflow interrupt
}

void callback()
{
  digitalWrite(10, digitalRead(10) ^ 1);
}

void loop()
{
  // your program here...
}

...you can see that the example does not actually contain "the" Timer interrupt routine but rather a callback() routine which the Timer library calls when the Timer interrupt occurs. Code to clear the Timer interrupt is missing and assumed to be in the Timer library.

Consider this if you are writing you own interrupt routine instead of using the Arduino's Timer library.

I'm going to have to call foul on myself. Clearing interrupts in their own interrupt routine is very common. But looking at this page, where the Atmel timer hardware is directly controlled (that is, the normal Arduino Timer library is not being used) and the real interrupt handler exists in the sketch, I see where this is not done. So, for the Atmel, there must be at least one timer mode where the timer interrupt does not have to be clear for the interrupt to happen again.

If you are using the Arduino's Timer library consider this warning from the Timer's library web page linked to above:

Be careful about trying to execute too complicated of an interrupt at too high of a frequency, or the CPU may never enter the main loop and your program will 'lock up'.

Finally, hardware timed interrupts are normally reserved for operations which are time sensitive and occur much faster than executing code can handle. For events that happen on the order of seconds, most applications setup 1 timed interrupt to count off a convenient interval. Say 1 second. Code is used to keep count and call the proper functions at the intended time.

3
  • 1
    On the AVR microcontrollers (and the Uno is AVR-based) most interrupt flags are cleared by the hardware when it executes the corresponding interrupt vector. This is the case for all the interrupts generated by the timers in all of their modes. Jun 30, 2016 at 14:27
  • @Edgar Bonet, thanks. That does appear to go against what other embedded processor makers do. At least that's been my experience. So not clearing the interrupt is not Rotem problem. Maybe he is doing too much in the interrupt. But at 8 seconds intervals, I doubt it. So it looks like we need Rotem to show us his code if we are to help.
    – st2000
    Jun 30, 2016 at 21:33
  • @IgnacioVazquez-Abrams: What architecture are you talking about? On the AVR, for most interrupt flags, the datasheet states (emphasis mine): “[The interrupt flag] is cleared by hardware when executing the corresponding interrupt handling vector. Alternatively, [the interrupt flag] is cleared by writing a logic one to the flag.” Note that the “vector” is the jmp to the ISR located in the interrupt vector table, thus it is executed before the ISR proper. Jul 1, 2016 at 8:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.