Count Input pulse using 16 bit counter present in Arduino Mega 2560.

I trying to count the input pulse using the 16 bit counter but my Serial port shows zero. I’m using Arduino mega 2560. and I also want to know what is input capture register and can I use it here.

my code is:

void setup()
{
Serial.begin(9600);
pinMode(5,INPUT_PULLUP);

}

void loop()
{
TCNT1 = 0x0000; // counter starts form 0.
TCCR1A= 0x00; //16-bit counter normal mode
TCCR1B= 0x06; //count from falling edge
delay(1000); // count for 1 sec.
TCCR1B = 0x00; // stop counter.
Serial.print(TCNT1);

}

I’m giving square to pin 5.

If you are simply counting pulses, look at external interrupts instead of timers.
The input capture system is used for precision measurement of a wave form to determine frequency, duty cycle etc. Again, not really for counting pulses.

Timers can be clocked on an external pin, which is the normal way to count pulses in hardware,
however only certain pins are connected to each timer, consult the datasheet for details.

input capture register is used to collect a snapshot of a timer's counter on an external signal -
if the counter is internally clocked, and the timer is suitably configured the timer register is
copied to the input capture register on the external pin changing.

I suggest reading the relevant section in the datasheet carefully. Such functionality is not
portable across different microcontrollers though, so often its a bad idea to use such
specific hardware if the performance isn't critical.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.