ATMEGA328 FALLING EDGE EXTERNAL INTERRUPT GETTING STUCK

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi, 

I am trying to use the falling edge on external interrupt 1 ( pin 3 on arduino). 

I have a perfectly debounced switch ( I am using a max6816 deboucer and I confirmed on the oscilloscope that the signal is clean . It's a clean High to low transition when the button is pressed). 

The problem I am getting is that as long as the button is pressed, the code seems to stay inside the ISR. 

Here is my arduino code. I am using the "BUTTON_PRESSED" variable for something else, so you can ignore it for now. 

when I run the serial monitor, it keeps printing "outside" as expected. 

When I pressed the button, It stops as long as I am pressing the button. If my registers are properly configured (EICRA |= (1<<ISC11); EICRA &= (0<<ISC10);) for the int on falling edge option( page 89 of atmega328 datasheet), this shouldn't happen. The code should just stop for a fraction of a second to go inside the ISR, during that time the serial print would stop, then the code will directly get out of the ISR even if the button is still pressed.  

Could anyone explain what I am doing wrong here?

 

 

volatile bool BUTTON_PRESSED = 0;

void setup() {
  // put your setup code here, to run once:
noInterrupts(); 
EICRA |= (1<<ISC11);
EICRA &= (0<<ISC10);
EIFR |= (1 << INTF1 );//clearing interrupt flag, just in case
EIMSK |= (1 << INT1); 
interrupts(); 
Serial.begin(9600);
}

void loop() {
Serial.println("outside");
}

 

ISR(INT1_vect){
                BUTTON_PRESSED=1; 
              }

This topic has a solution.
Last Edited: Fri. Feb 2, 2018 - 03:17 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think that you will find that

 

EICRA &= (0<<USC10);

is equivalent to 

 

EICRA = 0;

Generally (0<<USC10) does nothing, but, in this case, it does everything (bad).

 

Jim

 

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ka7ehk wrote:

I think that you will find that

 

EICRA &= (0<<ISC10);

I will bet that line of code was supposed to be 

 

EICRA &= ~(0<<ISC10);

To clear the ISC10 bit in the EICRA register

 

EDIT: I suggest replacing these two lines of code (corrected to match what the OP asked for in the first post - The falling edge of INT1 generates an interrupt request.)

EICRA |= (1<<ISC11);
EICRA &= ~(0<<ISC10);

with this line only

EICRA = (1<<ISC11);

 

"void transmigratus(void) {transmigratus();} // recursio infinitus" - larryvc

"It's much more practical to rely on the processing powers of the real debugger, i.e. the one between the keyboard and chair." - JW wek3

"When you arise in the morning think of what a privilege it is to be alive: to breathe, to think, to enjoy, to love." -  Marcus Aurelius

"Make XMEGA Great Again!"  - Greg Muth

Last Edited: Fri. Feb 2, 2018 - 01:01 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

haythamhakla, As long as you are keeping the button pressed the interrupts will keep firing and re-entering the ISR thus continuously interrupting your code in main.  It is up to you to write the code correctly to avoid this.

 

You do need to also fix your code as I suggested in my post above.

"void transmigratus(void) {transmigratus();} // recursio infinitus" - larryvc

"It's much more practical to rely on the processing powers of the real debugger, i.e. the one between the keyboard and chair." - JW wek3

"When you arise in the morning think of what a privilege it is to be alive: to breathe, to think, to enjoy, to love." -  Marcus Aurelius

"Make XMEGA Great Again!"  - Greg Muth

Last Edited: Fri. Feb 2, 2018 - 01:19 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks Larry, 

I think you mean EICRA &= ~(1<<ISC10) instead of EICRA &=(~0<<ISC10).

 I completely missed that. The code now works as expected. 

Last Edited: Fri. Feb 2, 2018 - 03:21 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks Ka7ehk, 

that was exactly the problem. It is solved now.