How to measure pulse duration with INT0 and TIMER0

hi, i am using atmega328p, i want to measure duration of the pulse in INT0 pin.
i think that i can set INT0_vect for activating TIMER0_OVF_vect which is prescaled and calculated for overflow for every 1 microseconds. but it is not working the way i expected. what can i do?
i think problem is with INTF0
I am expecting from 14 to 75 microseconds duration

ISR(INT0_vect){  
	num = 0; 
	TCNT0 = 0;
	TCCR0B |= (1 << CS01);
	
}

ISR(TIMER0_OVF_vect){
	     num++;
        if(!(INTF0 &(1<<0))){TCCR0B &= ~(1 << CS01);}
		             
}

It is impossible to say anything without your full code.

#define F_CPU 8000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdint.h>
#include <stdbool.h>
#include <util/delay.h>

volatile uint64_t num= 0;

volatile uint64_t counter = 0;
volatile uint64_t compare = 1005;
volatile uint64_t counter_delay = 0;
volatile bool count = true;


ISR(TIMER2_OVF_vect){                               
	TCCR2B &= ~(1 << CS22);
	TCNT2 = 0;
	TCNT2 = vaxt;
	if(vaxt == 11){
		vaxt = 247;
		PORTD &= ~(1 << PIND3);
	}
	else if(vaxt == 247){
		vaxt = 11;
		PORTD |= (1 << PIND3);
	}
	
	TCCR2B |= (1 << CS22);
}


ISR(INT0_vect){   
	num = 0; 
	TCNT0 = 0;
	TCCR0B |= (1 << CS01);
	
}

ISR(TIMER0_OVF_vect){
        sayi++;
		if(!(INTF0 &(1<<0))){TCCR0B &= ~(1 << CS01);}
		             
}


ISR(TIMER1_OVF_vect){
	switch(count){
		case true:
		counter++;
		if(counter >= compare){
			PORTB |= (1 << PINB5);
			counter = 0;
			count = false;
		}
		break;
		case false:
		counter_delay++;
		if(counter_delay >= 65){
			PORTB &= ~(1 << PINB5);
			count = true;
			counter_delay = 0;
		}
		break;
	}	
}

int main(void)
{
	DDRD |= (1 << PORTD3);    
	DDRB |= (1 << PORTB5);    
	DDRD &= ~(1 << PORTD2);   
	
	cli();
	TCCR2A &= ~(1 << WGM20);
	TCCR2A &= ~(1 << WGM21);
	TCCR2B &= ~(1 << WGM22);
	TCCR2B &= ~(1 << CS20);
	TCCR2B &= ~(1 << CS21);
	TCCR2B |= (1 << CS22);
	TIMSK2 |= (1 << TOIE2);
		
	EICRA |= (1 << ISC00);
	EICRA |= (1 << ISC01);
	EIMSK |= (1 << INT0);
	
	
	TCCR0B &= ~(1 << WGM02);
	TCCR0B &= ~(1 << CS00);
	TCCR0B &= ~(1 << CS01);
	TCCR0B &= ~(1 << CS02);
	TIMSK0 |= (1 << TOIE0);
	
	TCCR1B |= (1 << CS10);
	TCCR1B &= ~(1 << CS11);
	TCCR1B &= ~(1 << CS12);
	TIMSK1 |= (1 << TOIE1);
	
	sei();
	while (1)
	{
		if(num> 25){
			compare = 50;
		}
		else{compare = 600;}
	}		
		
	}

Please describe what you expected from the code and how it actually works. And what is the difference between latter and former?

as i said, the pulse goes to INT0 pin (PIND2). when INT0 fired, ISR(INT0_vect) activates TIMER0 clock source by "TCCR0B |= (1 << CS01);" and it sets num to 0 , so timer0 is starting to count milliseconds from 0 by incrementing num variable in each overflow interrupt.

ISR(INT0_vect){   
	num = 0; 
	TCNT0 = 0;
	TCCR0B |= (1 << CS01);
	
}

ISR(TIMER0_OVF_vect){
        num++;
		if(!(INTF0 &(1<<0))){TCCR0B &= ~(1 << CS01);}
		             
}

and in while loop it compares if variable num is greather than 25 or not. if num is greather than 25 so led starts blinking faster.

		if(num> 25){
			compare = 50;
		}
		else{compare = 600;}
	

Thanks, and how it works in reality?

The "num" variable is not counting i guess, i am incrementing pulse width more than 30 milliseconds but led is not blinking as i expected

Is it blinked at all? (with interval of compare =600) ?

yes it is blinking with interval of compare = 600

What is the sayi variable? Just a typo?

when i copied my code to forum it was accidentally happened.
it is like this in real:

ISR(TIMER0_OVF_vect){
        num++;
		if(!(INTF0 &(1<<0))){TCCR0B &= ~(1 << CS01);}
		             
}

Could it also happened when you uploaded the code to the board? :slight_smile:

Could you add the diagnostic message to the main loop of the code, that print the num value to the terminal immediately after the value was changed?

No, it can not, because i checked this

Is one of these lines misplaced?

no, the logic is i am making tcnt zero, then make it equal to desired time

Is it a continuous pulse train that arrives at INT0-pin? If so, what is the frequency?

Your thread title says that you want to use only INT0 interrupt and Timer0; if so, why have you used all three timers in post #3?

Do you have an Arduino UNO Board?

yes INT0 gets continious 500Hz pulses, the pulse length strarts from 14microseconds. i need to measure the length of this pulse and if it is longer than 14 microseconds the led have to blink faster. The other timers are about transmitting part and blinking part of LED. I just got that i should use CTC mode and PWM mode for other timers. But i still dont know how to measure the pulse length. I atmega328p set for 8Mhz internal oscillator.

Sounds contradictory. 500Hz consecutive pulses should be approximately 2ms long

Please, clarify:
1. 500 Hz pulse means 500 cycles/sec. Correct?
2. Pulse length means period of a cycle. Correct? Or it is length of On-period?
3. Period of the 500 Hz signal is 2000 us. Then what is this: 14 us? //Edit
4. Do you want measure the period using INT0 and Timer0? Is it a home work exercise or some part of a project?

5. Do you have an Arduino UNO Board like below (Fig-1)? This Board can be operated at 8 MHz by programming the "System Clock Prescaler" in order to play with your codes of post #3.
UNOBoardPic
Figure-1: