基于中断的LED在atmega32上递增计数器



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

最新更新