problem with ADC trigger on MEGA 2560

Hey everyone!

I'm having a bit of trouble getting the ADC interrupt to work.

I think the problem may be in the way I handle the interrupt. I'm trying to use:

-Free running mode at 500 kHz (ie: T_clk = 16Khz, so T_clk/32). this is less precise but that is okay.
-ADC 0

When I just print the ADCL register (which holds the converted data), like this, it at least prints:

void loop(){
  delay(100);
  if (!(ADCSRB & 0x40)){ // ie: anding with 0x40, or 0100-0000, means ADSC (bit 6) is high, meaning conversion is not done.          //so, we take the 'not'; if we take the loop, the conversion is done. pp293, 26.8.3
    Serial.println(ADCL);
  }
}

However, when I use the ISR loop at the bottom of this code (and have nothing in the loop except a delay), the serial monitor prints out just two values, and that is it. Any ideas?

Thanks in advance! Happy to clarify or try things.

// Note: all register definitions found in: // ADC: DOC_2549, section 26 
// http://www.atmel.com/Images/doc2549.pdf

void setup(){ 
  ADMUX = 0x80; // 8: 1000, choose 1.1V reference by REFS1=1,REFS0=0 (REFSx=10), left just (ADLAR = 0), Select ADC0 for input (MUXx=0).  *!Assume reading ADC 0!*

// also, from 11.9.6, pp 55: 
// For analog input pins, the digital input buffer should be disabled at all times
  DIDR0 = 0xff; // ie: disable (write to one) to save power
  DIDR2 = 0xff; // ie: disable (write to one) to save power

  
  ADCSRA =  = 0xed; // e (14): 1110 means "enable ADC (ADEN=1),start ADC(ADSC=1), auto-trigger ADC(ADATE=1), interrupt flag not messed with (ADIF=0) 
                    // d  (13): 1101 means "enable ADC interrupt(ADIE=1), then set ADC clock to 1/32 of the input (ADPS=101)

  ADCSRB = 0x00; // 0: 0000 means "X (0), for the analog comparator (ACME = 0), for pin selection
            // 0: 0000 means "MUX5 for pin selection is zero (MUX5=0), free running (ADTSx = 0)

  sei();       //SREG |= 0x80; // 1000-0000 : ie: enable interrupts. See: 7.4.1, pp 14

  Serial.begin(9600);

}

ISR(ADC_vect){
  Serial.println(ADCL);
  sei(); //SREG |= 0x80; // 1000-0000 : ie: enable interrupts. See: 7.4.1, pp 14
}

ISR(ADC_vect){
Serial.println(ADCL); // Calling any Serial methods in an interrupt service routine is a bad idea
sei(); //SREG |= 0x80; // Reenabling interrupts in an interrupt service routine is a very bad idea
}

However, when I use the ISR loop at the bottom of this code (and have nothing in the loop except a delay), the serial monitor prints out just two values, and that is it.

You can't have a delay in an ISR, either. ISRs are supposed to be fast. No kind of delay resembles fast, by any definition of fast.

Ah, thanks Coding Badly! Is there some place with more tips like this, especially for interrupts? For interrupts, the docsheet doesn't have much except the vector table, and I've tried searching the forums/using google-fu (to little avail).

PaulS:

However, when I use the ISR loop at the bottom of this code (and have nothing in the loop except a delay), the serial monitor prints out just two values, and that is it.

You can't have a delay in an ISR, either. ISRs are supposed to be fast. No kind of delay resembles fast, by any definition of fast.

Oh, I wasn't putting a delay in the ISR. I meant a delay in the void loop() method! That way, the code just 'spins' in the loop (with a delay), and the interrupt should print things (which seems a bad idea, according to Coding Badly). I'll keep that in mind though, thanks!

*** edit ***

Also, For anyone curious, I fixed the problem by having the following for my loop:

void loop(){
  delay(100);
  if (adc_ready){
    Serial.println(ADCL);
    adc_ready = 0;
  }
}

The ISR is below. Obviously very simple, but it is a starting point for the interrupt changing the global state of things. My eventual goal is to make a 2-D resistive touchscreen. Long way to go, but I'm excited. Thanks for the lightning fast responses!

ISR(ADC_vect){
   adc_ready = 1;
}

*** edit to my edit! ***

adc_ready should be declared volatile!

Nick Gammon has a nice write-up about interrupts.

There is this...
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1261124850