C++ reading off ADC at pace of CLK frequency

  adc, arduino, c++, for-loop, indexing

I am using an hyperspectral sensor from Hamamatsu (C12880MA). So far, I got my firmware done to fit the Atmega328p, timings etc. are working great.

But now, I bumped into some issues regarding reading the measurements off the ADC.

According to my Clock-Signal generation (at variable f between 0.5 and 5 MHz), I need to read off the ADC values at exact "flag" values.
I am using the internal TC1 timer for clock generation. At each toggle of the CLK signal, I set a flag via an ISR to time the other signals / restart the program.

Now to the problem: I know (see datasheet for ref), that the VIDEO Signal of the sensor will appear between the flags "ADC_start" and "ADC_End" at the pace of the generated CLK. I have to read the values through the internal Arduino ADC at exactly the correct flag, to later match with the correct underlaying wavelengths.

Here are the essential snippets of the code:

volatile uint16_t flag = 0;
uint16_t data[288]; // define 1d matrix for data storage
uint16_t ST_Start = 2; // starter flag for the programm
uint16_t ST_End = 134; // end flag for ST signal
uint16_t ADC_Start = 310; //starter flag for ADC readout
uint16_t ADC_End = 886; //end flag for ADC readout
uint16_t index; //index used for data storage
volatile uint16_t End = 1000; // end flag for programm restart and clearing cache

The ISR handles increasing the flag on every CPU clock..


if  (flag <= End){

    flag = flag+1;    
} else
    flag = 0;

this is my readData function. Unfortunately I have not found the correct way to read the ADC values at the exact flags needed. Say I need to read values at the flags 300, 302, 304, …, 900 (on every second flag, between an set interval; ADC_Start to ADC_End)

void readData() {

    if (flag >= ADC_Start && flag <= ADC_End ){

        for (uint16_t i = ADC_Start; i < ADC_End; i = i+2)
          index  = i - ADC_Start;
          data[index] = analogRead(VID);
    } else { }  


HereĀ“s the data sheet: https://www.hamamatsu.com/resources/pdf/ssd/c12880ma_kacc1226e.pdf
See p.7 for timings.


Source: Windows Questions C++