How fast is a Arduino Mega board/Atmel 16MHz micro

Hello All,
playing around with the function digitalRead() I have tried to understand how fast a Arduino Mega (16 MHz micro) can go (can read from a digital pin) and I’d like to share with you these (maybe wrong) results

void loop(){
 
  unsigned long start,stop;

  start=micros();
  for (int i=0;i<10000;i++){
    while(digitalRead(PCLK) == HIGH);
    while(digitalRead(PCLK) == LOW);
  }
  stop=micros();
  Serial.println(((stop-start)/10000)); 
  exit(-1);

PCLK is connected to an external 260KHz clock. The result of the routine is 229.

I seem to understand that the output of the routine should be the distance in us of two PCLK falling edges which in reality is 1/260k=~3.85 us.
…hum… this sounds like the function digitalRead() is not fast enough to correctly sample each PCLK.

Does this make sense? is there a way to acquire the signal on the pin PCLK at each edge without loosing information?
I hope you can comment on this

thanks a lot
fabrizio

Yes a digital read does take some time and certainly is longer that 3.85uS.

In order to speed things up you need to do direct port addressing (see the playground) as you pay for the convenience of the individual pin input / output abstraction.

Thank you for your help.

Would it be somebody who can provide a simple example on how to speed up the timing example above?

thanks a lot fabrizio