I want to cause the external interrupt on digital pin 2 (PD2
on ATmega328). I have chosen any logic change mode. Here is my code:
void setup()
{
DDRD &= ~( 1 << DDD2 );//PD2 on input mode
EICRA |= ( 1 << ISC00 );//any logic change
PORTD |= ( 1 << PORTD2 );//pull-up enable
EIMSK |= ( 1 << INT0 );//turns on INT0
DDRD |= ( 1 << DDD5 );//PD5 on output mode
PORTD |= ( 1 << PORTD5 );//turn on PD5
SREG |= ( 1 << 7 );//global interrupts enabled
}
void loop()
{
}
ISR( INT0_vect )
{
if( ( PIND & ( 1 << PIND5 ) ) > 0 )//if turned on
{
PORTD &= ~( 1 << PORTD5 );//turn off led
}
//and vice-versa
else
{
PORTD |= ( 1 << PORTD5 );
}
}
Here is how I connect things.
NOTE: I did not find an ordinary button which just connects two contacts ( which I actually use ). Here if I understand correctly the button in the picture connects left pair and right pair of contacts so everything is connected when pushed and disconnects them from each other when released.
The problem is when I push the button no interrupt occurs. What am I doing wrong?
1 Answer 1
Pin 2 is pulled up by internal R then pulled up when button is pressed? Connect button to GND.
-
OMG. I did not understand correctly what pull-up means. Thank you all. Now it works.LRDPRDX– LRDPRDX02/28/2018 11:42:48Commented Feb 28, 2018 at 11:42
PORTD
register.