ISR for Timer1 not firing

Hi everybody, I’m new to the Arduino world and am trying to detect an incoming pulse on Int 0.

Hardware: I am using an ATTiny85 as an external generator to feed a signal to the Mega.

The tiny is pumping out a square wave that I can read on any one of the digital pins of the mega (eg: 21) if I attach an interrupt to it. I can see the square wave on the serial plotter. However, I would like to have it trigger an interrupt on Int 0.

I’ve connected the output from the Tiny to pin 53 (PB0) and then used the following code:

ISR(TIMER1_CAPT_vect)
{
Serial.println(“In ISR”);

}

void setup()
{

Serial.begin(9600, SERIAL_8N1); // Open the serial library @ 9600bps

// Initialize Timer1
TCNT1 = 0;

TCCR1B &= ~(1 << ICES1); // Capture on falling edge
TIMSK1 |= (1 << ICIE1);

// Set internal pullup resistor for Timer1 ICP1
DDRB &= ~(1 << PB0);

// Start the timer
TCCR1B |= (1 << CS10);

}

I leave the loop() empty since this very basic work is being done inside the ISR.

As an experiment, I enable the overflow as well using:
TIMSK1 |= (1 << ICIE1) | (1 << TOIE1);

and it was indeed caught inside here:

ISR(TIMER1_OVF_vect)
{
Serial.println(“Overflow”);
}

I assume it’s something simple that I’m missing, which is why digging through the other forum posts hasn’t given me the answer yet.

Any ideas?

ISR(TIMER1_CAPT_vect)
{
   Serial.println("In ISR");

}

The very first thing you need to learn is what you can, and can not, do in an ISR. Serial.print(),especially on a ATTiny85 is a definite no!

Set a flag in the ISR, check it and print to serial from loop().

Be sure to declare variables that are shared with an interrupt as volatile.

For a flag variable, use byte or char type to avoid corruption of the variable contents. (That can happen with multibyte variables when both the main program and the ISR are attempting to accessing the same storage location).

This sounds exactly like my problem. Aside from his coding style not being optimized, I'd love to hear the solution regarding how to set up input capture on the mega.

I've connected the output from the Tiny to pin 53 (PB0)

Did you connect the grounds?

PaulS:
The very first thing you need to learn is what you can, and can not, do in an ISR. Serial.print(),especially on a ATTiny85 is a definite no!

Thank you, Paul.

This code is running on the Mega and not the Tiny. Sorry for the ambiguity. I didn't wire up a UART or Software UART on the Tiny to debug it.

Definitely, in the normal run of things, I wouldn't be printing (using a massive # of cycles) inside an ISR. It's just a test line to watch it fire. In theory.

That said, that you for the tip.

Gahhhrrrlic:
This sounds exactly like my problem. Aside from his coding style not being optimized, I'd love to hear the solution regarding how to set up input capture on the mega.

This post is quite similar, but all I can infer from the way the thread ends is that the input pins were incorrect.

The Arduino MEGA doesn't have a broken out pin that connects to Input Capture Pin for Timer1 (ICP1).

You can use:
Timer4 ICP4 (Pin 49)
Timer5 ICP5 (Pin 48)

robcanada:
Definitely, in the normal run of things, I wouldn't be printing (using a massive # of cycles) inside an ISR. It's just a test line to watch it fire. In theory.

It's more serious than just the lack of efficiency... interrupts are disabled by default while an ISR is executing. Since serial uses interrupts, it won't work at all.

Since serial uses interrupts, it won't work at all.

Actually, it can. Serial.print() calls Serial.write() actually send the data. If the buffer that print() writes to is full, the interrupt that normally fires to send data out can be triggered manually. write() does that, if it finds that it was called while interrupts are disabled.

Not that calling Serial.print() in an ISR is a good idea, but it no longer causes a lockup like it used to.