简体   繁体   English

更改中断方式时,为什么我的输入信号时序会改变?

[英]Why does my input signal timing change when changing interrupt method?

The AVR in use is ATmega2560. 使用的AVR是ATmega2560。

I've got an input signal that has a pulse width of 1 second that is generated. 我有一个产生的脉冲宽度为1秒的输入信号。

This signal is attached to an external interrupt pin on my AVR (INT0). 该信号连接到我的AVR(INT0)上的外部中断引脚。

INT0 is being initialized as follows: INT0正在如下初始化:

Code: 码:

DDRD &= ~(1 << PD0);
PORTD |= (1 << PD0);
EIMSK  = 1 << INT0; // enable
EICRA |= (1 << ISC00) | (1 << ISC01); // trigger on rising edge

sei(); //global interrupts

The mission of the ISR for this external interrupt is to a) figure out which edge (first case should be rising) and b) perform action based on which edge ISR针对此外部中断的任务是:a)找出哪个边沿(第一种情况应该上升),以及b)根据哪个边沿执行操作

The ISR looks something like this: ISR看起来像这样:

Code: 码:

ISR(INT0_vect)
{

if(EICRA == 0x02)
{
    // falling edge detected
    doFallingEdgeFunction_lightLED0();

    // quickly change the trigger to capture opposite edge
    EICRA |= (1 << ISC00) | (1 << ISC01); // trigger on rising edge

}

else if(EICRA == 0x03)
{
    doRisingEdgeFunction_lightLED1();

    // change trigger on falling edge
    EICRA = (1 << ISC01);
    EICRA &= ~(1 << ISC00);
}

}

It is able to detect the edges; 它能够检测边缘; the correct LEDs light up, but for some reason changing the edge interrupt bit in the ISR decreases my input signal to 0.1 second width instead of the full 1 second width. 正确的LED点亮,但是由于某种原因,更改ISR中的边沿中断位会将我的输入信号减小到0.1秒宽度,而不是整个1秒宽度。

On the scope, I see the original signal being mirrored but with 10x less width! 在示波器上,我看到原始信号被镜像,但宽度减小了10倍! If I remove the "switching trigger" items, the signal is fine. 如果删除“切换触发器”项,则信号很好。

From section 15.2.2, p. 从第15.2.2节开始。 114 114

Note: 1. n = 3, 2, 1or 0. When changing the ISCn1/ISCn0 bits, the interrupt must be disabled by clearing its Interrupt Enable bit in the EIMSK Register. 注:1. n = 3、2、1或0。更改ISCn1 / ISCn0位时,必须通过清除EIMSK寄存器中的中断允许位来禁止该中断。 Otherwise an interrupt can occur when the bits are changed. 否则,当这些位更改时,可能会发生中断。

EDIT: 编辑:

EIFR – External Interrupt Flag Register EIFR –外部中断标志寄存器

• Bits 7:0 – INTF7:0: External Interrupt Flags 7 - 0 •位7:0 – INTF7:0:外部中断标志7-0

When an edge or logic change on the INT7:0 pin triggers an interrupt request, INTF7:0 becomes set (one). 当INT7:0引脚上的边沿或逻辑变化触发中断请求时,INTF7:0会被置1(一个)。 If the I-bit in SREG and the corresponding interrupt enable bit, INT7:0 in EIMSK, are set (one), the MCU will jump to the interrupt vector. 如果将SREG中的I位和EIMSK中相应的中断允许位INT7:0设置为1,MCU将跳转到中断向量。 The flag is cleared when the interrupt routine is executed. 当执行中断程序时,该标志被清除。 Alternatively, the flag can be cleared by writing a logical one to it. 或者,可以通过向其写入逻辑标志来清除该标志。

Try clearing the flag in the interrupt 尝试清除中断中的标志

EIFR = (1 << INT0); 

Don't use 不要使用

EIFR |= (1<< INT0);  // DO NOT USE

That will clear all pending flags. 这将清除所有挂起的标志。

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

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