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
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.
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.
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.
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.