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