简体   繁体   English

中断是由启用中断之前发生的更改引起的

[英]Interrupt occurs from change that occurred before interrupt is enabled

I'm using an Arduino Uno with the Atmega328p microcontroller. 我正在将Arduino Uno与Atmega328p微控制器一起使用。 I'm trying to use INT1 as a software interrupt. 我正在尝试将INT1用作软件中断。 I manually set INT1's associated PORTD3 high or low depending on external info. 我根据外部信息手动将INT1的关联PORTD3设置为高电平或低电平。 What I want is to set the pin high or low at the startup of the device and then enable the interrupt on the pin without causing an interrupt if i set the pin high before enabling the interrupt. 我想要的是在设备启动时将引脚设置为高电平或低电平,然后在启用中断之前将引脚设置为高电平,然后在不引起中断的情况下启用引脚上的中断。

It doesnt seem to matter where I enabled the interrupt--if i changed the state of the pin at some point the interrupt will occur once its enabled. 在哪里启用中断似乎无关紧要-如果我在某个时候更改了引脚的状态,则一旦启用中断就会发生中断。 Here is a snippet of the code: 这是代码片段:

int main(void)
{
    DDRD |= (1<<DDD7)|(1<<DDD3);//7 for siren 3 for software int1
    USART_Init(MYUBRR);//Initialize USART
    while(door!='C'  && door!='O'){//get door state on startup
        door = getDoorState();
    }
    if(door=='O')
        PORTD |= 1<<PORTD3;
    else
        PORTD &= ~(1<<PORTD3);
    EIFR &= ~(1<<INTF1);//clear flag bit before enable, I'd heard this may help????
    EIMSK |= (1<<INT1);//enable door switch interrupt
    EICRA |= (1<<ISC00)|(1<<ISC10);//int1 and int0 set for any logical change

    sei();//global interrupt enable

    while (1) 
        {}
}

As soon as the global interrupt is enabled by a call to sei() the interrupt will occur if PORTD3 is high, regardless of where PORTD3 was set high or where sei() is. 通过调用sei()启用全局中断后,如果PORTD3为高电平,则无论PORTD3设置为高电平还是sei()在哪里,中断都会发生。 Calling sei() should never cause an interrupt in this code, ideally. 理想情况下,调用sei()绝不应在此代码中引起中断。

4386427 is correct. 4386427是正确的。 The bit is cleared by setting it to a one, not zero. 通过将其设置为1而不是0来清除该位。 Seems counter-intuitive to me so it threw me off but it works now. 对我来说似乎违反直觉,所以它使我失望了,但现在可以使用了。

EIFR |= (1<<INTF1);

EIFR &= ~(1<<INTF1) is incorrect. EIFR &= ~(1<<INTF1)不正确。

The correct way of doing this is EIFR = 1<<INTF1 . 正确的做法是EIFR = 1<<INTF1

Datasheet says: the flag is cleared by writing a '1' to it. 数据表说:通过向其写入1清除该标志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM