0

I've recently been playing around with the ATtiny85 as a means of prototyping some simple electronics in a very small package. I'm having a spot of trouble with this since the language used for many of its functions is very different (and a lot less intuitive!) than that found in a standard Arduino sketch. I'm having some difficulty finding a decent reference for the hardware-specific functions too.

Primarily, what I'd like to do is listen for both a pin change and a timer at the same time. A change of state in the pin will reset the timer, but at the same time the code needs to respond to the timer itself if it ends before the pin's state changes.

Now, from the tutorials I've managed to find it seems that both pin change and timer interrupts are funnelled through the same function - ISR(). What I'd like to know is:

  1. Is it possible to have both a pin and a timer interrupt going at the same time?
  2. Assuming they both call the same function, how do you tell them apart?
Bence Kaulics
  • 7,066
  • 7
  • 33
  • 63
Ash
  • 9,064
  • 3
  • 48
  • 59

3 Answers3

3

ISR() is not a function, it's a construct (macro) that is used to generate the stub for an ISR as well as inject the ISR into the vector table. The vector name passed to the macro determines which interrupt it services.

ISR(INT0_vect)
{
// Handle external interrupt 0 (PB2)
   ...
};

ISR(TIM0_OVF_vect)
{
// Handle timer 0 overflow
   ...
};
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • Ahhh! You see what I mean about not being able to find the proper documentation? None of the tutorials I have found mentioned this fact. – Ash Oct 23 '16 at 09:44
0

According to the datasheet ATtiny85 doesn't have the same interrupt vector for PCINT0 and TIMER1 COMPA/OVF/COMPB, so you can define different ISR handlers for each one of them.

If you're using the same handler for more interrupts, it might be impossible to differentiate between them, as interrupt flags are usually cleared by hardware on ISR vector execution.

KIIV
  • 3,534
  • 2
  • 18
  • 23
0

As an addition to the accepted answer:

Is it possible to have both a pin and a timer interrupt going at the same time?

The interrupt can occur at exactly the same time on the hardware level and the corresponding interrupt flags would be set accordingly. The flags indicate that the ISR for the respective interrupt should be executed. But the actual ISRs are (more or less obviously) not executed at the same time / in parallel. Which ISR is executed first (in case multiple interrupts are pending) depends on the interrupt priority, which is specified in the interrupt vector table from the data sheet.

Rev
  • 5,827
  • 4
  • 27
  • 51