Attiny85 Strange things happening with code in ISR

Hi Guys,

Have some strange goings on, and maybe someone can shed some light on what's happening.

ATtiny85
ATTinyCore
8MHz internal
(more info if needed)

I have an PC interrupt on PB2 (PIN 7). The ISR then toggles PB1 (PIN 6) twice with a delay in between. And that's it.

If I have the "toggle" code in the ISR then everything works fine. However, if I just set a flag in the ISR then the "toggle" code is in the loop(), PB1 doesn't toggle.

This code works fine:

#define pulseIn 2
#define pulseOut 1
bool pinOut = false;

void setup() {
  DDRB &= ~bit(2);    //  Initialize PB2 (PIN 7) as an input. This detects pulse from SCR
  PORTB &= ~bit(2);  //  Set PB2 (PIN 7) low to start
  DDRB |= bit(1);       //  Initialize PB1 (PIN 6) as an output. This pin fires the transistor
  PORTB &= ~bit(1);  //  Set PB1 (PIN 6) low to start

  cli();                        // disable interrupts
  GIMSK |= bit(5);   // turns on pin change interrupts
  PCMSK |= bit(2);  // turn on interrupts on pins PB2 (PIN 7)
  sei();                       // enable interrupts
}

void loop() {

//  Nothing here

}

ISR(PCINT0_vect)  {
  PINB = bit(1);          //  Toggle PB1
  delay(1);                  //  Wait 1ms
  PINB = bit(1);          //  Toggle PB1
}

However. The following code using a simple flag in the ISR doesn't work. I get no response on PB1 (PIN 6).

#define pulseIn 2
#define pulseOut 1
bool pinOut = false;

void setup() {
  DDRB &= ~bit(2);     //  Initialize PB2 (PIN 7) as an input. This detects pulse from SCR
  PORTB &= ~bit(2);   //  Set PB2 (PIN 7) low to start
  DDRB |= bit(1);       //  Initialize PB1 (PIN 6) as an output. This pin fires the transistor
  PORTB &= ~bit(1);  //  Set PB1 (PIN 6) low to start

  cli();// disable interrupts
  GIMSK |= bit(5);   // turns on pin change interrupts
  PCMSK |= bit(2);  // turn on interrupts on pins PB2 (PIN 7)
  sei();                       // enable interrupts
}

void loop() {
  if (pinOut) {   //  pinOut flag set in ISR
    PINB = bit(1);            //  Toggle PB1
    delay(1);                    //  Wait 1ms
    //    delayMicroseconds(500);  //  Tried this as well but no joy
    PINB = bit(1);            //  Toggle PB1
    pinOut = false;         //  Set flag false
  }
}

ISR(PCINT0_vect)  {
  pinOut = true;
}

Any ideas?

As it is, the code runs fine with the toggle code in the ISR, but I know that this is not best practice, especially as I am using an delay().

Any help gratefully accepted.

Cheers,

Matt

Someone else will probably come along with a better guess, but try declare as:

volatile bool pinOut = false;

Read more: volatile - Arduino Reference

@toddnz

You are of course 100% correct.

I did actually figure it out pretty much straight away, but have been testing some other things and didn't come straight back to the forum.

Works perfectly now.

Cheers,
Matt

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.