Pulse counter with interrupts maximum result

Hi,

following question:

I use attachinterrput function to pin 2 for getting theoretical possible number of pulses 270000 per second.
All pulses comes in series and it last for one second only, this can happen more or less any time, so I can not use frequency library to measure frequency continuously. I need to count pulses when they start and make sure that value is correct.
So far I am sure that I can read 125000 pulses with no problem.
I just do not know when physical maximum happens for Arduino. Will I be able to read 270000 pulses with same code and will value be reliable?
Pulses are depending on the process, I can not simulate this particular process right now, thats why I need to know If all is good.

Is it the only and right way to do it? Can you advise some backup solution with using registers directly?

Arduino Mini 05

///sample
int pin_count = 2;
int pin_count_res = 3;
volatile long unsigned cnt_value=0;

void setup() {

attachInterrupt(digitalPinToInterrupt(pin_count), CNTUP, RISING);
attachInterrupt(digitalPinToInterrupt(pin_count_res), CNTRES, RISING);
}

void CNTUP() {
cnt_value++;
}

void CNTRES() {
cnt_value=0;
}

thanks!

If that code is the final code (that is, the Arduino is not going to be busy doing other things) then I would expect it to cope. But somewhere you will need code to display the value, I'm guessing. And other stuff?

That said, counting pulses is best carried out in hardware which can count into the gigahertz level without effort and does not require a microcontroller to do so (a bit of a waste of an Arduino, really!)

Have you looked into digital counters, eg an 8-bit counter like the 74592? Then you can read those values into an Arduino and use the microcontroller for other stuff too.

You example is simple enough that you can work it out from the datasheet. There is a specific number of instructions to enter and exit an ISR. Then you are only loading 4 bytes from memory, doing 4 additions and saving 4 bytes back to memory. Divide by your clock speed and you have your answer.

Do you have a second Arduino to make pulses for testing? Unfortunately programming a pulse generator for that frequency is not as simple.

Any of the 32-bit Arduinos will be able to do this faster. They also have a much higher clock speed. I've never looked into the overhead for entering an ISR on those, but it can't be worse than the 8-bit ones in real time, even if the number of instructions is higher.

Yes, I had to minimize rest of the code so that I refresh display only if counter total value is changed. It does not happen too often.
Before I had serious troubles with updating screen :slight_smile:

Now I have tolerance +-2, in my case, this is fine.

I will check 8 bit counter and will try to calculate maximum resource of ISR.
Do you have any direct example how this hardware counter communicates with arduino?

I use arduino for couple of other functions of the device and took it, since I had it occasionally at home.

The issue for me, that I am used to program industrial PLCs, but not chips. Chips have absolutely different meaning for counters and timers. So a bit of struggling, I guess

frzw:
So a bit of struggling, I guess

Think of it as a learning experience. :slight_smile:

Glass half full and all that! After all, Electronics and Arduino programming is supposed to be FUN, right?

It is learning off cause, but as always timeline was "last month". Could solve it easily with any PLC, but lack of space, power and weight requirements here.
I found enormous amount of hw counters at farnell.com, but I do not get, how Arduino can read from them. I mean how to choose one, which could be readable?

I mean how to choose one, which could be readable?

Choose one with an interface that the Arduino can use. SPI perhaps