我正在尝试使用外部中断闪烁 LED 的简单任务。我使用的是 ATtiny10,所以只有一个用于中断的引脚(PB2)。
//cpu freq set to 1MHz
#undef F_CPU
#define F_CPU 1000000UL
//io is pin/reg macros.. PB0,PORTB,etc..
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
ISR(INT0_vect) // Interrupt Service Routine for logical change on INT0 (PB2)
{
PORTB = 0X0E; //turn PB0 High
_delay_ms(2000);
}
int main(void)
{
// initializations
//DDRB 1-output,0-input
DDRB = 0x0B; // enable PB0,1,3 as outputs..PB2 is INT0 interrupt... 1011 (3,1,0)
DDRB &= ~0x04; //1011 & w/ 1011 (~0x04) keeps bits as we set them
PORTB = 0x04; // H or L for any DDRB output pins, set all Low, 0100 pulls up PB2 resistor since it is an input
EICRA |= 0x01; //any logical change on INT0 generates interrupt
sei(); //enable global interrupts
while(1){
PORTB = 0X0E; //turn PB0 on w/ LOW signal
_delay_ms(500);
PORTB = 0x0F; //turn all off again
_delay_ms(500);
PORTB = 0X0D; //turn PB1 LOW
_delay_ms(500);
PORTB = 0x0F; //turn all off again
_delay_ms(500);
}
}
主要问题:我是否错误地设置了中断?我拉高了 INT0 引脚 PB2 的内部电阻。我还没能到达要中断的事件。如果需要更多信息,请告诉我。感谢大家。