volatile unsigned PulseTime = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
analogWrite(10,255/2);
noInterrupts();
TCNT1=0;
TIFR1=0;
TCCR1B|=(1<<CS10);
TCCR1B|=(1<<ICES1);
TIMSK1|=(1<<ICIE1);
interrupts();
}
void loop() {
// put your main code here, to run repeatedly:
unsigned pulseTime=0;
noInterrupts();
pulseTime=0;
interrupts();
if(pulseTime){
Serial.println(pulseTime);
}
}
ISR(TIMER1_CAPT_vect){
static unsigned RisingEdgeTime = 0;
static unsigned FallingEdgeTime = 0;
if(TCCR1B&(1<<ICES1)){
RisingEdgeTime = ICR1;
TCCR1B&=~(1<<ICES1);
}
else{
FallingEdgeTime=ICR1;
TCCR1B|=(1<<ICES1);
PulseTime = FallingEdgeTime - RisingEdgeTime;
}
}