Some code from an older thread to measure short pulses, - see the discussion - http://arduino.cc/forum/index.php/topic,96971.0.html -
//
// FILE: PulseWidthMeter.pde
// AUTHOR: Rob Tillaart
// DATE: 2012-apr-01
//
// LINK: http://arduino.cc/forum/index.php/topic,96971.45.html
//
// PURPOSE: measures short pulses that are send one per second
//
#include <avr/io.h>
#include <avr/interrupt.h>
volatile unsigned int count = 0;
unsigned int pulseCounter = 0;
void setup()
{
Serial.begin(38400);
Serial.println("PulseWidthMeter (timer1) 0.2");
pinMode(3, INPUT);
}
void loop()
{
// reset Timer1 registers and the counters (timer must be stopped before reset of counters!.
TCCR1A = 0;
TCCR1B = 0;
count = 0;
TCNT1 = 0;
TIMSK1 = (1 << TOIE1); // enable Timer1 overflow interrupt:
while ((PIND & B00001000) == B00000000); // wait for HIGH
TCCR1B |= (1 << CS10); // Set CS10 bit so timer runs at clock speed: 16 MHz
while ((PIND & B00001000) == B00001000); // wait for low
TCCR1B = 0; // stop counting
pulseCounter ++;
// Read the counters and convert to long
unsigned long total = count * 65536L + TCNT1; // Might need a +1 to correct start/stop calibration.
float usec = (1.0 * total) / 16;
// Display values
Serial.print(total, DEC);
Serial.print(" \t ");
Serial.println(usec, 1);
Serial.print("pulses: ");
Serial.println(pulseCounter);
}
// count the overflows in IRQ
ISR(TIMER1_OVF_vect)
{
count++;
}