input capture triggers erratically

Hi, I have set up timer1 for input capture like so:

ISR(TIMER1_CAPT_vect) {

    // make icr1 available outside ISR, not sure if this is needed at all, but who cares, figure out later
    icr1 = ICR1

    // timer 1 disable all IRQs
    IMSK1 = 0;

    // timer 1 no clock source --> full stop timer 1
    TCCR1B = 0;
}

And I start the capture mode like so:

    cli();

    // stop timer 0 and timer 1 prescaler (and thus timer 0 and 1 as well)
    GTCCR = (1 << TSM) | (1 << PSRSYNC);

    // initialize all counters to 0
    TCNT0 = 0;
    TCNT1 = 0;

    // no noise canceler, trigger on falling edge, no prescaler / use system clock
    TCCR1B = (0 << ICNC1 | 0 << ICES1 | 0 << CS12 | 0 << CS11 | 1 << CS10);

    // Timer/Counter1, Input Capture Interrupt Enable
    TIMSK1 = (1 << ICIE1 );           

    // reset input capture flag
    TIFR1 = ( 1 << ICF1 );                         

    // go
    GTCCR = 0;
  
    sei();

Actually this is part of more code, but this is how I start it. In addition I configure PIN 8 (PORT B, 0) as input and enable the pullup.

Now what I would expect is that this starts timer 1 and a falling edge triggers the input capture. This does work.

However I would also expect that if I do not connect anything to PIN8, then it should not trigger the ISR. However it is triggered after 1.78 to 1.82 seconds and I have absolutely no clue why. I thought that it might be connected with other ISRs but I double checked and this is not the case.

It is definitely the capture ISR that starts to run even though the pin is pulled up.

Any ideas what could be the cause and how to fix it? Maybe some flag that I did not properly set? Something else?

Cheers, Udo

Try setting TCCR1A to 0, this disables PWM and other functions on Timer/Counter 1

I am not sure you need (or should) fiddle with GTCCR register, comment out the code that modifies that register.

Also, of you do use the icr1 variable, don't forget to disable interrupts when you access the value.

Good luck!

By now I found the issue. In some somewhat unrelated piece of code I accidentally overwrote TCCR0B, especially I set the reserved bits that always "read zero" to 1.

The reason why I did this was that I put a flawed constraint into the parameters of some inline assembler code. Thus I ended up shooting myself into the foot :frowning:

But thanks for your recommendation, I will switch off the PWM anyway. Every source for oscillation in my environment is undesirable.

Cheers, Udo