Reading and resetting counters on the '328

I have a question regarding the use of counters on the UNO. The 16 bit counter is plenty, and I want to read it once per second then work on those readings.

Planning to use an ISR that is triggered by a 1PPS GPS signal to transfer the reading and reset the counter.

I have seen a register code that transfers the counter value to the buffer, and have seen the register use but really need to see more information.


Can the counter be read and reset via the register settings on the fly?

Is it possible to count all events on the counter input without losing a pulse?


Hi, Bill.
It sounds like you are programming in machine code.

If you used C++ you can make as many counters as you need just by declaring a variable.

unsigned int   variable = 0;

Will give you a 16bit counter that counts from 0 to 65535.

Then in your ISR function you have this;

variable = variable +1;

Will increment the variable.


variable = 0;

Will reset it when you want.

I hope this is what you are looking for... Tom... :slight_smile:

Cool great idea. It can also be as big or small as necessary. My project will be using the 16 bit hardware counter on the '328. Not sure I have the timing overhead to do it in SW.


You can use the hardware timer/counter: look at datasheet to timer/counter1 description.
You can set it to be timed from external clock, it will then count pulses on T1 pin. It will count all pulses if they are not too fast - a bit under main clock speed/2 (8MHz Arduino Uno) provided the signal has 50% duty (both HIGH and LOW time of signal should be over 1 main clock tick). But if the pulses come too fast then timer will overflow in 1 second ofc.
You can set Input Capture to buffer time of the timer at the moment of PPS signal from GPS. It even has its own flag and interrupt.
You CANNOT reset the counter at input capture. But you don't need it: you may store previous captured value and subtract it from the new one to get number of pulses during one second.
Note 2 Arduino's PWM pins depemd on this timer, you won't be able to use PWM on those pins.