Interrupt in IDE 1.8.19 with Attiny 1614 (also 1624)

Hello. Please advise me. When I write the program below and run it in attiny, I don't see the first two states (130 and 150) of the variable "a" in the ISR routine, because it seems that the variable "a" is only updated at the end of the loop(), so after some time I only see "244". How do I store the contents of the variable in the middle of the loop() loop so that I can see all three values of the variable sequentially in the interrupt routine? Thank you.

// attiny 1614
byte a;

void setup() {
  Serial.begin(9600);
  RTC.CLKSEL = RTC_CLKSEL_INT32K_gc; 
  RTC.PITINTCTRL = RTC_PI_bm;
  while(RTC.PITSTATUS & RTC_CTRLABUSY_bm){}
  RTC.PITCTRLA = RTC_PERIOD_CYC8192_gc | RTC_PITEN_bm;
  delay(100);   
  sei();
}

ISR(RTC_PIT_vect){
   RTC.PITINTFLAGS = RTC_PI_bm;
   Serial.println(a);
}

void loop() {
  a=130;
  delay(1000);
  a=150;
  delay(1000);
  a=244;
  delay(1000);
}  

byte a needs to be declared volatile

volatile byte a;

Don't print from within an ISR. inside the ISR interrupts are disabled, and print may need USART interrupts.
[edit] in this case it works, but still would be careful with it.

sei(); is not doing any harm, but MegaTinycore is setting interrupts on by default, so you actually don't need it.

Thank you. Everything's fine with the declaration. I will not use USART inside the ISR, I put it there only for debugging the program.

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