atmega32 上基于中断的 LED 计数器

问题描述

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

enter image description here

解决方法

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++;
}