Need help in interrupt code

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

Hi i tried to execute this code in proteus

#include
#include
ISR(INT0_vect)
{
PORTA=0;
}
ISR(INT1_vect)
{
PORTA=0b00000001;
}
void main()
{
MCUCR|=(1<<ISC00)|(1<<ISC01);
MCUCR|=(1<<ISC10)|(1<<ISC11);
GICR|=(1<<INT0);
GICR|=(1<<INT1);
DDRD&=~(1<<PD2);
DDRD&=~(1<<PD3);
DDRA=0xFF;
sei();
while(1){};
}

But i saw that without giving external interrupt on INT1, it automatically glows on. Why this is happening??

It is supposed to that, I will make low to high transition in INT1 pin and then INTerrupt 1 ISR will be called. But instead of doing so, from the very beginning of the program starts, the PA0 led continuously glows.

Please help how can i get rid of it

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Would that code not be easier to read if you wrote it as:

#include
#include
ISR(INT0_vect)
{
  PORTA = 0;
}
ISR(INT1_vect)
{
  PORTA = 0b00000001;
}
void main()
{
  MCUCR |= (1<<ISC00)|(1<<ISC01);
  MCUCR |= (1<<ISC10)|(1<<ISC11);
  GICR |= (1<<INT0);
  GICR |= (1<<INT1);
  DDRD &= ~(1<<PD2);
  DDRD &= ~(1<<PD3);
  DDRA = 0xFF;
  sei();
  while(1){};
} 

Spaces/tabs cost nothing in C.

Quote:
But i saw that without giving external interrupt on INT1, it automatically glows on. Why this is happening??

What us actually connected to that pin and also how is the LED connected to PORTA.0? It could either be that the input is floating and there appears to be activity on it or it could be the LED is active low - on when the output is 0 and off when the output is 1.

Oh and the usual comments about whether you can actually trust the simulation apply - what happens in a real AVR on a breadboard?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Clear the interrupt flags just before the sei().

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

What us actually connected to that pin and also how is the LED connected to PORTA.0? It could either be that the input is floating and there appears to be activity on it or it could be the LED is active low - on when the output is 0 and off when the output is 1.

Indeed. While the AVR model is not given, it would appear that the external interrupts are triggered on the rising edge. Is there an external pulldown to keep the pins from floating when not "made"?

(Most of us would use the internal pullup on an AVR8 and make the signals active-low.)

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Koshchi wrote:
Clear the interrupt flags just before the sei().

How?? Please give a sample code

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Interrupt flags are cleared by writing 1 to them:

GIFR = (1<<INTF1) | (1<<INTF0);

Regards,
Steve A.

The Board helps those that help themselves.