Digital Pulse Counting with Mega 2560

Hi,
I'm very new to arduino world. I had some problems during my research through web so maybe you guys can help me with which way I should use for my problem.
First of all, I'll start with what I want to do. I'm in need of measuring digital pulses coming from my sensor in some time intervals down to microseconds. However i had a lot of trouble doing that. I have found some codes through my research. However ppl doing it with some delay function. That makes milisec error during my counting and it a big problem for me. So I tried to interrupt counting with some other descending counter to make it accurate. And come with this

volatile int carryOut = 0;
volatile int freqCarry = 0;
void setup(){
cli();
Serial.begin(9600);
TCCR5A = 0;
TCCR1B = 0x07;
bitSet(TCCR5A ,0);
bitSet(TCCR5A ,1);
bitSet(TCCR5A,2);
TIMSK5 |= (1 << TOIE5) ;
sei();
}
void loop(){

delay(1000);
Serial.println(carryOut);
Serial.println("--");
Serial.println(TCNT5);
carryOut = 0;
TCNT5 = 0;

}

ISR(TIMER5_OVF_vect){
carryOut++;
}

The problem is at least i should be measuring some pulses accurate or not using pin 11 or 12 but i cannot . Can you help me with that thanks in advance

However ppl doing it with some delay function.

"ppl" - Pulse per -?-"

You are not counting pulses. Nowhere do you read a pin that might be changing state.

Now, what ARE you trying to count pulses for, and where do you think you are doing that?

@AWOL, I think "ppl" is short for people.

@otobus, I suggest that you set up an interrupt that just increments a variable every time it detects a rising pulse. Then periodically you can check the value of micros() and the value that the interrupt count has reached. Then figure out how many microsecs passed since you last checked and how many additional counts there were. I'm sure you can guess the rest.

I would only bother with Timer5 if that simple approach doesn't give suitable results.

...R

volatile int carryOut = 0;
volatile int freqCarry = 0;
volatile int freq = 0;
void setup(){
  cli();
  Serial.begin(9600);
  TCCR5A = 0; 
  TCCR5B = 0x07;  
  TIMSK5 |= (1 << TOIE5) ;
  sei();  
}
void loop(){

    delay(1000);
    Serial.println("COut:"); Serial.println(carryOut);
    freq = 65535 * carryOut + TCNT5;
    Serial.println("frequency:"); Serial.println(freq);   
    Serial.println("Clock:"); Serial.println(TCNT5);
    TCNT5 = 0;
    carryOut = 0;
      
}

ISR(TIMER5_OVF_vect){ 
 carryOut++;
}

@Pauls This is what i got at least. The pin i was trying to read was 11 since I set the B channel of fifth clock of mega as an external counter. However it was all wrong what i understand; it should be pin 47. (some guys put wrong pins or for older board version on some websites), at last I am able to read some value. I am trying to count pulses for luminescence sensor for a project so that I can see how they behave with respect to time.
@Robin2 That approach didin't work for pulses which has greater frequency than 5-6 MHz . However with this method i still have problem with accuracy which is probably related to delay() function inside the loop. I will try to use another counter for a fixed time whenever that one overflow i will put the code inside the loop function into service routine of this counter's interrupt with overflow. Hope it is going to work well.. And of course changing cable trough pulse generator to arduino makes it little bit accurate as I experimented.

Thanks for help guys; I'll be very glad to hear for your advices more

otobus:
@Robin2 That approach didin't work for pulses which has greater frequency than 5-6 MHz .

Reading 5MHz pulses on a 16MHz MCU is a challenge.

...R

Robin2:

otobus:
@Robin2 That approach didin't work for pulses which has greater frequency than 5-6 MHz .

Reading 5MHz pulses on a 16MHz MCU is a challenge.

...R

LOL. Bit of an understatement. I wonder if the OP meant 5KHz?

DuncanC:
LOL. Bit of an understatement. I wonder if the OP meant 5KHz?

Perhaps. But then it would be trivial.

...R

otobus:
The problem is at least i should be measuring some pulses accurate or not using pin 11 or 12 but i cannot . Can you help me with that thanks in advance

have you looked at pulseIn() ?