Arduino mega 2560 Digital sample rate

Hello.

I have been hunting around for sample rates and the analog rate I can find, but I am struggling to find the digital sample rate. I`m looking to sample 2 digital inputs at around 5000 pulses per second, and sample them at the same time (no delays)

Would this be possible ?

If something similar has been asked before then apologies.

Cheers for the help in advance.

Nikki

So you want to read 2 pins every 200uS. Not hard.

void loop(){
  current_time = micros();  // capture the current time; time elements are all unsigned long data type
  if ( (current_time - previous_time)>=sample_time){  // time for next sample?
    previous_time = previous_time + sample_time; // set up for next tim
    // direct port manipulation read, and mask off for the intended bit. S/W guys probably have a fancier way to do this
    dataBit0 = PORTx & B00000001; // where x is the port you are reading, mask for the desired bit
    dataBit1 = (PORTx & B00000010)>1; // result is 0,1 for the 2 bytes dataBit0 & dataBit1
   }
// do other stuff while waiting for next sample_time to come up
}

dataBit1 = (PORTx & B00000010)>1; close... :wink:

Although, yes, I guess ok,.
Sorry. Knee jerk.
Or just, in my case, jerk.

I've done it with just digitalRead at 100us interval. If you need say 10us or less, maybe read directly from the port. If even less time is required, use some assembly. I don't even know how to write assembly for this chip since I didn't find need to do 4us timing. I kind of said bye-bye to 80386 assembly in late 1990s, although it's cool to know it.

rep movsd; //any takers?

"rep movsd;"

I don't even know what that means liudr.

AWOL, I think I get what you are hinting at - should have been >> for shift right?

I'm sure this is kludgy if the bits 6 & 7 are used for example,

dataBit1 = (PORTx & B01000000)>>6; //  double >> for shift right?

I guess another way is this, "seems" like it would be slower; maybe faster than 6 or 7 shift-rights tho:

if ( PORTx & B01000000 ){dataBit6 = 1;} // dB6 = 1 when the masking result is not 0
else {dataBit6 = 0;} // otherwise dB6 = 0

but not bad if bits 0 & 1 are used, so there is minimal shifting.

Awesome, cheers very much