ADC with trigger

Hi, I have seen this program

\begin{lstlisting}

const byte adcPin = 0; // A0

const int MAX_RESULTS = 256;

volatile int results [MAX_RESULTS];
volatile int resultNumber;

// ADC complete ISR
ISR (ADC_vect)
{
if (resultNumber >= MAX_RESULTS)
ADCSRA = 0; // turn off ADC
else
results [resultNumber++] = ADC;
} // end of ADC_vect

EMPTY_INTERRUPT (TIMER1_COMPB_vect);

void setup ()
{
Serial.begin (115200);
Serial.println ();

// reset Timer 1
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
TCCR1B = bit (CS11) | bit (WGM12); // CTC, prescaler of 8
TIMSK1 = bit (OCIE1B); // WTF?
OCR1A = 39;
OCR1B = 39; // 20 uS - sampling frequency 50 kHz

ADCSRA = bit (ADEN) | bit (ADIE) | bit (ADIF); // turn ADC on, want interrupt on completion
ADCSRA |= bit (ADPS2); // Prescaler of 16
ADMUX = bit (REFS0) | (adcPin & 7);
ADCSRB = bit (ADTS0) | bit (ADTS2); // Timer/Counter1 Compare Match B
ADCSRA |= bit (ADATE); // turn on automatic triggering

// wait for buffer to fill
while (resultNumber < MAX_RESULTS)
{ }

for (int i = 0; i < MAX_RESULTS; i++)
Serial.println (results );

  • } // end of setup*
    void loop () { }
    \end{lstlisting}
    on this site:
    Gammon Forum : Electronics : Microprocessors : ADC conversion on the Arduino (analogRead)
    I’m not an expert of Arduino but my professor have used it to read some voltage values and I want to understand a bit more about this program.
    1) I know what a isr() is, but what is ISR(ADC_vect)?
    2)I know what a trigger is but in this program what does automatic triggering do?
    3) I don’t understand what the while does, how does it knew when the buffer is full? There is nothing in the while

but what is ISR(ADC_vect)?

The Arduino uses an AVR processor.
Now, combine that useful nugget of information with “ADC_vect”, and what do you get from Mr Google?

Please remember to use code tags when posting code

The ATmega328P Datasheet would be an essential read for understanding this code.

2)I know what a trigger is but in this program what does automatic triggering do?

Reads the ADC at regular intervals, set by Timer1 compare.

  1. I don't understand what the while does

Nothing, until the while condition becomes true.

The comment is not the sign of a knowledgeable programmer, but this line is in fact essential.

  TIMSK1 = bit (OCIE1B);  // WTF?

jremington:
The comment is not the sign of a knowledgeable programmer, but this line is in fact essential.

  TIMSK1 = bit (OCIE1B);  // WTF?

This isn’t too good either:

while (resultNumber < MAX_RESULTS)
    { }

Accesses of the 16-bit ‘resultNumber’ variable will be non-atomic on an 8-bit AVR. They need to be protected by a Critical Section.