Pulse counter with Timer ?

I have not found so far any way to use the Timers of Arduino as pulse counters.

I'd like a counter to collect pulses so that I can read the cumulated pulses from the counter register, reset the counter and let it continue running. I'm not interested in frequency, length of pulse nor time between them. All the mathematics will be done elsewhere. The frequency of these pulses vary from once per 5 minutes to 10kHz

Is there any examples ? I have found none.

I've been using counters in other environments for about 30 years and know enough about electronics.

mistofeles: The frequency of these pulses vary from once per 5 minutes to 10kHz

Is there any examples ? I have found none.

I've been using counters in other environments for about 30 years and know enough about electronics.

Frequencies up to 10 kHz can be counted by using hardware interrupts on the RISING or FALLING flank. Hardware interrupts are directly supported by the Arduino core library ("attachInterrupt()").

But at 10 kHz the period is only 100µs, so you better not use that in programs that either disables interrupts for more than 100µs or that use interrupt handling routines that run longer than 100µs.

What you perhaps are referring to is using a timer as a hardware counter, which is not very common in the Arduino programming environment as - by board design this is only possible with one single pin (pin number depends on the board) - using a timer as a counter with external clock signal is not supported by the Arduino core library

But it can be done with direct register/port programming. Perhaps you better watch out for "AVR Atmega demo code" instead of "Arduino demo code" doing so.

It's easy to set up an interrupt to count pulses on an input pin. Easy peasy, one line interrupt handler.

volatile long int count;
void countISR ()
{
count++;
}

Your application needs will determine if this method is sufficient or whether you need to use the external timer/counter input method.

Now I do not understand what was wrong in my message.

I said that I want to use the Timer Counter.

I do very well know how to count one, two, three,... by program and I do know how to use interrupts. No problem here.

And I do not understand, why you say Arduino should use more pins while counting pulses. I would understand, if you want to use synchronous counting, but most of the counters allow you to select also a asynchronous mode. That means counting in edge triggered mode.

I have a good reason to use Timer/Counter. The other routines of the system are so time sensitive that using interrupts is no answer.

mistofeles: I have a good reason to use Timer/Counter. The other routines of the system are so time sensitive that using interrupts is no answer.

So if you want to avoid interrupts counting pulses as well as polling the pin state, you most likely refer to what is described in the Atmega328 datasheet in chapter "16.3 External Clock Source" for Timer0 and Timer1:

"An external clock source applied to the T1/T0 pin can be used as Timer/Counter clock"

This mode of Timer0/Timer1 is not supported by the Arduino software. The T0 pin is even not available on the Arduino boards. You can use the T1 pin with Atmega328 based boards as a hardware counter, which is labeled "D5" on an Arduino UNO.

As there is no ready-to-use Arduino library function available, you will have to configure and use that by direct pin/port programming. Arduino users hardly use that timer/counter configuration for hardware counting.

So if you are looking for a code example, perhaps not only look for Arduino sources, but search for general AVR/Atmega/GCC code that uses the T1 register with external clock setting for your type of controller. Please keep in mind, that your code will be strictly dependent on hardware and needs changes if you want to run the code on Atmega328, Atmega2560 oder Atmega32U4 controllers. If you know the type of controller, you can start loading the appropriate datasheet for the controller where you can find all information about the configuration settings needed to use a counter/timer with an external clock as a counting source.