3

In my project Pin 3 of Arduino UNO is as External interrupt (INT1) and at the same time, I have to run Timer2. INT1 dosen't work When Timer2 is run. Timer2 is run just as timer. Is it my mistake or it is logical?

I connect a key to pin 3 as external interrupt and also run timer. Every time INT1 on pin 3 senses rising edge Button_ISR() is called. but because of running timer2, Button_ISR() is not called.

THis is my code:

int buttonPin = 3;
void setup()
{
  pinMode(buttonPin, INPUT);
  attachInterrupt(digitalPinToInterrupt(3), button_ISR, RISING);
  Serial.begin(9600);
  TCCR2B = 0x07; // clock frequency / 1024
  //OCR2B = 0x00;  // Output compare
  TCNT2 = 0;     // Set counter 2 to zero
  TIMSK2 = 0x01; // Enable overflow interrupt

}

void loop()
{

}


void button_ISR()
{
  Serial.print("button_ISR()");
  Serial.print("\n");
}
3
  • 1
    And if you cut out the timer config the button code is running correctly? The pin being OC2B should not matter here, since you are disabling the output compare mode. Thus the timer hardware is not connected to the pin hardware
    – chrisl
    Aug 24 at 14:37
  • Can you put a bit of debugging code in the loop to verify that pin 3 is going HIGH?
    – Cary H
    Aug 24 at 17:04
  • you have enabled the overflow interrupt. have you handled it?
    – ArduinoFan
    Aug 24 at 19:39
4

Here are my 2 cents:

Your code is missing the handler for the timer. You have enabled the Timer overflow interrupt, but you have ISR prepared for it. So, the control is ending up reaching unhandled interrupt ISR which is a Restart

Arduino is restarting all the time.

Adding one line should fix it temporarily.

ISR(TIMER2_OVF_vect) {}

Here is the updated code. I can see that the code is now running (not restarting at least)

https://wokwi.com/arduino/projects/307835582788141632

    int buttonPin = 3;
    void setup()
    {
      pinMode(buttonPin, INPUT_PULLUP);
      Serial.begin(9600);
      Serial.println("ready!");
      TCCR2B = 0x07; // clock frequency / 1024
      //OCR2B = 0x00;  // Output compare
      TCNT2 = 0;     // Set counter 2 to zero
      TIMSK2 = 0x01; // Enable overflow interrupt
      attachInterrupt(digitalPinToInterrupt(3), button_ISR, RISING);
    
    
    }
    
    void loop()
    {
    
    }

void button_ISR()
{
  Serial.print("button_ISR()");
  Serial.print("\n");
}

ISR(TIMER2_OVF_vect) {}

Here is the disassembly code found on the Wokwi Arduino simulator for your sketch

enter image description here

and if you see the location 0xf6: enter image description here

you will see that the code is doing a soft reset ( JMP 0). I hope this is helpful to you. Please feel free to ask if you have any questions.

1
  • 1
    I just changed the switch configuration to active LOW, but it should not matter.
    – ArduinoFan
    Aug 24 at 19:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.