-1

我正在设计一个基于中断的数字计数器,它使用 atmega32 显示在 8 个 LED 上递增的值。我的问题是我的 ISR(中断服务程序)无法点亮 LED,因为我从 INT0 递增下面是我制作的代码,只有 ISR 没有点亮 LED

在此处输入图像描述

4

1 回答 1

0

SR中,count变量在堆栈上。它的值不会调用中持续存在。


这是您的原始代码[带注释]:

SR(INT0_vect)
{
    DDRA = 0xFF;
// NOTE/BUG: this is on the stack and is reinitialized to zero on each
// invocation
    unsigned char count = 0;

// NOTE/BUG: this will _always_ output zero
    PORTA = count;
// NOTE/BUG: this has no effect because count does _not_ persist upon return
    count++;

    return;
}

您需要添加static以使值保持不变:

void
SR(INT0_vect)
{
    DDRA = 0xFF;
    static unsigned char count = 0;

    PORTA = count;
    count++;
}
于 2021-05-12T18:17:04.567 回答