0
votes

The While(busy); loop is instantly skipped. But the only place where busy can be set to 0 is in the Timer1 ISR. But Timer 1 is stopped and only ever starts when in the Pin Change ISR.

From the UART output I can tell that Timer 1 ISR happens, while Pin Change ISR never does. which should not be possible, right?

What am I missing?

In my main function:

...
    uint32_t temp = 0;

    busy = 1;
    mode = 1;

    // Timer Interrupt Init
    TCCR1B &= ~((1<<2) | (1<<1) | (1<<0));  // Makeing sure timer is not running
    TIMSK1 |=  (1 << TOIE1);                // Timer 1 overflow interrupt enable
    TCNT1 = 0;                              // Makeing sure Timer is on 0

    // Pin Change Interrupt Init
    PCICR  |= (1<<2);   // Activating PCMSK2
    PCMSK2 |= (1<<6);   // PCMSK2 -> PCINT23.. 16 seem to correspond to physical pins D 0-7

    UartSendstring("1");
    // Scanning (see ISR)
    sei();
    TCCR1B &= ~((1<<2) | (1<<1) | (1<<0));
    while(busy);
    cli();
...

Timer 1 ISR:

ISR(TIMER1_OVF_vect)
{
    UartSendstring("3");
    busy = 0;
}

Pin Change ISR:

ISR(PCINT2_vect)
{
    UartSendstring("2");
    //todo make first values not empty
    TCCR1B &= ~((1<<2) | (1<<1) | (1<<0));// CS12 - CS10 are set to 0 to stop the timer
    data[addr] |= TCNT1L;
    data[addr] |= (TCNT1H << 8);                // High and low byte are saved to data

    TCNT1 = 0;                      // Timer is reset
    TCCR1B |= ((1<<1) | (1<<0));    // CS12 is set to 1 to restart the timer with prescaler 64 -> tick time = 4us
                                    // Signal period duration is 1s / 38 000 = 26us
                                    // -> at least on timer tick in one signal period
    addr++;                         // Prepares to write to the next address with next edge
}

Uart output is:

13

edit

I tried moving the TIMSK1 |= (1 << TOIE1); to the Pin Change ISR. Now it goes in there at least once like I want it but as soon as I enable the Interrupt it triggers the ISR again and ends.

1
Have you cleared Timer1 overflow flag before enabling interrupts? It's started by default and it might get set before you stop the timer.KIIV
Setting TIFR1 = 0; did not change anything unfortunately.Nikolas
The interrupt flags are cleared by writing logic 1 into corresponding bit. So it should be more like TIFR1 = _BV(ICF1) | _BV(OCF1B) | _BV(OCF1A) | _BV(TOV1); (to clear all flags)KIIV
That did the trick - Thank you so muchNikolas
Ok, I'll make an answerKIIV

1 Answers

0
votes

As the Arduino core starts all timers by default (because of PWM), there is possibility that interrupt flags are already set and they fires as soon as you enable the corresponding interrupts. So you have to clear them before reenabling interrupts. But there is a tiny little obstacle: interrupt flags are cleared by writing logic one into corresponding bit. Therefore you have to use something like this TIFR1 = _BV(ICF1) | _BV(OCF1B) | _BV(OCF1A) | _BV(TOV1); (however as you don't use any other Timer1 interrupt, you can clear TOV1 flag only).