我正在尝试使用外部中断闪烁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的内部电阻。我还没能到达中断事件。如果需要其他信息,请告诉我。感谢大家。
我的设置中唯一缺少的项目是设置EIMSK(外部中断屏蔽)寄存器。设置为“1”时,将启用“外部中断”功能。有趣的挑战想出那一个!不要跳过数据表孩子,否则你最终会导致更多问题!
...
EIMSK = 0x01; //enable external interrupts
EICRA |= 0x01;
sei();
...