Hello Guys,
I am trying to learn how to handle interrupts with the Arduino 2009 and I was wondering if I can have your opinion on two matters:
is the Arduino function attachInterrupt() much slower then the ISR(INT1_vect)? see code below? I am trying to trigger the interrupt with an external 200 KHz signal and I'd like to know if I need to avoid using attachInterrupt()
is the code below right? I seem to get the interrupt ISR() triggered when signal changes on pin 2 as well as when signal changes on pin 3.... does INT1 correspond to pin 2 and INT0 to pin 3? how do find this info?
// example of how to use a simple interrupt with the Arduino Duemilanove (ATMEGA168)
//
// original code:
// http://gonium.net/md/2006/12/20/handling-external-interrupts-with-arduino/
#include <avr/io.h>
#include <avr/interrupt.h>
int ledPin = 13; // pin that correspond to the LED
volatile int value = 0;
// this is the function that gets triggered by the interrupt
ISR(INT1_vect) {
value = !value;
}
void setup() {
Serial.begin(9600);
Serial.println("start...");
//turn LED on
pinMode(ledPin, OUTPUT);
Serial.println("Initialise the interrupt handler");
EIMSK |= ( 1 << INT1); // arduino pin-2
EICRA |= ( 1 << ISC00); // Signal change triggers interrupt
EICRA |= ( 0 << ISC01);
Serial.println("Finished initialisation");
}
void loop() {
if (value) {
Serial.println("Value high!");
digitalWrite(ledPin, HIGH);
} else {
Serial.println("Value low!");
digitalWrite(ledPin, LOW);
}
delay(2000);
}
The code for setting up interrupts is in the file WInterrupts.c in the Arduino cores directory. This is how interrupt 0 is enabled:
EICRA = (EICRA & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
EIMSK |= (1 << INT0);
The Arduino code calls a function from the ISR and this will slow things down compared to using the ISR directly. But as I mentioned in another thread, If you are processing a repeating signal at 200khz or more, you may be better off not using interrupts.
mem,
thanks for your hint. Yeah the other post about the image stuff was me ! in fact it was RuggedCircuits who suggested:
"260 kHz is pretty fast to read with digitalRead. I would go down to the port level and use either a pin change interrupt, regular interrupt pin, or input capture (associated with Timer 1)."
that you were a little sceptical about the interrupt solution... (Reply #7).... what solution do you think could achieve a successful decoding at 260KHz?
Basically I'd like to acquire one line (170 byte) resize it in a 57 byte array (averaging groups of 3 byte) and store it. Repeat it line by line for the whole frame which is about 3.3KB (Arduino Mega has 8 KB of SRAM)
Once a frame is acquired I stop the whole thing and I send it to through the serial port and start again from the beginning. The only problem in all this, is that clock is 260KHz.... I could lower it... but if i do so I loose the 3 frame per second which is what I aim for.
I hope this is enough info for judging whether the interrupt solution could be good.
I doubt any of those would help you to decode the video signal. 260KHz clock rate means you have 60 cpu cycles to read and decode each pixel. The interrupt handler overhead alone would eat into much of this. I think your code would need to sit in a tight loop to monitor the data using direct port i/o and store the bytes in an array. You don't have enough memory to hold a complete frame so you will need to do some processing in the line blanking interval – and that does not give you much to play with.
My reply in that other thread expressed my doubt that an interrupt would be efficient enough and suggested an approach that would be more efficient that did not use interrupts.
I suggest you work on the code for processing the data first and see how much time you need to handle each line. Ignore interrupts for now and see if you can capture, resize and store the incoming bytes in the very short time you have between the end of one line and the beginning of the next.