l正在设计一个基于中断的数字计数器,该计数器使用atmega32在8个LED上显示值的增量。我的问题是我的ISR(中断服务程序(无法点亮LED,因为从INT0开始的l增量是下面的代码l,只有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++;
}