Measure a nonperiodic signal

Hi all,

i like to count a 2us pulse which is generated by a laser powersupply.
the signal is an non periodic signal and si it is difficult to find onlay the short pulses with 2us.
when the signal is periodic a have on problems to cout the pulses.
i am using the program posted ba NICK GAMMON “Frequency Counter sketch for Atmega328”.

The real signal is the following i like to measure:


| | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | |
— | |-------- – – – … – ----------------- ------ --------- ------ ---- …

200ms 2us 2us 2us 2us 2us 400ms 2us 5us >200ms

< NO COUNT > < COUNT > < NO COUNT >

i only like to count the 2us pulse when it comes periodically.

i hope i get an anwer.

thanks very much !!

best regards
paul

All you want to do is count the pulses?

What arduino are you using?

2us is short for a processor at 16Mhz

One way is to increment a counter on a rising edge interrupt but that may not get two pulses close together. You will have a better chance of capturing each pulse with an external counter such as the CD4040. However, you will need some kind of input multiplexer to scan the 12 output bits of the counter. A pair of 8:3 chips would work wired as a 16:4. There are many special purpose chips in the CD40xx and CD45xx family.

I haven't tried using the input capture feature yet of timer/counter1, but something like this might work:

  • Configure timer/counter1 as a counter (use the T1 pin for your signal input).
  • Monitor (poll) the status of T1 in the main loop.
  • If HIGH and stable for >=100ms, then do a software capture to read the count value.
  • First falling edge after stable HIGH initiates a new test (or allows counting to continue).

^^^ this

@Puali_paul: Please stop reporting these posts to moderators. Just reply to the posts if you want answers.

hi,

i am using the arduino nano with the ATMega328 with a clockrate of 16Mhz. i was possible to count the pulses with a continous signal with a 2us pulse width.

but my problem is, that i have different pulse width in my signal and i want only count the 2us pulses.

What is the shortest time between pulses?

The fastest method I can think of is to use an ISR triggered on RISING edge, then in the ISR wait 2us and sample the pin again to see if it was short or long. Due to the enter/exit overhead of an ISR that will probably not handle gaps between pulses shorter than 5 or 6 us.

So if the pulses come every 4us you'll probably not be able to do it without external hardware (monostable and some gates)

@pauli_paul

at 16MHz in 2us HIGH + 2us LOW for a full cycle, you have 4us which is the time to execute 64 x 1 cycle processor instructions.

if you add overhead of saving registers, calling an IRQ and you probably have less than 50 cycles at best... no way you get this if you do any digitalRead stuff, need to go to PORT level and try to see if the compiler gives you good enough code. I would even recommend going assembly level to control exact timing.

On an Uno, I thought I’d try the input capture feature as a counter.

  • No timer1 interrupts are used or enabled.
  • The test code creates a pulse stream on pin 11 (MOSI).
  • Pin 11 is jumpered to pin 5 (T1) which is the counter input pin.
  • The pulse pattern has 6 rising edges, then 200ms delay, then repeats.
  • Some of the pulses have 500ns period.Pulse waveform:

Zoomed:

Results:

Code:

#include "SPI.h"

void setup() {
  Serial.begin(115200);
  Serial.println("\nRunning Test .....");
  SPI.begin();
  inputCaptureInit();
}

void loop() {
  SPI.transfer(0x5C);         // send pulses
  SPI.transfer(0xEA);         // send pulses
  delay(200);                 // 200ms HIGH signal
  Serial.println (TCNT1);
}

void inputCaptureInit()
{
  TCCR1A = 0;
  TCCR1B = (1 << ICES1) | (1 << CS12) | (1 << CS11) | (1 << CS10);   // clock on rising edge, signal input on T1 pin                                           // trigger on rising edge
  TCCR1C = 0;
  TIFR1 = (1 << ICF1) | (1 << OCF1B) | (1 << OCF1A) | (1 << TOV1);   // clear pending interrupts
  TIMSK1 = 0;                                                        // disable capture interrupts
  pinMode(5, INPUT_PULLUP);                                          // capture input pin T1 (Uno pin 5)
  TCNT1 = 0;
}

but my problem is, that i have different pulse width in my signal and i want only count the 2us pulses.

All that’s left to do is poll the status of the signal and check micros(). Each time the signal goes HIGH for greater than your 2µs interval (limited by the loop iteration speed) , then subtract 1 from the count.
Also, could use another timer to check the input status at regular intervals.