I'm trying to use light a led placed in PB0, when detecting the INT1 interrupt on an atmega328p without an external ocilator. I'm using avrdude to upload the code to the micro. This is the code:
#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
int main(void)
{
cli();
UCSR0B = 0;
DDRB |= 0b00000001;
DDRD &= 0b11111101;
EICRA |= 0b00001111;
EIMSK |= 0b00000010;
EIMSK &= 0b11111110;
sei();
while(1)
{
PORTB &= 0b11111110;
}
}
ISR(INT1_vect)
{
PORTB |= 0b00000001;
_delay_ms(1000);
}
But doesn't work. The LED stays at a medium-light flare and does nothing. But when uploading the same code through the Arduino IDE it works as it has to:
int main()
{
cli();
DDRB |= 0b00000001;
DDRD &= 0b11111101;
EICRA |= 0b00001111;
EIMSK |= 0b00000010;
EIMSK &= 0b11111110;
sei();
while(1)
{
PORTB &= 0b11111110;
}
}
ISR(INT1_vect)
{
PORTB |= 0b00000001;
_delay_ms(1000);
}
I can't understand anything.
Ok i found some things in your code that should not be there.
if you don´t need the
UCSR0Bregister don´t paste it in your code! The question now is where are you awaiting the interrupt. It seems to be onPORTDonPIND3(from the Datasheet)?This makes no sense
Cause
DDRDis initially setup to0b00000000after reset! So you are trying to do this:But the
PORTDconfiguration seems to be correctPIND3is setup as input. The question now is do you need the internal pullup resistor?The LED seems to be connected on
PORTBPINB0? So everything is fine. Lets checkup the interrupt configuration:So the whole code should look like:
Please give me feedback if that solved your problem?