Need help in Timers

Hello Guys,
Right now i am using arduino mega adk, i am using it's timer5 as a external pulse counter. but when i connect with pulse sensor it's gives me random values i dont know why, any idea why it's not working for me

Code

TIMSK5 = 0x10;
TCCR5A = 0x00;
TCCR5B = 0x46;
TCNT5 = 0x00;

That doesn't compile.

Read this before posting a programming question

My Bed....... for this

void setup()
{
  Serial.begin(115200);
  TIMSK5 = 0x10;
  TCCR5A = 0x00;
  TCCR5B = 0x46;
  TCNT5  = 0x00; 
}

void loop()
{
  Serial.println(TCNT5);
}

Could you describe the wiring of this external pulse counter?

TIMSK3 = 0x10 ;

is clearly wrong as that bit is read-only.

What mode do you think you are programming? Normal mode 0b0000 is what you
have programmed, and you are setting the clock input to be T5 (pin 47) falling edge,
so you should be seeing the counter incrementing once per falling edge on that
pin.

If your pulse source isn't debounced that could explain why you see "random"
counts, but that could also be because you are connected to the wrong pin. T5 is
hard wired as pin 47 (from my notes, haven't actually tested this, can someone
else check?)

If you are changing the timer mode it is a good idea to change all three
control registers, TCCR5A/B/C, so that you don't inherit state from what it
happened to be before (although you should know what that is as you have
the Arduino sources).

TCNT5 is a 16 bit register, I would use 0x0000 rather than 0x00
to make that obvious in the source code.

You are also setting the input capture edge bit (0x40 in TCCR5B) despite the
fact you are apparently not using input capture.

MarkT:
T5 is hard wired as pin 47 (from my notes, haven't actually tested this, can someone else check?)

Pin 37 on the chip, D47 on the board.

Thanks for your suggestions.... :slight_smile:

I tried new config bits but still i did not find any result.....

void setup()
{
  Serial.begin(9600);
 TIFR5 = 0x00;
TIMSK5 = 0x00;
TCCR5A = 0x00;
TCCR5B = 0x06;
TCNT5 = 0x0000;
}

void loop()
{
  Serial.println(TCNT5);
  delay(40);
  TCNT5 = 0;
  delay(40);
}

Any thing you want to add so please tell me

Could you describe the wiring of this external pulse counter?

5V
GND
Output :- 47 pin of ADK board

Why are you clearing the counter each time through loop()? The serial printing is buffered and won't complete before the counter is reset.