【发布时间】:2021-05-12 17:01:35
【问题描述】:
我正在设计一个基于中断的数字计数器,它使用 atmega32 显示在 8 个 LED 上递增的值。我的问题是我的 ISR(中断服务程序)无法点亮 LED,因为我从 INT0 递增下面是我制作的代码,只有 ISR 没有点亮 LED
【问题讨论】:
-
请勿发布代码、数据、错误消息等的图片 - 将文本复制或输入到问题中。 How to Ask
我正在设计一个基于中断的数字计数器,它使用 atmega32 显示在 8 个 LED 上递增的值。我的问题是我的 ISR(中断服务程序)无法点亮 LED,因为我从 INT0 递增下面是我制作的代码,只有 ISR 没有点亮 LED
【问题讨论】:
在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++;
}
【讨论】: