'Is my selfmade 16-bit Timer Overflow working properly?

Hello Guys its me again ive read your comments and made a minimal reproducible example for one of my main problems with my code. I also integrated the single responsibility rule and now it works even faster so thanks to you guys. However my actual problem is that my selfmade 16 Bit timer is missing the actual period time by approximatly 100 microseconds. But just sometimes and only for one measurement. So its pretty hard even to recognize it. But its still a no go for my application. I dont think it can be noise because I already tried it with the not selfmade 16bit Timer and it worked just fine. I think the problem is how I handle the overflow... but I am not sure. Can you please help me now guys?

Initializing of Timer 0 / Interrupt:

TCCR0 = 1<<CS01;        // prescaler 8 F = 8MHz / 8 = 1MHz
TIMSK = 1<<TOIE0;       // activate Overflow Interrupt

Overflow handling:

ISR(TIMER0_OVF_vect)
{
    CounterH ++;    //counting upper Byte up when lower is full
}

Counting time between Edges:

ISR(INT0_vect)
{

    if(firstedge == false)                  // if no edges passed
    {                                       // first edge passed
        firstedge = true;
        TCNT0 = 0;                          //Reset Timer 0
        CounterH = 0;                       //Reset "upper byte" of Timer 0 
    }
    else                                    // if firstedge is true
    {
        periodduration_us = (TCNT0)|(CounterH<<8);
                   // Periodduration equals Timer0 and upper byte
        secondedge = true;                  // second edge passed
    }
}

I use the ATmega32A if this is relevant. I Hope this post is better than my last.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source